Posted on by Kalkicode
Code Geometric

# Calculate area of circle

The area of a circle is a fundamental mathematical concept used in various fields such as geometry, physics, and engineering. The formula to calculate the area of a circle is π * radius^2, where π (pi) is a constant approximately equal to 3.14159.

## Problem Statement

Given the radius of a circle, the task is to calculate and display its corresponding area.

## Example

For a circle with a radius of 7 units, the area can be calculated using the formula: π * (7^2) = 154π square units.

## Solution Idea

The solution involves using the formula for calculating the area of a circle and displaying the result.

## Pseudocode

``````circle_area(radius):
display area``````

## Algorithm Explanation

1. The `circle_area` function takes the radius of a circle as input.
2. It calculates the area of the circle using the formula π * (radius * radius).
3. The calculated area is displayed along with the corresponding radius.

## Code Solution

``````// C Program
// Calculate area of circle
#include <stdio.h>

#include <math.h>

//Calculate area of circle by given radius
void circle_area(double r)
{
//M_PI containing π value 3.14159265358979323846
double area = M_PI * (r * r);
//Display the result of circle area
printf("Circle Radius : %lf , Circle Area : %lf\n", r, area);
}
int main()
{
//Test Cases
circle_area(63);
circle_area(15.5);
circle_area(34.47);
return 0;
}``````

#### Output

``````Circle Radius : 63.000000 , Circle Area : 12468.981242
Circle Radius : 15.500000 , Circle Area : 754.767635
Circle Radius : 34.470000 , Circle Area : 3732.780387``````
``````/*
Java program
Calculate area of circle
*/
public class CircleArea
{
//Calculate area of circle by given radius
public void circle_area(double r)
{
//Math PI containing π value nearly 3.141592653589793
double area = Math.PI * (r * r);
//Display the result of circle area
System.out.print("Circle Radius : " + r + " , Circle Area : " + area + "\n");
}
public static void main(String[] args)
{
CircleArea obj = new CircleArea();
//Test Cases
obj.circle_area(63);
obj.circle_area(15.5);
obj.circle_area(34.47);
}
}``````

#### Output

``````Circle Radius : 63.0 , Circle Area : 12468.981242097889
Circle Radius : 15.5 , Circle Area : 754.7676350249478
Circle Radius : 34.47 , Circle Area : 3732.7803865757082
``````
``````/*
C++ program
Calculate area of circle
*/
#include<iostream>
#include <math.h>
using namespace std;
class CircleArea
{
public:
//Calculate area of circle by given radius
void circle_area(double r)
{
//M_PI containing π value nearly 3.141592653589793
double area = M_PI *(r *r);
cout << "Circle Radius : " << r << " , Circle Area : " << area << "\n";
}
};
int main()
{
CircleArea obj =  CircleArea();
//Test Cases
obj.circle_area(63);
obj.circle_area(15.5);
obj.circle_area(34.47);
return 0;
}``````

#### Output

``````Circle Radius : 63 , Circle Area : 12469
Circle Radius : 15.5 , Circle Area : 754.768
Circle Radius : 34.47 , Circle Area : 3732.78``````
``````/*
C# program
Calculate area of circle
*/
using System;
public class CircleArea
{
//Calculate area of circle by given radius
public void circle_area(double r)
{
//Math PI containing π value nearly 3.141592653589793
double area = Math.PI * (r * r);
Console.Write("Circle Radius : " + r + " , Circle Area : " + area + "\n");
}
public static void Main(String[] args)
{
CircleArea obj = new CircleArea();
//Test Cases
obj.circle_area(63);
obj.circle_area(15.5);
obj.circle_area(34.47);
}
}``````

#### Output

``````Circle Radius : 63 , Circle Area : 12468.9812420979
Circle Radius : 15.5 , Circle Area : 754.767635024948
Circle Radius : 34.47 , Circle Area : 3732.78038657571``````
``````<?php
/*
Php program
Calculate area of circle
*/
class CircleArea
{
//Calculate area of circle by given radius
public 	function circle_area(\$r)
{
//M_PI  containing π value nearly 3.141592653589793
\$area = M_PI * (\$r*\$r);
//Display the result of circle area
echo("Circle Radius : ". \$r ." , Circle Area : ". \$area ."\n");
}
}

function main()
{
\$obj = new CircleArea();
//Test Cases
\$obj->circle_area(63);
\$obj->circle_area(15.5);
\$obj->circle_area(34.47);
}
main();``````

