[C/C++] Program to Find The Area of Circle

Program to find area of circle using radius or diameter

  1. 2 functions one will calculate and return area of circle by multiplying radius
  2. Other function will get diameter as parameter and will divide it to half which is equals to radius of circle and pass that value to 1st function.

C Code

#include <stdio.h>
#include <conio.h>

const float PI = 3.14159;

float AreaOfCircle(float radius);
float AreaWithDiameter(float diameter);

int main()
{
 float radius,diameter,circleArea;
 int choice=0;
 
 printf("Find Area Of Circle\n");
 
 do
 {
 printf("[1] Radius\n[2] Diameter\n");
 printf("Choose an Option: ");
 scanf("%d", &choice);
 if(choice != 1 && choice != 2)
 printf("Invalid Option Please Choose Valid Option\n\n");
 }
 while(choice != 1 && choice != 2);

 if(choice == 1)
 {
 printf("\nEnter Radius: ");
 scanf("%f", &radius);
 circleArea=AreaOfCircle(radius);
 }
 else if(choice == 2 )
 {
 printf("\nEnter Diameter: ");
 scanf("%f", &diameter);
 circleArea=AreaWithDiameter(diameter);
 }
 
 printf("\nArea of Circle is: %.2f", circleArea);
 
 return 0;
}

float AreaOfCircle(float radius)
{
 return (PI*(radius*radius));

}

float AreaWithDiameter(float diameter)
{

 return (AreaOfCircle(diameter/2));
}

C++ Code

#include <iostream>

using namespace std;

const float PI = 3.14159;

float AreaOfCircle(float radius);
float AreaWithDiameter(float diameter);

int main()
{
 float radius,diameter,circleArea;
 int choice=0;
 
 cout << "Find Area Of Circle" << endl;
 
 do
 {
 cout << "[1] Radius" << endl
 << "[2] Diameter " << endl;
 cout << "Choose an Option: ";
 cin >> choice;
 if(choice != 1 && choice != 2)
 cout << "Invalid Option Please Choose Valid Option" << endl << endl;
 }
 while(choice != 1 && choice != 2);

 if(choice == 1)
 {
 cout << endl << "Enter Radius: ";
 cin >> radius;
 circleArea=AreaOfCircle(radius);
 }
 else if(choice == 2 )
 {
 cout << endl << "Enter Diameter: ";
 cin >> diameter;
 circleArea=AreaWithDiameter(diameter);
 }
 
 cout << endl << "Area of Circle is: " << circleArea;
 
 return 0;
}

float AreaOfCircle(float radius)
{
 return (PI*(radius*radius));

}

float AreaWithDiameter(float diameter)
{

 return (AreaOfCircle(diameter/2));
}

Output:

Find-Area-of-Circle

 

Other Related Search Terms

  1. C Program to Find Area of circle
  2. C++ program to find Area of circle using radius
  3. C and C++ find area of circle using diameter

Categories: C Language,C Programs,Computer Science

Tags: ,,

Leave A Reply

Your email address will not be published.

×