Open In App

Program to find area of a circle

Last Updated : 27 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given the radius r. Find the area of a circle. The area of the circle should be correct up to 5 decimal places.

area_of_circle

Examples:

Input: r = 5
Output: 78.53982
Explanation: As area = PI * r * r = 3.14159265358979323846 * 5 * 5 = 78.53982, as we only keep 5 digits after decimal.

Input: r = 2
Output: 12.56637
Explanation: As area = PI * r * r = 3.14159265358979323846 * 2 * 2 = 12.56637, as we only keep 5 digits after decimal.

The area of a circle can be calculated using the formula: area = PI * r * r

C++
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

float findArea(float r) {
    return (M_PI * r * r);
}

int main() {
    float r = 5, area;
    area = findArea(r);
    cout << fixed << setprecision(5) << area;
    return 0;
}
C
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846

float findArea(float r) {
    return (PI * r * r);
}

int main() {
    float r = 5, area;
    area = findArea(r);
    printf("%.5f\n", area);
    return 0;
}
Java
import java.lang.Math;

class GfG {
    static float findArea(float r) {
        return (float)(Math.PI * r * r);
    }

    public static void main(String[] args) {
        float r = 5;
        float area = findArea(r);
        System.out.printf("%.5f%n",area);
    }
}
Python
import math

def findArea(r):
    return math.pi * r * r

if __name__ == "__main__":
  r = 5
  area = findArea(r)
  print(f"{area:.5f}")
C#
using System;

class GfG {
    static float FindArea(float r) {
        return (float)(Math.PI * r * r);
    }

    static void Main() {
        float r = 5;
        float area = FindArea(r);
        Console.WriteLine("{0:F5}",area);
    }
}
JavaScript
function findArea(r) {
    return Math.PI * r * r;
}

//Driver Code
let r = 5;
let area = findArea(r);
console.log(area.toFixed(5));

Output
78.53982

Time Complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.
 


Next Article

Similar Reads