Welcome to our website! We are adding new data every day, so please check back often for updates.
Posts

How to Write a C Program to Calculate the Perimeter and Area of a Circle

write a C program that can calculate the perimeter and area of a circle with a given radius. basic concepts of C programming, such as variables,

Do you want to learn how to write a C program that can calculate the perimeter and area of a circle with a given radius. In this program, I will show you how to do that in a few simple steps. You will also learn some basic concepts of C programming, such as variables, constants, input/output, and math functions.

What is a Circle?

A circle is a geometric shape that consists of all the points that are equidistant from a fixed point called the center. The distance from the center to any point on the circle is called the radius. The perimeter of a circle is the length of the boundary of the circle, and it is also known as the circumference. The area of a circle is the amount of space enclosed by the circle.

How to Calculate the Perimeter and Area of a Circle?

To calculate the perimeter and area of a circle, we need to use two mathematical formulas:

  • Perimeter = 2 * PI * radius
  • Area = PI * radius * radius

PI is a constant that represents the ratio of the circumference of a circle to its diameter. It is approximately equal to 3.14159.

How to Write a C Program to Calculate the Perimeter and Area of a Circle?

To write a C program that can calculate the perimeter and area of a circle with a given radius, we need to follow these steps:

  1. Include the header files stdio.h and math.h. These files contain the functions and constants that we need for input/output and math operations.
  2. Define a macro named PI with the value 3.14159. This will allow us to use PI as a constant in our program.
  3. Declare three variables of type double: radius, perimeter, and area. These variables will store the input and output values.
  4. Prompt the user to enter the radius of the circle using the printf function.
  5. Read the input value from the user using the scanf function and store it in the radius variable.
  6. Calculate the perimeter of the circle using the formula: perimeter = 2 * PI * radius.
  7. Calculate the area of the circle using the formula: area = PI * pow(radius, 2). The pow function is defined in math.h and it returns the value of x raised to the power of y.
  8. Display the output values using the printf function with two decimal places.
  9. Return 0 from the main function to indicate successful execution.

C Code

Here is how our C program looks like:

#include <stdio.h>

#include <math.h>

#define PI 3.14159

int main()

{

    double radius, perimeter, area;

    printf("Enter the radius of the circle: ");

    scanf("%lf", &radius);

    perimeter = 2 * PI * radius;

    area = PI * pow(radius, 2);

    printf("The perimeter of the circle is %.2f\n", perimeter);

    printf("The area of the circle is %.2f\n", area);

    return 0;

}

Output

We will see something like this on our screen:

Enter the radius of the circle: 5

The perimeter of the circle is 31.42

The area of the circle is 78.54

We can change the input value and see how our program calculates different output values.

Conclusion

In this program, you learned how to write a C program that can calculate the perimeter and area of a circle with a given radius. You also learned some basic concepts of C programming, such as variables, constants, input/output, and math functions.

I hope you found this tutorial helpful and informative. If you want to learn more about C programming, you can check out some of my other posts on this topic. Thank you for reading and happy coding!



We love your feedback and invite you to comment on our articles, exercises, examples, quizzes and others. Your feedback helps us make our content awesome and serve you better. Please leave a comment and tell us what you think. How did our content help you learn something new? Thank you for being a part of our community!

Post a Comment

For more Tech content follow us on Social media
© Ruturaj Khansole. All rights reserved. Distributed by ASThemesWorld