codescracker
c

C Program to Calculate Area and Circumference of Circle



« Previous Program Next Program »

In this article, you will learn and get code about calculating area and circumference of a circle.

To calculate area or circumference of any circle in C programming, you have to ask from user to enter the radius of circle say r. Using this radius, to find area, use 3.14*r*r. And to find circumference, use 2*3.14*r. Here 3.14 is the value of π (Pi)

List of programs, available here in this article:

Calculate Area of Circle in C

As area of any circle can be calculated using the formula πr2. Here r represents radius of circle, and the value of π (Pi) is equal to 3.14. So the formula can be written as 3.14r2 or 3.14*r*r. Let's take a look at the program:

// Calculate area of a Circle in C
// ------codescracker.com-------

#include<stdio.h>
#include<conio.h>
int main()
{
    float rad, area;
    printf("Enter Radius Value of Circle: ");
    scanf("%f", &rad);
    area = 3.14*rad*rad;
    printf("\nArea = %0.2f", area);
    getch();
    return 0;
}

This program was written under Code::Blocks IDE. Here is the first snapshot of sample run:

c program to calculate area circumference of circle

Now supply any number as radius value of circle say 5 and press ENTER key to see the output as shown in the second snapshot of sample run:

area of circle in c

The format specifier, %0.2f is used to print the value of area upto only two decimal place.

Calculate Area of Circle using Function in C

Let's create another program that does the same job but using function as shown here:

// Calculate area of a Circle using Function in C
// ------codescracker.com-------

#include<stdio.h>
#include<conio.h>
float findArea(float);
int main()
{
    float rad, area;
    printf("Enter Radius Value of Circle: ");
    scanf("%f", &rad);
    area = findArea(rad);
    printf("\nArea = %0.2f", area);
    getch();
    return 0;
}
float findArea(float rad)
{
    return 3.14*rad*rad;
}

After compiling and executing the above program, you will get the same output as seen from the first program's output. To learn more about function, refer to Function in C tutorial.

Calculate Circumference of Circle in C

As circumference of a circle can be calculated using the formula 2πr. Here the value of π is 3.14, and r represents radius of circle. Therefore, after putting the value of π, the formula becomes 2*3.14*r. Here is the program to find circumference of circle:

// Calculate Circumference of Circle in C
// ------codescracker.com-------

#include<stdio.h>
#include<conio.h>
int main()
{
    float r, circumf;
    printf("Enter Radius Value of Circle: ");
    scanf("%f", &r);
    circumf = 2*3.14*r;
    printf("\nCircumference = %0.2f", circumf);
    getch();
    return 0;
}

Here is the sample run of above program:

calculate circumference of circle in c

Calculate Circumference of Circle using Function in C

Now let's create another program that does the same job as of previous program, but this time using function.

// Calculate Circumference of Circle using Function in C
// ------codescracker.com-------

#include<stdio.h>
#include<conio.h>
float findCircumf(float);
int main()
{
    float r, circumf;
    printf("Enter Radius Value of Circle: ");
    scanf("%f", &r);
    circumf = findCircumf(r);
    printf("\nCircumference = %0.2f", circumf);
    getch();
    return 0;
}
float findCircumf(float r)
{
    return 2*3.14*r;
}

This program produce the same output as seen from previous program's output.

Same Program in Other Languages


« Previous Program Next Program »