#include <stdio.h>
#define PI 3.14159
int main() {
float radius, circumference;
// Prompt the user to enter the radius of the circle
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
// Calculate the circumference
circumference = 2 * PI * radius;
// Print the result
printf("The circumference of the circle is: %.2f\n", circumference);
return 0;
}
In this version, the calculation of the circumference is directly performed within the main function, without the need for a separate function.
The program follows the same steps as before:
The program includes the necessary header file stdio.h for input/output functionality.
The constant PI is defined with the value of 3.14159, representing the mathematical constant π.
The main function declares the variables radius and circumference of type float, which will store the user input and the calculated circumference, respectively.
The program prompts the user to enter the radius of the circle using the printf function.
The scanf function reads the user's input from the keyboard and stores it in the radius variable.
The program calculates the circumference using the formula 2 * PI * radius and assigns the result to the circumference variable.
Finally, the program uses the printf function to display the calculated circumference on the screen, formatted with two decimal places.
Post Views: 128