CHAPTER 02
(Mahmud_Jamil)..BRUR// 02-09-2016
2.1 Write a program to determine and print the
sum of following harmonic series for given
value of n:
1+1/2+1/3+…….+1/n.
The value of n should be given interactively through the terminal.
value of n:
1+1/2+1/3+…….+1/n.
The value of n should be given interactively through the terminal.
Solution:
#include <stdio.h>
int
main()
{
int n;
float sum,i;
printf("Enter the value of n:\n");
scanf("%d",&n);
sum=0;
for(i=1;i<=n;i++){
sum=sum+(1/i);
}
printf("\n\tSum
= %.2f\n",sum);
return 0;
}
2.2 Write a program to read the price of an item
in decimal form and print it in paise
(like paise 2563).
Solution:
#include<stdio.h>
int main()
{
float
Price;
int
Paise;
printf("Enter
the price of Item in decimal\n");
scanf("%f",&Price);
Paise=Price*100;
printf("\nPrice in
Paise is %d \n",Paise);
return 0;
}
2.3 Write a program that prints the even no. from
1 to 100.
Solution:
#include<stdio.h>
int main()
{
int
i=2;
printf("The even numbers from 1 to 100
are:\n\n");
while(i<=100) {
printf(" %d" ,i);
i=i+2;
}
printf("\n");
return 0;
}
2.4 Write a program that requests two float type
numbers from the user and then divides
the first number by the second and display the result along with the numbers.
the first number by the second and display the result along with the numbers.
Solution:
#include<stdio.h>
int main()
{
float x,y,Division;
printf("Enter two values are:\n");
scanf("%f %f", &x,&y);
Division=x/y;
printf("\nx=%.2f y=%.2f
\n\nDivision=%.2f\n",x,y,Division);
return 0;
}
2.5 The price of one kg of rice is Rs. 16.75 and
one kg of sugar is Rs.15. Write a program
to get these values from the user and display the prices as follows:
*** LIST OF ITEMS***
Item Price
Rice Rs. 16.75
Sugar Rs. 15.00
to get these values from the user and display the prices as follows:
*** LIST OF ITEMS***
Item Price
Rice Rs. 16.75
Sugar Rs. 15.00
Solution:
#include<stdio.h>
int main()
{
float RicePrice,SugarPrice;
printf("Enter the price of
Rice:\n");
scanf("%f",&RicePrice);
printf("Enter the
price of Sugar:\n");
scanf("%f",&SugarPrice);
printf("***LIST OF
ITEMS***\n");
printf("Item Price\n");
printf("Rice Rs. %.2f\n",RicePrice);
printf("Sugar Rs. %.2f\n",SugarPrice);
return 0;
}
2.6 Write
program to count and print the number of negative and positive numbers in a
given set of
numbers. Test your program with a suitable
set of numbers. Use scanf to read the numbers.
Reading should be terminated when the
value 0 is encountered.
Solution:
#include <stdio.h>
int main()
{
int i,m,sum=0,add=0;
float n;
scanf("%d",&m);
printf("Enter %d numbers are\n",m);
for(i=1; i<=m; i++){
scanf("%f",&n);
if(n>0){
sum=sum+1;
}
if(n<0){
add=add+1;
}
}
printf("\n\tTotal %d Numbers are
positive\n",sum);
printf("\tTotal %d Numbers are
negative\n\n",add);
return 0;
}
2.7 Write a program to do the following:
a) Declare x and y as integer variables and z as a short integer variable.
b) Assign two 6 digit numbers to x and y.
c) Assign the sum of x and y to z.
d) Output the value of x, y and z.
Comment on the output.
a) Declare x and y as integer variables and z as a short integer variable.
b) Assign two 6 digit numbers to x and y.
c) Assign the sum of x and y to z.
d) Output the value of x, y and z.
Comment on the output.
Solution:
#include<stdio.h>
int main()
{
int x,y;
short int sum;
x=123456789123456;
y=987654321123456;
sum=x+y;
printf("x=%d \ny=%d
\nsum=%d\n",x,y,sum);
return 0;
/*Comment:
x,y that is integer data type value but sum is short integer data
type
value.Cause
of
we have output result gerbage value.If we
are taken sum value intger
when we
get required value*/
}
2.8 Write a program to read two floating point
numbers using a scanf statement, assign their sum
to an integer variable and then output the values of all the three variables.
to an integer variable and then output the values of all the three variables.
Solution:
#include<stdio.h>
int main()
{
float x,y;
int sum;
printf("Enter the value x,y:\n");
scanf("%f %f",&x,&y);
sum=x+y;
printf("x=%.1f \ny=%.1f
\nsum=%d\n",x,y,sum);
return 0;
}
2.9 Write a program to illustrate the use of
typedef declaration in a program.
Solution:
#include<stdio.h>
int main()
{
typedef int NewData;
NewData x;
printf("Enter
Value\n");
scanf("%d",&x);
printf("This Value
is:\nx=%d\n",x);
return
0;
}
2.10 Write a program to illustrate the use of
symbolic constants in a real life application.
Solution:
#include<stdio.h>
int main()
{
float Area,pi=3.1416;
int Rad;
printf("Enter the Radius\n");
scanf("%d",&Rad);
Area=pi*Rad*Rad;
printf("Area of Circle
is: %.2f\n",Area);
return 0;
}
>>>>>END<<<<<