#### Output

``````Circle Radius : 63 , Circle Area : 12468.981242098
Circle Radius : 15.5 , Circle Area : 754.76763502495
Circle Radius : 34.47 , Circle Area : 3732.7803865757``````
``````/*
Node Js program
Calculate area of circle
*/
class CircleArea
{
//Calculate area of circle by given radius
circle_area(r)
{
//Math PI containing π value nearly 3.141592653589793
var area = Math.PI *(r *r);
//Display the result of circle area
process.stdout.write("Circle Radius : " + r + " , Circle Area : " + area + "\n");
}
}

function main(args)
{
var obj = new CircleArea();
//Test Cases
obj.circle_area(63);
obj.circle_area(15.5);
obj.circle_area(34.47);
}
main();``````

#### Output

``````Circle Radius : 63 , Circle Area : 12468.981242097889
Circle Radius : 15.5 , Circle Area : 754.7676350249478
Circle Radius : 34.47 , Circle Area : 3732.7803865757082``````
``````#   Python 3 program
#   Calculate area of circle
import math
class CircleArea :
# Calculate area of circle by given radius
def circle_area(self, r) :
# r indicates radius of circle
# math.pi containing π value nearly 3.141592653589793
area = math.pi * (r * r)

# Display the result of circle area
print("Circle Radius : ", r ," , Circle Area : ", area ,"\n", end = "")

def main() :
obj = CircleArea()
# Test Cases
obj.circle_area(63)
obj.circle_area(15.5)
obj.circle_area(34.47)

if __name__ == "__main__": main()``````

#### Output

``````Circle Radius :  63  , Circle Area :  12468.981242097889
Circle Radius :  15.5  , Circle Area :  754.7676350249478
Circle Radius :  34.47  , Circle Area :  3732.7803865757082``````
``````#   Ruby program
#   Calculate area of circle

class CircleArea

# Calculate area of circle by given radius
def circle_area(r)

# r indicates radius of circle
# Math PI containing π value nearly 3.141592653589793
area = Math::PI * (r * r)
# Display the result of circle area
print("Circle Radius  : ", r ," , Circle Area  : ", area ,"\n")
end
end
def main()

obj = CircleArea.new()
# Test Cases
obj.circle_area(63)
obj.circle_area(15.5)
obj.circle_area(34.47)
end
main()``````

#### Output

``````Circle Radius  : 63 , Circle Area  : 12468.981242097889
Circle Radius  : 15.5 , Circle Area  : 754.7676350249478
Circle Radius  : 34.47 , Circle Area  : 3732.7803865757082
``````
``````/*
Scala program
Calculate area of circle
*/
class CircleArea
{
//Calculate area of circle by given radius
def circle_area(r: Double): Unit = {
//Math PI containing π value nearly 3.141592653589793
var area: Double = Math.PI * (r * r);
//Display the result of circle area
print("Circle Radius : " + r + " , Circle Area : " + area + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: CircleArea = new CircleArea();
//Test Cases
obj.circle_area(63);
obj.circle_area(15.5);
obj.circle_area(34.47);
}
}``````

#### Output

``````Circle Radius : 63.0 , Circle Area : 12468.981242097889
Circle Radius : 15.5 , Circle Area : 754.7676350249478
Circle Radius : 34.47 , Circle Area : 3732.7803865757082``````
``````/*
Swift program
Calculate area of circle
*/
class CircleArea
{
//Calculate area of circle by given radius
func circle_area(_ r: Double)
{
//Double.pi containing π value nearly 3.141592653589793
let area: Double = Double.pi * (r * r);

//Display the result of circle area
print("Circle Radius : ", r ," , Circle Area : ", area ,"\n", terminator: "");
}
}
func main()
{
let obj: CircleArea = CircleArea();
//Test Cases
obj.circle_area(63);
obj.circle_area(15.5);
obj.circle_area(34.47);
}
main();``````

#### Output

``````Circle Radius :  63.0  , Circle Area :  12468.9812420979
Circle Radius :  15.5  , Circle Area :  754.767635024948
Circle Radius :  34.47  , Circle Area :  3732.78038657571``````

## Output Explanation

The output of the program displays the calculated area for each provided radius. For example, when the radius is 63 units, the area is calculated as 12468.981242 square units.

## Time Complexity

The time complexity of the algorithm is constant because the calculation involves basic arithmetic operations. The time taken to calculate the area is not affected by the value of the radius.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post