You are on page 1of 19

Problem Solving and C Programming

C PREAMBLE PROBLEM
SHEET - 3
HARINI A (D20PC32)
10/26/2020
Q1) To convert pounds and ounces to grams and kilograms.

#include <stdio.h>

int main()
{
float lb,oz,kg,g;
printf("Enter Weight in Pounds and ounces : ");
scanf("%f %f",&lb,&oz);
g=lb*453.592+oz*28.349527;
kg=g/1000;
printf("\nThe weight is %.2f kilograms(kg) or %.2f
grams(g)\n",kg,g);
}

OUTPUT:-
Q2) To calculate of times the heart beat in a lifetime of 78 years.

#include <stdio.h>

int main()
{
float days,n,m;
days=78*365.35;
m=days*24*60*75;
printf("Number of times the heart beat in a lifetime of 78
years is %.2f \n",m);
}

OUTPUT:-
Q3) To calculate volume of a sphere.

#include <stdio.h>
pi=3.14;

int main()
{
float d,v,vol,area;
printf("Enter the diameter of the pipe: ");
scanf("%f",&d);
printf("Enter the velocity of water : ");
scanf("%f",&v);
float r=d/2;
area=pi*r*r;
vol=area*v;
printf("Diameter : %.2f ft \n",d);
printf("Velocity : %.2f ft/s \n",v);
printf("Volume : %.2f ft^3\n",vol);
return 0;
}

OUTPUT:-
Q4) To calculate distance between two points.

#include <stdio.h>
#include <math.h>

int main()
{
float x1,y1,x2,y2;
printf("Enter the coordinates of point P : ");
scanf("%f %f",&x1,&y1);
printf("Enter the coordinates of point Q : ");
scanf("%f %f",&x2,&y2);
float d=sqrt(pow(x2-x1,2)+pow(y2-y1,2));
printf("The length of the line PQ is : %.2f",d);
return 0;
}

OUTPUT:-
Q5) To find volume of a sphere.

#include <stdio.h>
pi=3.14;

int main()
{
float d,r,vol;
printf("Enter the diameter of the sphere (in inches ) : ");
scanf("%f",&d);
r=d/2;
vol=(4/3)*pi*r*r;
printf("The diameter of the sphere is : %.2f inches \n",d);
printf("The volume of the sphere is : %.2f inches^3\n",vol);
return 0;
}

OUTPUT:-
Q6) To calculate Total sales and commission.

#include <stdio.h>

int main()
{
int id;
float n,cost,com,t;
printf("COOL YOUR HOME\n\n");
printf("Enter Your ID : ");
scanf("%f",&id);
printf("Enter Number of items sold : ");
scanf("%f",&n);
printf("Enter Coat per item : ");
scanf("%f",&cost);
t=cost*n;
com=t*0.12;
printf("Total Sales : Rs %.2f\n",t);
printf("Commission : Rs %.2f",com);
return 0;
}

OUTPUT:-
Q7) To convert the given centimetres into the equivalent in kilo meters, meters and
centimetres.

#include <stdio.h>
int main()
{
int cm,km,m;
printf("Enter the length in cm : ");
scanf("%d",&cm);
km=cm/100000;
int km2=cm%100000;
m=km2/100;
int cm2=km2%100;
printf("Km : %d \nM : %d\nCm :%d ",km,m,cm2);
return 0;
}

OUTPUT:-
Q8) To calculate Compressed stress.

#include <stdio.h>
int main()
{
float f,d,p,area,r;
printf("Enter Diameter (in inches): ");
scanf("%f",&d);
r=d/2;
area=3.14*r*r;
printf("Enter Compression Load (in pounds) : ");
scanf("%f",&p);
f=p/area;
printf("The diameter is : %.2f inches \n");
printf("Compressed Stress : %f pounds/inches^2",d,f);
return 0;
}

OUTPUT:-
Q9) To express a given number of seconds in terms of hours, minutes and seconds

#include <stdio.h>
int main()
{
int s,hr,day,min;
printf("Enter time in seconds : ");
scanf("%d",&s);
day=s/(24*3600);
int rem1=s%(24*3600);
hr=rem1/3600;
int rem2=rem1%3600;
min=rem2/60;
int s2=rem2%60;
printf("%d seconds is %d days %d hours %d minutes and %d
seconds",s,day,hr,min,s2);
}

OUTPUT:-
Q10) To find coordinates of a point.

