Find area of a circle in C, C++, Java and Python | faceprep

Program to find the area of a circle is discussed here. Area of the circle can be found using the formula, Area = πr², where r is the radius of the circle. When the radius of the circle is known, the area of the circle can be calculated using the formula mentioned.

INPUT & OUTPUT FORMAT:

  • The input corresponds to the radius of the circle.
  • The output corresponds to the area of the circle and it should be a floating point variable with 2 point precision.

Program to find area of a circle using functions

// Area of a circle in C programming

#include <stdio.h>
#include <math.h>

float area_of_a_circle(float radius)
{
return M_PI * radius * radius;
}
int main()
{
float area, radius;
printf(“\nEnter the radius of the circle : “);
scanf(“%f”, &radius);
area = area_of_a_circle(radius);
printf(“\nArea of the circle : %f\n”, area);
return 0;

// C++ program to find the area of a circle

#include <iostream>
#include <math.h>
using namespace std;

float area_of_a_circle(float radius)
{
return M_PI * radius * radius;
}

int main()
{
float area, radius;
cout << “\nEnter the radius of the circle : “;
cin >> radius;
area = area_of_a_circle(radius);
cout << “\nArea of the circle : ” << area <<endl;
return 0;
}

// Java program to find the area of a circle
import java.util.*;
public class Main
{
static double area_of_a_circle(double radius)
{
return Math.PI * radius * radius;
}

public static void main (String[] args)
{
double radius;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter the radius of the circle : “);
radius = sc.nextDouble();
System.out.println(“Area of the circle : ” + area_of_a_circle(radius));

}
}

 

# Python program to find the area of a circle

from math import pi
def area_of_a_circle(radius):
return str(pi * radius**2)
radius = float(input (“Input the radius of the circle : “))
print (“The area of the circle is: ” + area_of_a_circle(radius))

Program to find area of a circle using pointers

#include <stdio.h>
#include <math.h>

float area_of_circle(float *radius)
{
return M_PI * (*radius) * (*radius);
}

#include<stdio.h>

int main()
{
float radius;

printf(“\nEnter the radius of Circle : “);
scanf(“%f”, &radius);
printf(“\nArea of Circle : %f”, area_of_circle(&radius));

return (0);
}

#include <iostream>
#include <math.h>
using namespace std;

double area_of_circle(double *radius)
{
return M_PI * (*radius) * (*radius);
}

int main()
{
double radius;

cout << “\nEnter the radius of Circle : “;
cin >> radius;
cout << “\nArea of Circle : ” << area_of_circle(&radius);
return 0;
}

Program to find area of circle using command line arguments

/*Here diameter is taken as the input*/

#include<stdio.h>
#define PI 3.14
int main(int a, char *b[])   //command line arguments
{
int d; float area =0;
d= atoi(argv[1]);
area =(float) PI*(d/2)*(d/2);
printf(“%0.2f”, area);    //%0.2f is to print the answer with 2 values after decimal point.
return 0;
}

Output:

area of a circle