Write C++ program to Find Diameter, Circumference and Area of Circle Using Function

Program Overview

The program calculates the diameter, circumference, and area of a circle using functions. It takes the radius as input from the user and computes the required values using mathematical formulas.

 

circle_operations.cpp

#include <iostream>  
#include <cmath>  

// Function to calculate and return the diameter of a circle  
double calculateDiameter(double radius) {  
    return 2 * radius;  
}  

// Function to calculate and return the circumference of a circle  
double calculateCircumference(double radius) {  
    return 2 * M_PI * radius;  
}  

// Function to calculate and return the area of a circle  
double calculateArea(double radius) {  
    return M_PI * radius * radius;  
}  

int main() {  
    // Variable to store the radius of the circle  
    double radius;  

    // Prompt the user to enter the radius  
    std::cout << "Enter the radius of the circle: ";  
    std::cin >> radius;  

    // Display the calculated diameter  
    std::cout << "Diameter of the circle: " << calculateDiameter(radius) << std::endl;  

    // Display the calculated circumference  
    std::cout << "Circumference of the circle: " << calculateCircumference(radius) << std::endl;  

    // Display the calculated area  
    std::cout << "Area of the circle: " << calculateArea(radius) << std::endl;  

    return 0;  
}  
 File Tree 
circle_operations/
└── circle_operations.cpp
 

Code Explanation

1. Header Files

#include <iostream>
#include <cmath>
  • #include <iostream>: Used for input and output operations (e.g., std::cin, std::cout).
  • #include <cmath>: Provides mathematical constants and functions like M_PI for π (pi).

2. Function Definitions

  1. Calculate Diameter
double calculateDiameter(double radius) {  
    return 2 * radius;  
}

Purpose: This function computes the diameter of the circle using the formula: Diameter=2×Radius\text{Diameter} = 2 \times \text{Radius}Diameter=2×RadiusParameters:

  • radius: The radius of the circle (passed as a parameter).

Returns: The diameter of the circle.

2. Calculate Circumference

double calculateCircumference(double radius) {  
    return 2 * M_PI * radius;  
}

Purpose: Computes the circumference using the formula: Circumference=2×π×Radius\text{Circumference} = 2 \times \pi \times \text{Radius}Circumference=2×π×RadiusParameters:

  • radius: The radius of the circle.
double calculateArea(double radius) {  
    return M_PI * radius * radius;  
}

Here is a detailed explanation of the provided C++ program:

Code Explanation

1. Header Files

cppCopy code#include <iostream>
#include <cmath>
  • #include <iostream>: Used for input and output operations (e.g., std::cin, std::cout).
  • #include <cmath>: Provides mathematical constants and functions like M_PI for π (pi).

2. Function Definitions

  1. Calculate Diameter
cppCopy codedouble calculateDiameter(double radius) {  
    return 2 * radius;  
}
  • Purpose: This function computes the diameter of the circle using the formula: Diameter=2×Radius\text{Diameter} = 2 \times \text{Radius}Diameter=2×Radius
  • Parameters:
    • radius: The radius of the circle (passed as a parameter).
  • Returns: The diameter of the circle.
  1. Calculate Circumference
cppCopy codedouble calculateCircumference(double radius) {  
    return 2 * M_PI * radius;  
}
  • Purpose: Computes the circumference using the formula: Circumference=2×π×Radius\text{Circumference} = 2 \times \pi \times \text{Radius}Circumference=2×π×Radius
  • Parameters:
    • radius: The radius of the circle.
  • Returns: The circumference of the circle.
  1. Calculate Area
cppCopy codedouble calculateArea(double radius) {  
    return M_PI * radius * radius;  
}
  • Purpose: Computes the area of the circle using the formula: Area=π×(Radius)2\text{Area} = \pi \times (\text{Radius})^2Area=π×(Radius)2
  • Parameters:
    • radius: The radius of the circle.
  • Returns: The area of the circle.

3. main() Function

  1. Variable Declaration
double radius;

Declares a variable radius to store the user input.

2. User Input

std::cout << "Enter the radius of the circle: ";  
std::cin >> radius;

Prompts the user to enter the radius of the circle and read it into the radius variable.

3. Function Calls and Output

std::cout << "Diameter of the circle: " << calculateDiameter(radius) << std::endl;

Calls calculateDiameter(radius) and prints the result.

std::cout << "Area of the circle: " << calculateArea(radius) << std::endl;

Calls calculateArea(radius) and prints the result.

4. Program Termination

return 0;

Indicates successful termination of the program.

How the Program Works

  1. The user is prompted to enter the radius of the circle.
  2. The program computes the diameter, circumference, and area using the respective functions.
  3. It prints the calculated values on the screen.

Here is a detailed explanation of the provided C++ program:


Program Overview

The program calculates the diameter, circumference, and area of a circle using functions. It takes the radius as input from the user and computes the required values using mathematical formulas.


Code Explanation

1. Header Files

cppCopy code#include <iostream>
#include <cmath>
  • #include <iostream>: Used for input and output operations (e.g., std::cin, std::cout).
  • #include <cmath>: Provides mathematical constants and functions like M_PI for π (pi).

2. Function Definitions

  1. Calculate Diameter
cppCopy codedouble calculateDiameter(double radius) {  
    return 2 * radius;  
}
  • Purpose: This function computes the diameter of the circle using the formula: Diameter=2×Radius\text{Diameter} = 2 \times \text{Radius}Diameter=2×Radius
  • Parameters:
    • radius: The radius of the circle (passed as a parameter).
  • Returns: The diameter of the circle.
  1. Calculate Circumference
cppCopy codedouble calculateCircumference(double radius) {  
    return 2 * M_PI * radius;  
}
  • Purpose: Computes the circumference using the formula: Circumference=2×π×Radius\text{Circumference} = 2 \times \pi \times \text{Radius}Circumference=2×π×Radius
  • Parameters:
    • radius: The radius of the circle.
  • Returns: The circumference of the circle.
  1. Calculate Area
cppCopy codedouble calculateArea(double radius) {  
    return M_PI * radius * radius;  
}
  • Purpose: Computes the area of the circle using the formula: Area=π×(Radius)2\text{Area} = \pi \times (\text{Radius})^2Area=π×(Radius)2
  • Parameters:
    • radius: The radius of the circle.
  • Returns: The area of the circle.

3. main() Function

  1. Variable Declaration
cppCopy codedouble radius;
  • Declares a variable radius to store the user input.
  1. User Input
cppCopy codestd::cout << "Enter the radius of the circle: ";  
std::cin >> radius;
  • Prompts the user to enter the radius of the circle and reads it into the radius variable.
  1. Function Calls and Output
cppCopy codestd::cout << "Diameter of the circle: " << calculateDiameter(radius) << std::endl;
  • Calls calculateDiameter(radius) and prints the result.
cppCopy codestd::cout << "Circumference of the circle: " << calculateCircumference(radius) << std::endl;
  • Calls calculateCircumference(radius) and prints the result.
cppCopy codestd::cout << "Area of the circle: " << calculateArea(radius) << std::endl;
  • Calls calculateArea(radius) and prints the result.
  1. Program Termination
cppCopy codereturn 0;
  • Indicates successful termination of the program.

How the Program Works

  1. The user is prompted to enter the radius of the circle.
  2. The program computes the diameter, circumference, and area using the respective functions.
  3. It prints the calculated values on the screen.

Example Output

If the user enters 5 as the radius:

Enter the radius of the circle: 5
Diameter of the circle: 10
Circumference of the circle: 31.4159
Area of the circle: 78.5398

Leave a Comment