Solution2

advertisement
1. Write a C code that prompts the user to input tree
integer values and find the greatest value of the three
values.
Example:
Enter 3 integer vales separated by space: 10 15 20
The greatest value is: 20
Solution:
#include <stdio.h>
void main()
{
int x, y, z, max;
printf("Enter 3 integer vales separated by space:");
scanf("%d %d %d",&x,&y,&z);
max=x; if(max<y) max=y; if(max<z) max=z; printf("The max
is %d.",max);
printf("\n");
}
2. Write a program that determines a student’s grade.
The program will read three scores and determine the
grade based on the following rules:
-if the average score =90% =>grade=A
-if the average score >= 70% and <90% => grade=B
-if the average score>=50% and <70% =>grade=C
-if the average score<50% =>grade=F Solution:
#include <stdio.h>
void main()
{
float x;
float y;
float z;
float avg;
printf("Enter 3 score vales separated by space:");
scanf("%f %f %f",&x,&y,&z);
avg=(x+y+z)/3;
if(avg>=90)printf("Grade A");
else if(avg>=70) printf("Grade B");
else if(avg>=50) printf("Grade C");
else printf("Grade F");
printf("\n");
}
3. Write C code to compute the real roots of the
equation: ax2+bx+c=0.
The program will prompt the user to input the values of a, b, and c. It
then computes the real roots of the equation based on the following
rules:
-if a and b are zero=> no solution
-if a is zero=>one root (-c/b)
-if b2-4ac is negative=>no roots
-Otherwise=> two roots
The roots can be computed using the following formula:
x1=-b+(b2-4ac)1/2/2a
x=-b-(b2-4ac)1/2/2a
Solution
#include <stdio.h>
#include <math.h>
void main()
{
float a;
float b;
float c;
float delta;
printf("Enter values of a b c separated by space:");
scanf("%f %f %f",&a,&b,&c);
if(a==0 && b==0)printf("No root");
else if(a==0)printf("The equation has only one root:%.2f",-b/c);
else {
delta=b*b-4*a*c;
if(delta<0) printf("No root");
else printf("The equation has two roots:x=%.2f",-b+sqrt(b*b4*a*c)/(2*a));printf(",x1=%.2f",-b-sqrt(b*b-4*a*c)/(2*a));
}
printf("\n");
}
Download