forked from portfoliocourses/c-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circle_circumference.c
61 lines (49 loc) · 1.99 KB
/
circle_circumference.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*******************************************************************************
*
* Program: Calculate Circle Circumference With User Input
*
* Description: Program to calculate the circumference of a circle with a user
* input value for the radius in C.
*
* YouTube Lesson: https://www.youtube.com/watch?v=rHdBxp4oseA
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
// Define a preprocessor constant for PI
#define PI 3.14159265359
double calculate_circumference(double radius);
int main(void)
{
// Variable to store the radius entered by the user. We use type double for
// variables as circles can have radius and circumferences with decimal
// places.
double radius;
// Variable to store calculated circumference
double circumference;
// Prompt the user to enter the circle radius
printf("Enter Radius: ");
// Store the value entered by the user into the radius variable
scanf("%lf", &radius);
// We could calculate the circumference directly and assign the result to
// the variable circumference
// circumference = 2 * PI * radius;
// Or we could call the the calculate_circumference() function we have
// defined with the entered radius as an argument, and assign the return
// value to the circumference variable.
circumference = calculate_circumference(radius);
// Output the circumference, the .2 will ensure it is output with 2 decimal
// places of accuracy.
printf("Circumference: %.2f\n", circumference);
return 0;
}
// Returns the circumference of a circle with the radius provided as an
// argument. The advantage of using a function to calculate the circle
// circumference is that we do not have to reproduce the formula in different
// places in our code to calculate the circumference, we can instead call the
// function where the formula is defined once.
double calculate_circumference(double radius)
{
return 2 * PI * radius;
}