#include <stdio.h>
#include <math.h>

int main()
{
float r,x,y,t;
printf("Enter distance of the point from the origin : ");
scanf("%f",&r);
printf("Enter the angle made by the line with x axis (in radians): ");
scanf("%f",&t);
x=r*cos(t);
y=r*sin(t);
printf("The coordinates of the point is (x,y) : (%.1f,%.1f) ",x,y);

OUTPUT:-
Q11) To calculate Voltage gain.

#include <stdio.h>
#include <math.h>

int main()
{
float f=120,v,n=4,x,y;
x=sqrt(23*23+0.5*(f*f));
v=pow(275/x,n);
printf("The voltage gain is : %.2f V ",v);
return 0;
}

OUTPUT:-
Q12) To calculate world population in the year 2012.

#include <stdio.h>
#include <math.h>

int main()
{
float year=2012,p,x;
x=0.02*(year-2000);
p=6.0*(pow(2.718281828,x));
printf("The worldwide population in the year 2012 is : %.2f\n
billion ",p);
return 0;
}

OUTPUT:-
Q13) To calculate absolute value of x-y.

#include <stdio.h>
int main()
{
int x,y;
printf("Enter the value of x and y : ");
scanf("%d %d",&x,&y);
if ((x-y)>0){
printf("The absolute value of x-y is : %d",x-y);
}
else{
printf("The absolute value of x-y is : %d",y-x);
}
}

OUTPUT:-
Q14) To evaluate the fuel consumption of a car..

#include <stdio.h>

int main()
{
float m1,m2,g1,g2,tm,tg,km,l;
float const cf1=1.609,cf2=3.785;
printf("Enter Millage at the start : ");
scanf("%f",&m1);
printf("Enter Millage at the end : ");
scanf("%f",&m2);
printf("Enter fuel at the start in galleons: ");
scanf("%f",&g1);
printf("Enter fuel at the end in galleons : ");
scanf("%f",&g2);
tm=m2-m1;
tg=g1-g2;
printf("\nTotal miles traveled = %.2f miles \n",tm);
printf("Total fuel consumed = %.2f galleons \n",tg);
printf("Miles per Galleons : %f\n\n",tm/tg);
km=tm*cf1;
l=tg*cf2;
printf("Total Kilometer traveled = %.2f\n km",km);
printf("Total fuel consumed in liters = %.2f liters \n",l);
printf("Liters of fuel per 100 km : %.2f\n\n",(l/km)*100);
return 0;

OUTPUT:-
Q15) To form Pythagorean triplet .

#include <stdio.h>
int main()
{
int m,n,p;
printf("Enter 2 positive integers : ");
scanf("%d %d",&m,&n);
int s1=(m*m)-(n*n);
int s2=2*m*n;
int hyp = (m*m)+(n*n);
printf("The Pythagorean triplet formed when m=%d and n=%d is :
(%d , %d, %d)",m,n,s1,s2,hyp);

OUTPUT:-
Q17) To calculate world population in the year 2012.

#include <stdio.h>
#include <math.h>

int main()
{
float a,b,c,d;
float x1,x2;
printf("Enter a,b,c of a quadratic equation : ");
scanf("%f %f %f",&a,&b,&c);
d=sqrt(b*b-(4*a*c));
x1=((-b)+d)/2*a;
x2=((-b)-d)/2*a;
printf("The roots of the quadratic equation is : %.2f,%.2f \n",x1,x2);
return 0;

OUTPUT:-
Q18) To calculate world population in the year 2012.

#include <stdio.h>

int main ()
{
float ib,r,bal;
printf("Enter Initial Balance : ");
scanf("%f",&ib);
printf("Enter Annual Interest rate in percentage : ");
scanf("%f",&r);
r=r/100;
int i;
for (i=1;i<4;i++){
bal=ib*pow(1+(r/12),i);
printf("\nThe Balance at the end of Month %d : %.2f
\n",i,bal);
}
return 0;

OUTPUT:-
Q19) To Resistance of a circuit.

#include <stdio.h>

int main()
{
float r1,r2,r3,x,req;
printf("Enter Resistance of 3 Resistors : ");
scanf("%f %f %f",&r1,&r2,&r3);
x=(r2*r3)/(r2+r3);
req=r1+x;
printf("The total resistance is : %.2f ohms",req);
return 0;
}

OUTPUT:-

You might also like