The program given below is a C program that is written to find out the sphere surface area and volume of a sphere. There are rules to find them and we have to use those rules in the program. Let's see the rules...
Area of a Sphere = 4πr2
Volume of a Sphere = (4/3)πr3According to the above rules, we need to know the r (radius) only to find out the area and volume. Because the value of pi (π) is constant (3.1416).
Here,
Area = ?
Volume = ?
π = 3.1416
r = ?
We have to find out the Area and the Volume, the value of π is fixed and we need to know the radius r only. By the second line #define PI 3.1416 the value of PI has defined 3.1416
By the fifth line float r, area, vol; we have to take three float type variables r, area and vol. r will be used to store the radius of the sphere that we will get from the keyboard using the seventh line scanf("%f", &r);
We have to tell the user to input a value by the sixth line printf("Enter radius of the Sphere: ");
The variables area and vol will be used to store the results.
Later, in eighth and ninth line the program will perform mathematical operations according to the rules described above. The eighth line area = 4*PI*r*r; represent the rule Area of a Sphere = 4πr2 and the ninth line vol = (4/3)*PI*r*r*r; represent the rule Volume of a Sphere = (4/3)πr3
By the tenth and eleventh line the result will be displayed. Generally we use %f to display the float type data, but I have used %.2f to show immediate two digits of the float number.
Area = ?
Volume = ?
π = 3.1416
r = ?
We have to find out the Area and the Volume, the value of π is fixed and we need to know the radius r only. By the second line #define PI 3.1416 the value of PI has defined 3.1416
By the fifth line float r, area, vol; we have to take three float type variables r, area and vol. r will be used to store the radius of the sphere that we will get from the keyboard using the seventh line scanf("%f", &r);
We have to tell the user to input a value by the sixth line printf("Enter radius of the Sphere: ");
The variables area and vol will be used to store the results.
Later, in eighth and ninth line the program will perform mathematical operations according to the rules described above. The eighth line area = 4*PI*r*r; represent the rule Area of a Sphere = 4πr2 and the ninth line vol = (4/3)*PI*r*r*r; represent the rule Volume of a Sphere = (4/3)πr3
By the tenth and eleventh line the result will be displayed. Generally we use %f to display the float type data, but I have used %.2f to show immediate two digits of the float number.
#include<stdio.h>
#define PI 3.1416
main()#define PI 3.1416
{
float r, area, vol;
printf("Enter radius of the Sphere: ");
scanf("%f", &r);
area = 4*PI*r*r;
vol = (4/3)*PI*r*r*r;
printf("\nThe area is = %.2f",area);
printf("\nThe volume is = %.2f",vol);
}
No comments:
Post a Comment