[Solved] Area of a Circle with Java, C++, Python

Write a program to find the area of a circle using functions.

Input Format:

Input consists of 1 floating point number which corresponds to the radius of the circle.

Output Format:

Output consists of a single floating point number (correct to 2 decimal places). Refer sample output for formatting details.

Take the value of pi as 3.14.

Sample Input:

3

Sample Output:

The area of the circle is 28.26

Solution

import java.util.*;
import java.text.DecimalFormat;

public class main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        float r, area;
        r = input.nextFloat();
        // sauravhathi
        DecimalFormat df = new DecimalFormat("0.00");
        area = calarea(r);
        System.out.println("The area of the circle is " + df.format(area));

    }

    public static float calarea(float x){
        float c = 3.14 * x * x;
        return c;
    }
}
#include <iostream>
using namespace std;

int main() {
    float r, area;
    cin >> r;
    // sauravhathi
    area = calarea(r);
    printf("The area of the circle is %.2f\n", area);
    return 0;
}

float calarea(float x){
    float c = 3.14 * x * x;
    return c;
}
def calarea(x):
    c = 3.14 * x * x
    return c

r = float(input())
area = calarea(r)
printf("The area of the circle is %.2f\n", area)

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *