Replace a magic number with a name.
What to build
A program that defines and uses a constant.
Requirements
- Define
PIas 3.14159, using#define. - Read a radius and print the area and the circumference, each with two decimals.
Input
One number.
Output
Two lines: area A, then circumference C.
Constraints
The radius is from 0 to 1000.
Example
Input:
1
Output:
area 3.14
circumference 6.28
Why it works
A named constant says what the number means, and changing it needs only one edit.
Examples
Example 1 · Input
1
Expected output
area 3.14 circumference 6.28
Example 2 · Input
2.5
Expected output
area 19.63 circumference 15.71
Hints
Hint 1
#define PI 3.14159 goes at the top, with no semicolon.
Hint 2
Read a double with %lf.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
#define PI 3.14159
int main(void) {
double r;
scanf("%lf", &r);
printf("area %.2f\n", PI * r * r);
printf("circumference %.2f\n", 2 * PI * r);
return 0;
}