0007.(1 and 2)
Attempt the 2 c programming exercises below. If you are having a hard time starting, try:
- Examining the solution the exercise 2 for guidance.
- Attempt exercise 1 (no solution was provided here)
- Ideally, attempt exercise 2 without looking at the solution.
Exercise.
1. Create a program which accepts the radius for a circle/sphere and outputs the volume and maximum cross sectional area. Your program must prompt for input, and produce relevant results.
2. Given the coefficient a b and c of a quadratic equation in the form y=ax^2+bx+c output the solution to the equation when y=0
Solution to Find the roots of a Quadratic Equation:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float a,b,c,D,root1,root2;
printf("Please enter a value for a...\n");
scanf("%f",&a);
printf("Please enter a value for b...\n");
scanf("%f",&b);
printf("Please enter a value for c...\n");
scanf("%f",&c);
printf("\n");
D=(b*b)-(4*a*c);
if(D<0)
{
printf("no roots\n Exiting program...\n");
}
else if (D==0)
{
root1=(-b)/(2*a);
printf("Equation has only one root = %.2f \n Exiting program...\n ");
}
else
{
root1=((-b)-sqrt(D))/(2*a);
root2=((-b)+sqrt(D))/(2*a);
printf("Roots are %.2f and %.2f \n Exiting program...\n",root1,root2);
}
return 1;
}
Strategy for Coding using Exercise 1 as an example
© 2020 Vedesh Kungebeharry. All rights reserved.