Loading your workspace…
Loading your workspace…
Read the radius of a circle and print its area and circumference.
#include <stdio.h>
#define PI 3.14159
int main(void) {
double radius, area, circumference;
printf("Enter radius:\n");
if (scanf("%lf", &radius) != 1 || radius < 0) {
printf("Error: radius must be a non-negative number\n");
return 1;
}
area = PI * radius * radius;
circumference = 2 * PI * radius;
printf("Area = %.2f\n", area);
printf("Circumference = %.2f\n", circumference);
return 0;
}
Input
7Output
Enter radius:
Area = 153.94
Circumference = 43.98Input
0Output
Enter radius:
Area = 0.00
Circumference = 0.00The area uses the square of the radius and the circumference uses the radius once, so the two formulas differ in how the radius is multiplied. PI is a macro so the constant is written in one place.
A negative radius is rejected because it has no geometric meaning, even though the formulas would still produce a number.
Edit the program and press Run. It is free and needs no sign-in.
Copy a sample and paste it into the STDIN box in the editor before you press Run.
7
Expected output
Enter radius: Area = 153.94 Circumference = 43.98
0
Expected output
Enter radius: Area = 0.00 Circumference = 0.00
3.14159, a five-decimal approximation of π.
The radius may be fractional, and a double keeps the decimal part.
A circle cannot have a negative radius, so the input is invalid.
It makes the preprocessor replace every PI with 3.14159 before compilation.
The %.2f format limits the printed precision so the answer reads cleanly.
Back to all RCU 21BCA1C1P lab programs.