Wednesday, April 30, 2025

C Program - Multiple Circles inside C Array - Circumference and Area Calculator

This program calculates the area and circumference of a circle, given its radius. It prompts the user to enter the radius of the circle, reads in the value using scanf, and then calculates the area and circumference using the formulas "PI * radius * radius" and "2 * PI * radius", respectively, where PI is a constant value defined using the #define preprocessor directive.

After calculating the area and circumference, the program prints the values to the console using printf. Finally, the program returns 0 to indicate successful program execution.

In short, this program is a simple calculator for the area and circumference of a circle, given its radius. 

//calculate multiple circle area and circumference using c array

#include <stdio.h>

#define PI 3.14159
#define SEPARATOR "----------------------------------------- \n"

int main()
{

    float area, circumference;
    int x; // for for loop

    int radiuses[] = {20, 34, 50, 14}; //circles

    int number_of_circles = sizeof(radiuses)/sizeof(radiuses[0]);

    for (x = 0; x < number_of_circles; x++)
    {
        printf("\nCalculation for circle %d with radius %d: \n", x, radiuses[x]);

        area = PI * radiuses[x] * radiuses[x];
        printf("Area: %f \n", area);

        circumference = 2 * PI * radiuses[x];
        printf("Circumference: %f \n", circumference);
        printf(SEPARATOR);
    }

    return 0;

}

Result: 


Calculation for circle 0 with radius 20:
Area: 1256.635986
Circumference: 125.663597
-----------------------------------------

Calculation for circle 1 with radius 34:
Area: 3631.677979
Circumference: 213.628113
-----------------------------------------

Calculation for circle 2 with radius 50:
Area: 7853.975098
Circumference: 314.158997
-----------------------------------------

Calculation for circle 3 with radius 14:
Area: 615.751648
Circumference: 87.964523
-----------------------------------------

Process returned 0 (0x0)   execution time : 0.062 s
Press any key to continue.

Here's a line-by-line explanation of the code: 

#include <stdio.h>

This line includes the standard input/output library, which is needed for functions like printf and scanf. 

#define PI 3.14159
#define SEPARATOR "----------------------------------------- \n"

These lines define two constant values: "PI" and "SEPARATOR". PI is set equal to 3.14159, and SEPARATOR is set equal to a string of dashes that will be used to separate the calculations for each circle. 

int main()
{
    float area, circumference;
    int x; // for for loop

    int radiuses[] = {20, 34, 50, 14}; //circles

    int number_of_circles = sizeof(radiuses)/sizeof(radiuses[0]);

This is the "main" function of the program. It declares two float variables: "area" and "circumference". It also declares an integer variable "x" to use in a for loop. It then declares an integer array "radiuses" containing the radii of four circles.

The variable "number_of_circles" is set to the number of elements in the "radiuses" array using the sizeof operator. 

    for (x = 0; x < number_of_circles; x++)
    {
        printf("\nCalculation for circle %d with radius %d: \n", x, radiuses[x]);

        area = PI * radiuses[x] * radiuses[x];
        printf("Area: %f \n", area);

        circumference = 2 * PI * radiuses[x];
        printf("Circumference: %f \n", circumference);
        printf(SEPARATOR);
    }

This is a for loop that iterates through the "radiuses" array for each circle. It first prints a message using printf indicating which circle is being calculated, along with its radius. It then calculates the area and circumference of the circle using the formulas "PI * radius * radius" and "2 * PI * radius", respectively, and stores them in the "area" and "circumference" variables.

It then prints the calculated values of the area and circumference to the console using printf, along with the SEPARATOR string to separate the calculations for each circle. 

    return 0;
}

Finally, the main function returns 0 to indicate successful program execution.

In summary, this code calculates the area and circumference of multiple circles based on an array of radii using a for loop. It first defines constant values for PI and a separator string, then reads in the radii from an array and calculates the area and circumference of each circle using formulas. It then prints the calculated values of the area and circumference for each circle to the console, along with a separator string to make it clear which circle the calculations correspond to.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...