ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ย
C programming Lab 2
1. 1
University of Sargodha
Mandi Bahauddin campus
LAB Sheet#2
C Lab Report Submitted By:
Name: M Umar
RollNo: BSSE-F17-18
Submitted To:
Department of CS & IT University of Sargodha M.B.Din
Lab Date:2 January 2018 Marks & Signature
Submission Date: 16th
January 2018
2. 2
Program No. 1
Title
Write a program to declare two integer and one float variables then initialize them to 10,
15, and 12.6. Also print the variable values in the screen.
Problem Analysis:
In this problem we will need three variables. The data type of two variables will
be integer and one will be float. In this we have to simply declare three variables and
assign them values.
Input Variables
Processing
variables/calculatio
ns
Output Variables Header files
Int a,b printf --------- Stdio.h
Float c
Algorithm:
1. Start.
2. Declare three variables (int a,b, float c)
3. Assign values to variables
4. Use printf to display the output.
5. Stop
Code:
#include<stdio.h>
int main(void)
{
int a,b;
float c;
a=10;b=15;c=12.6;
printf("%d %d %.1f",a,b,c);
return 0;
}
Output:
10 15 12.6
Program No. 2
Title:
Write a C program to prompt the user to input 3 integer values and print these values in
forward
and reversed order.
Problem Analysis:
In this we have to get three variables of data type integer from the user. We will
need three integers int a, int b, int c. We will use scanf () to get values from the user and
simply use
printf () to print values in forward and reverse order.
3. 3
Input variable
Processing
variables/calculation
s
Output variables Header Files
int a,b,c; printf() ----- stdio.h
Algorithm:
1. Start
2. Declare three variables int a, b, c
3. Use scanf to get values from user
4. Use printf to print values in forward and reverse order
5. Display the result
6. Stop
Code:
#include<stdio.h>
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("Values in forward order = %dt%dt%d",a,b,c);
printf("nnValues in reverse order = %dt%dt%d",c,b,a);
return 0;}
Output:
5 6 7
Values in forward order = 5 6 7
Values in reverse order = 7 6 5
Program No. 3
Title:
To calculate simple and compound interest.
Problem Analysis:
In the above problem, we have to calculate two things, Simple Interest
and compound interest. We need three input variables for both simple and compound
interest. The three variables will be of float data type. We need the values of principle,
time and rate. Hence, the data types of these values will be float.
Si=(p*r*t)/100 : ci=p*(1+r/100)^t-p;
To use the power function we will have to include math.h header file.
Input variables Processing variables Output variables Header files
float p si=(p*r*t)/100 float si stdio.h
float r ci=p*(1+r/100)^t-p float ci Math.h
float t
4. 4
Algorithm:
1. Start
2. Declare three variables of data type float float p,r,t
3. Use scanf() to get values from the user.
4. Declare an output variable of data type float, float si, to calculate and store the
values of Simple interest. Si=(p*r*t)/100.
5. Declare another output variable of data type float, float ci, to calculate and
store the values of compound interest. ci=p*(1+r/100)^t-p
6. Use printf() to show the output.
7. Stop
Code:
#include<stdio.h>
#include<math.h>
int main()
{
float p,r,t,si,ci;
printf("Enter the values of principle, rate and time =");
scanf("%f%f%f",&p,&r,&t);
si=(p*t*r)/100;
ci=p*pow(1+r/100,t)-p;
printf("Simple interest =%fncompound interest =%f",si,ci);
return 0;
}
Output:
Enter the values of principle, rate and time =2000 3.4 32
Simple interest =2176.000000
compound interest =3830.257813
Program No. 4
Title:
To swap two integers using a third integer.
Problem analysis:
In this we have to declare two variables and get the values from users by using
scanf(). Then declare another temporary variable temp to swap the values. Temp=a;
A=b; B=temp;
Input Varibles
Processing
variables
Output variables Header files
5. 5
int a; Temp=a; -------- Stdio.h
int b; A=b;
int temp; B=temp;
Algorithm:
1. Start
2. Declare three variables int a,b,temp;
3. Use scanf to get values from the user.
4. To swap th values ,we will use an expression. temp=a;a=b;b=temp;
5. Use printf() to show output.
6. Stop
Code:
#include<stdio.h>
int main(){
int a,b,temp;
scanf("%d%d",&a,&b);
printf("values before swapping= %dt%d",a,b);
temp=a;a=b;b=temp;printf("nnvalues after swapping= %dt%d",a,b);
return 0;}
Output:
6 9
values before swapping= 6 9
values after swapping= 9 6
To swap two values without using third variable.
Problem Analysis:
In this we have to declare two variables of data type integer int a, int b. We will
use scanf() to get the values of a & b from the user. To swap the values simply use
printf() statement.
Printf(โ%dt %dโ,b,a);
Input variable processing variables Output variable Header files
int a; printf stdio.h
int b;
Algorithm:
1. Start
2. Declare two variables of data type integer int a,b.
3. Use scanf() to get values from the user.
4. Use printf() to swap th values.
5. Display the result
6. 6
6. Stop
Code:
#include<stdio.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("values before swapping= %dt%d",a,b);
printf("nnvalues after swapping= %dt%d",b,a);
return 0;
}
Output:
7 2
values before swapping= 7 2
values after swapping= 2 7
Program No. 5
Title:
To check that the input number is even or odd (a). Modulus Operator.
Problem Analysis:
In this we will have to get a number from the user. The data type of the number will be
integer int n. We will use modulus operator. If the mode of n%2=0 then it is even,
otherwise it will be an odd number. We will use if-else statement.
Input variable Processing variables Output variable Header files
int n; if(a%2==0),even stdio.h
else odd
Algorithm:
1. Start
2. Declare an variable int n, and use scanf() to get values from the user.
3. Use modulus operator and if-else statement to check whether the number is
even or odd.
4. If(a%2==0) then number is even else the number will be odd.
5. Display the result
6. Stop
Code:
#include<stdio.h>
int main()
{
int n;
7. 7
scanf("%d",&n);
if(n%2==0)
{
printf("%d is an even number",n);
}
else{
printf("%d is an odd number",n);
}
return 0;
}
Output:
57
57 is an odd number
To check that the input number is even or odd.(b). Using bitwise operator.
Problem Analysis:
We can check that the number is even or odd by using AND (&) bitwise
operator. In this we will use scanf() to get value from the user and use AND to check
whether it is even
or odd. If the AND of n will be equal to 1, than it will be odd number, otherwise it will be
even number.
Algorithm:
1. Start
2. Declare an variable of data type integer to get number from user, int n.
3. Use scanf() to get n from user.
4. Use if(n&1) to check whether the number is even or odd.
5. If n&1 , then it is odd number else it is even.
6. Use printf() to show the output.
7. Stop
Code:
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
if(n&1){
printf("%d is an ODD number",n);
}else{
printf("%d is an EVEN number",n);}
return 0;}
Output:
8. 8
4
4 is an EVEN number
To check that the input number is even or odd. (C). Without Using Modulus and
bitwise operator.
Problem Analysis:
In this we have to check the input number whether it is even or odd without
using modulus or bitwise operator. We will use expression n/2*2==n.
Algorithm:
1. Start
2. Declare an variable int n.
3. Use scanf() to get values from the user.
4. If(n/2*2==n) then it is even else it is an odd number
5. Display the result
6. Stop
Code:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
if(n/2*2==n)
{
printf("%d is an Even number",n);
}
else{
printf("%d is an odd number",n);
}return 0;}
Output:
5
5 is an odd number
Write a program to check odd or even number. (d) using conditional operator.
Problem analysis:
In this we will have to get a number from the user. The data type of the number will be
integer int n. We will use conditional operator. If the mode of n%2=0 then โ?โ statement
will execute else โ:โ statement will execute.
Input variable Processing variables Output variable Header files
int n; (a%2==0) ? ------- stdio.h
:
Algorithm:
9. 9
1. Start
2. Declare an variable int n, and use scanf() to get values from the user.
3. Use modulus operator and if-else statement to check whether the number
is even or odd.
4. Display the result
5. Stop
Code:
#include<stdio.h>
int main()
{
int n;
printf("Enter an integern");
scanf("%d",&n);
n%2 == 0 ? printf("Even numbern"): printf("Odd numbern");
return 0;
}
Output:
6
6 is an Even number
Program No. 6
Title:
Print the value of y for given x=2 & z=4 and analyze the output.
(A). y = x++ + ++x;
Code:
#include<stdio.h>
int main()
{
int x=2,z=4,y;
y=x++ + ++x;
printf("%d",y);
return 0;
}
Output:
6
Analyzing Output:
10. 10
As we are using y=x++ + ++x;
When this statement will be executed, at first the value of x=2, and is used and after that
the value of x
is incremented by 1, now x=3. For second x in the expression, the value of x will
preincrement and now
x=4 will be used in place of second x.
So, y=2+4;
Initial value of x=2, and is used in first x in the expression. Then x is incremented by 1,
now x=3.
For second x, the value of x will preincrement and then x=4.
y=2+4;
(b). y=++x + ++x;
Code:
#include<stdio.h>
int main()
{
int x=2,z=4,y;
y=++x + ++x;
printf("%d",y);
return 0;
}
Output:
8
Analyzing Output:
As we are using y=++x + ++x;
++x means first increase the value of x by 1 and then use it. Whereas x++ means first
use the value of x and then increment itโs value by 1.
So the value of x increases from 2 to 3 and the value of z after increases from 4 to 5.
x=3, z=5
3+5=8
(c). y= ++x + ++x + ++x;
Code:
11. 11
#include<stdio.h>
int main()
{
int x=2,z=4,y;
y=++x + ++x + ++x;
printf("%d",y);
return 0;
}
Output:
13
Analyzing output:
As we are using y=++x + ++x + ++x;
++x means first increase the value of x by 1 and then use it
(d). y=x>z;
Code:
#include<stdio.h>
int main()
{
int x=2,z=4,y;
y=x>z ;
printf("%d",y);
return 0;
}
Output:
0
Analyzing Output:
Y=x>z;
In the above statement, we are using a relational operator โ>โ. When the statement
y=x>z; will run. The
condition x>z will be evaluated. Depending on condition this will return a binary digit 0 or
1. If the
condition is true it will return 1, and if condition is false it will return 0. As x=2, z=4,
y=2>4.As 2 is not
greater than 4, so it will retrun 0 to y.
Y=0.
12. 12
(e). y=x>z?x:z;
Code:
#include<stdio.h>
int main(void)
{
int x=2,z=4,y;
y=x>z?x:z;
printf("%d",y);
return 0;
}
Output:
4
Analyzing Output:
Y=x>z?x:z;
In the above statement a conditional ternary operator โ?โ is used. The syntax of
conditional ternary
operator is condition? result 1 : result 2;
If condition will be true result 1 will be execute otherwise result 3 will be executed.
(f). y=x&z.
Code:
#include<stdio.h>
int main()
{
int x=2,z=4,y;
y=x&z;
printf("%d",y);
return 0;
}
Output:
0
Analyzing Output:
Y=x&z;
A bitwise operator AND โ&โ is used. The output of bitwise AND is 1 if the corresponding
bits of two operands is 1. If either bit of an operand is 0, the result of corresponding bit
is evaluated to 0. In the above program bitwise operator is applied on two variables x=2
and z=4.
Applying & operator= 0000
Hence the value 0 will be returned. Therefore y=0.
As 2 in binary= 0010
As 4 in binary= 0100
13. 13
(g). y= x>>2 + z<<1;
Code:
#include<stdio.h>
int main()
{
int x=2,z=4,y;
y= x>>2 + z<<1;
printf("%d",y);
return 0;
}
Output:
0
Analyzing Output:
We are using bitwise right shift and bitwise left shift operators in above program. Bit
Pattern of the data can be shifted by specified number of Positions to Right and Bit
Pattern of the data can be shifted by specified number of Positions to Left. When Data
is Shifted Right , leading zeroโs are filled with zero and When Data is Shifted Left ,
trailing zeroโs are filled with zero. Right shift Operator is Binary Operator [Bi โ two] and
left shift Operator is also Binary Operator [Bi โ two]. Binary means , Operator that
require two arguments.