CHAPTER 03
(Mahmud_Jamil)..BRUR// 03-09-2016
3.1 Given the values of the variables x, y and
z, write a program to rotate their
values such that x has the value of y, y has the value of z and z has the value of x.
values such that x has the value of y, y has the value of z and z has the value of x.
Solution:
#include <stdio.h>
int main()
{
int x,y,z,a;
printf("Enter three values
are:\n");
scanf("%d %d
%d",&x,&y,&z);
a=x;
x=y;
y=z;
z=a;
printf("\n\tx=%d \n\ty=%d
\n\tz=%d\n",x,y,z);
return 0;
}
3.2 Write a program that reads a floating-point
number and then displays the right
–most digit of the integral part of the number.
–most digit of the integral part of the number.
Solution:
#include<stdio.h>
int main()
{
int a,e;
float p;
printf("Enter the value of p\n");
scanf("%f",&p);
a=(int)p;
printf("%d\n",a);
e=a%10;
if(a>10)
printf("%d\n",e);
return 0;
}
3.3 Modify the above program to display the two
right-most digits of the integral part of the number.
Solution:
#include<stdio.h>
int main()
{
int a,e;
float p;
printf("Enter the value of p\n");
scanf("%f",&p);
a=(int)p;
printf("%d\n",a);
e=a%10;
if(a>10)
printf("%d\n",e);
return 0;
}
3.4 Write a program that will obtain the length
and width of a rectangle from the
user and compute its area and perimeter.
user and compute its area and perimeter.
Solution:
#include <stdio.h>
int main()
{
int len,wid,area,peri;
printf("Enter the Length
of Rectangle:\n");
scanf("%d",&len);
printf("Enter the Width of
Rectangle:\n");
scanf("%d",&wid);
area=len*wid;
peri=2*(len+wid);
printf("\nThe Area of Rectangle:
%d\n",area);
printf("The Perimeter of Rectangle:
%d\n",peri);
return 0;
}
3.5 Given an integer number, write a program
that displays the number as follows:
First line : all digits
Second line : all except first digit
Third line : all except first two digits
……
Last line : The last digit.
First line : all digits
Second line : all except first digit
Third line : all except first two digits
……
Last line : The last digit.
For example, the number 5678
will be displayed as:
5 6 7 8
6 7 8
7 8
8
Solution:
#include <stdio.h>
int main()
{
int x,a,b,c;
printf("Enter a four digit number:
");
scanf("%d",&x);
a=x%1000;
b=a%100;
c=b%10;
printf("%d\n",x);
printf("%d\n",a);
printf("%d\n",b);
printf("%d\n",c);
return 0;
}
3.6 The straight line method of computing the
yearly depreciation of the value of an
item is given by:
item is given by:
Depreciation = Purchase
price- Salvage Value/Years of Service
Write a Program to determine the
salvage value of an item when the purchase price, years
of service and the annual depreciation are given.
of service and the annual depreciation are given.
Solution:
#include<stdio.h>
int main()
{
float Dep,Year_Ser,Pur_Price,Sal_value;
printf("Enter Deperaciation, Year of
Service, Purchase price\n");
scanf("%f %f
%f",&Dep,&Year_Ser,&Pur_Price);
Sal_value=Pur_Price-(Dep*Year_Ser);
printf("The salvage value of an item =
%.2f\n",Sal_value);
return 0;
}
3.7 Write a program that will read a real no. from
the keyboard and print the
output in one line:
Smallest integer the given largest integer
not less than number not greater than the
output in one line:
Smallest integer the given largest integer
not less than number not greater than the
the number the number
Solution:
#include<stdio.h>
int main()
{
int SN,LN;
float RN;
printf("Enter the real
no.");
scanf("%f",&RN);
SN=RN;
LN=RN;
printf("\n Smallest integer not");
printf(" The given no. ");
printf(" Largest integer not \n");
printf(" less than the number ");
printf(" greater than the
no.\n");
printf(" %d",SN);
printf(" %.2f",RN);
printf(" %d\n",LN);
return 0;
}
3.8 The total distance travelled by
a vehicle in t seconds is given by
distance=
ut+(at2)/2
Where u is the initial velocity( meter per second),a is the acceleration (meter per second2). Write
Where u is the initial velocity( meter per second),a is the acceleration (meter per second2). Write
a program to evaluate the distance
travelled at intrevales of time, give the value of u and a. the
program should provide the
flexibility to the user to select his own time intervals and repeat the
calculation for different value of u
and a.
Solution:
#include<stdio.h>
int main()
{
int u,t,a;
float Dis;
printf("Enter the
value of u,a and t\n");
scanf("%d %d
%d",&u,&a,&t);
Dis=(u*t)+(a*(t*t))/2;
printf("The
distance is : %.2f \n",Dis);
return 0;
}
3.9 In inventory
management ,the Economic Order Quantity for a single item is given by
EOQ=sqrt { (
2*demand rate*setup rate )/(holding cost per item per unit time )}
And the Time Between Orders
TBO=sqrt{( 2*
setup cost )/(demand rate*holding cost per item per unit time ) }
Solution:
#include<stdio.h>
#include<math.h>
int main()
{
float Dr,Sc,Hc,TBO,EOQ;
printf("Enter Demand Rate \n");
scanf("%f\n",&Dr);
printf("Enter Setup Cost \n");
scanf("%f\n",&Sc);
printf("Enter Holding Cost \n");
scanf("%f\n",&Hc);
EOQ=sqrt((2*Dr*Sc)/Hc);
TBO=sqrt((2*Sc)/(Dr*Hc));
printf("\tThe Economic Order Quantity
is : %f\n",EOQ);
printf("\tThe time Between Order is :
%f\n",TBO);
return 0;
}
3.10 For a certain electrical circuit with an
inductance L and resistance R,the
damped natural frequency is given by frequency is
damped natural frequency is given by frequency is
Frequency=
sqrt((1/LC)-(R*R)/(4*C*C))
It is desired to study the
variation of this frequency with c,write a program to
calculate the frequency for diff values of c starting from .01 to.10(interval is
.01).
calculate the frequency for diff values of c starting from .01 to.10(interval is
.01).
Solution:
#include<stdio.h>
#include<math.h>
int
main()
{
float L,R,C,x,a,b,c,d,F;
printf("Enter the value of L,R,C\n");
scanf("%f %f %f",&L,&R,&C);
c=(1/L*C);
d=(R*R/4*C*C);
a=c-d;
F=sqrt(a);
printf("%f",F);
return
0;
}
3.11 Write a program to read a four digit integer
and print the sum of its digits.
Hint: Use / and % operators.
Hint: Use / and % operators.
Solution:
#include<stdio.h>
int main()
{
int Num,Sum,Sum1,Sum2,Sum3,Sum4;
Sum1=Sum2=Sum3=Sum4=0;
printf("Enter a Four Digits
Number\n",&Num);
scanf("%d",&Num);
Sum1=Num%10;
Num=Num/10;
Sum2=Num%10;
Num=Num/10;
Sum3=Num%10;
Num=Num/10;
Sum4=Num%10;
Sum=Sum1+Sum2+Sum3+Sum4;
printf("\nSum of Digits are :
%d\n",Sum);
return 0;
}
3.12 Write a program to print the size of various
data types in C.
Solution:
#include<stdio.h>
int main()
{
printf("Size of Integer Data Type : %d
\n",sizeof(int));
printf("Size of Character Data Type : %d
\n",sizeof(char));
printf("Size of Float Data Type : %d
\n",sizeof(float));
printf("Size of Double Data Type : %d
\n",sizeof(double));
return 0;
}
3.13 Given three values, write a program to read
three values from keyboard and print out the largest
of them without using if statement.
of them without using if statement.
Solution:
#include<stdio.h>
int main()
{
int x,y,z;
printf("Enter Three
Numbers:\n");
scanf("%d %d %d",&x,&y,&z);
((x>y)&&(x>z))?printf("Largest is x : %d",x):
((y>x)&&(y>z))?printf("Largest is y : %d",y):
printf("Largest is z :-- %d",z);
return 0;
}
3.14 Write a program to read two integer values m
and n and to decide and print whether m is
Multiple of n.
Solution:
#include<stdio.h>
int main()
{
int m,n,x;
printf("Enter Two Numbers:\n");
scanf("%d %d",&m,&n);
x=m%n;
(x==0)?printf("m is multiple of
n\n"):printf("m is not multiple of n\n");
return 0;
}
3.15 Write a program to read three values using
scanf statement and print the following
results:
a) Sum of the values
b) Average of the three values
c) Largest of three values
d) Smallest of three values
results:
a) Sum of the values
b) Average of the three values
c) Largest of three values
d) Smallest of three values
Solution:
3.16 The cost of one type of mobile service is
Rs. 250 plus Rs. 1.25 for each call made over and above
100 calls. Write a program to read customer codes and calls made and print the bill for each
customer.
100 calls. Write a program to read customer codes and calls made and print the bill for each
customer.
Solution:
3.17 Write a program to
print a table of sin and cos functions for the interval 0 180 degrees in increments of 15 as shown below:
-----------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
x(degees)
sin(x)
cos(x)
O ……. …….
15 .…… …….
….. ……. ……
O ……. …….
15 .…… …….
….. ……. ……
180
……. …….
Solution:
3.18 Write a program to compute the values of
square-roots and squares of the number 0 to 100 in steps 10 print the output in
a tabular form as shown below.-----------------------------------------------------------------------------------------------
number
Square-root
square
0 0 0
100 10 10000
100 10 10000
Solution:
3.19
Write a program that determines whether
a given integer is odd or even and displays the number and description on the same line.
Solution:
3.20
Write a program to illustrate the use of
cast operator in a real life situation.
Solution: