Question:
Write a program that converts degree Fahrenheit to degrees Celcius using the following formula:
degree C = (F-32)/1.8 (ref: Programming in C By Stephen G. Kochan)
The C program
#include
int main()
{
float degF;
float degC;
printf(“enter the temperature in Fahrenheit= “);
scanf(“%f”,°F);
degC = (degF – 32.0) / 1.8;
printf(“%.2f deg Fahrenheit = %.2f deg Celsius\n”, degF, degC);
return 0;
}
Question:
Writes a program that computes the roots of a quadratic equation given a=1.2, b=2.3 and c=-3.6
Coding:
#include
#include
int main()
{
float a,b,c,root1,root2;
printf(“this program solves for eq ax^2+bx+c=0”);
printf(“\nplease input the value for a,b and c, a must not be zero\n”);
scanf(“%f %f %f”,&a,&b,&c);
printf(“a=%f,b=%f,c=%f \n”,a,b,c );
root1=-b+sqrt((pow(b,2)-4*a*c)/(2*a));
root2=-b-sqrt((pow(b,2)-4*a*c)/(2*a));
printf(“root1=%3f,root2=%3f”,root1,root2);
return 0;
}
C-Program
#include<math.h>
#include<math.h>
int main(int argc,char*argv[])
{
float a,b,c;
double root1,root2,determinant,root3,r4,i4;
printf(“this program solves for eq ax^2+bx+c=0”);
printf(“\nplease input the value for a,b and c, a must not be zero\n”);
scanf(“%f %f %f”,&a,&b,&c);
printf(“a=%f,b=%f,c=%f\n”,a,b,c);
determinant=b*b-4*a*c;
if(determinant>0)
{
root1=-b+sqrt(determinant/(2*a));
root2=-b-sqrt(determinant/(2*a));
printf(“root1=%3f,root2=%3f”,root1,root2);
}
else if(determinant==0)
{
root3=-b/(2*a);
printf(“root1=root2=%.3f”,root3);
}
else
{
r4=-b/(2*a);
i4=sqrt(-determinant)/(2*a);
printf(“root1=%.2lf+i%.2lf and root 2=%.2lf-i%.2lf”,r4,i4,r4,i4);
}
}