C program to find diameter, circumference and area of circle

Write a C program to input radius of a circle from user and find diameter, circumference and area of circle. How to calculate diameter, circumference and area of a circle whose radius is given by user in C programming.

Also check how to write this program into a function - C program to find diameter, circumference and area of circle using functions.

Example:
Input radius: 10
Output diameter = 20 units
Output circumference = 62.79 units
Output area = 314 sq. units

Required knowledge

Fundamentals of C, Data types, Talking user input in C, Properties of circle

Diameter of circle

Diameter of a circle can be expressed by formula: 2 * radius.

Circumference of circle

Circumference of a circle can be expressed by formula: 2 * π * radius.

Area of circle

Area of a circle can be expressed by formula: π * r2.

Program to find diameter, circumference and area of circle

/**
 * C program to calculate diameter, circumference and area of circle
 */

#include <stdio.h>

int main()
{
    float radius, diameter, circumference, area;
    
    /*
     * Reads radius of circle from user
     */
    printf("Enter radius of circle: ");
    scanf("%f", &radius);

    /*
     * Calculates diameter, circumference and area of circle
     */
    diameter = 2 * radius;
    circumference = 2 * 3.14 * radius;
    area = 3.14 * (radius * radius);

    /*
     * Prints all results
     */
    printf("\nDiameter of circle = %.2f units.\n", diameter);
    printf("Circumference of circle = %.2f units.\n", circumference);
    printf("Area of circle = %.2f sq. units.", area);

    return 0;
} 

Note : Since the above program contains a constant value PI it is always advisable to use a constant variable to represent such types of constants. You can write the above program using a predefined constant for PI present in math.h header file. math.h header file defines a predefined constant for PI and is named as M_PI.

Program to find diameter, circumference and area of circle

/**
 * C program to calculate diameter, circumference and area of circle
 */

#include <stdio.h>
#include <math.h> //Used for PI referred as M_PI 

int main()
{
    float radius, diameter, circumference, area;
    
    /*
     * Reads radius of circle from user
     */
    printf("Enter radius of circle: ");
    scanf("%f", &radius);

    /*
     * Calculates diameter, circumference and area of circle
     */
    diameter = 2 * radius;
    circumference = 2 * M_PI * radius;
    area = M_PI * (radius * radius);

    /*
     * Prints all results
     */
    printf("\nDiameter of circle = %.2f units.\n", diameter);
    printf("Circumference of circle = %.2f units.\n", circumference);
    printf("Area of circle = %.2f sq. units.", area);

    return 0;
}


Output
Enter radius of circle: 10

Diameter of circle = 20.00 units.
Circumference of circle = 62.79 units.
Area of circle = 314.00 sq. units.

Note: Here %.2f is used to print the fractional value up to two decimal places. You can also use %f to print up to default 6 decimal places.

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment