# Java Program to Find the Area of a Circle

Problem Description

Write a Java program to find the area of a circle using the given radius.

Area of circle is defined as pi*r*r where pi is a constant whose value is (22/7 or 3.142) and r is the radius of a circle.

Formula to calculate the area of circle is: Area = pi*r*r

Example:
If the radius of a circle is 30, then area of circle is pi*r*r = 3.14*22*22 = 1519.76.

Problem Solution

Step 1: Start the program
Step 2: Input the value of radius (float value).
Step 3: Calculate area using formula pi*(r2)
Step 4: Print area of circle
Step 5: End the Program

Program/Source Code

Here is the source code of the Java Program to Find the Area of a Circle Given the Radius. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

advertisement
advertisement
1. `/*`
2. ` * Java program to find the area of a circle`
3. ` */`
4. ` `
5. `public class Circle `
6. `{`
7. `    public static void main(String[] args) `
8. `    {`
9. `        int r;`
10. `        double pi = 3.14, area;`
11. `        Scanner s = new Scanner(System.in);`
12. `        System.out.print("Enter radius of circle: ");`
13. `        r = s.nextInt();`
14. `        area = pi * r * r;`
15. `        System.out.println("Area of circle: "+area);`
16. `    }            `
17. `}`
Program Explanation

1. This is a Java program that calculates the area of a circle based on the radius.
2. The program defines a class called “Circle“. Within the Circle class, there is a method called “main” that takes an array of strings as input.
3. The program declares three variables: an integer variable called “r” to store the radius, a double variable called “pi” to store the value of pi, and a double variable called “area” to store the calculated area.
4. Then the program calculates the area of the circle using the formula pi * r * r and stores the result in the “area” variable.
5. Finally, the program outputs the calculated area using the “println” method.

Time Complexity: O(1)
The time complexity of this program is O(1), which means that the running time of the program is constant and does not depend on the size of the input. This is because the program performs a fixed set of operations, regardless of the input value.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Space Complexity: O(1)
The space complexity of this program is also O(1), as it only uses a fixed amount of memory to store a few variables (r, pi, area, and s), regardless of the input value. The amount of memory used by the program is constant and does not grow with the size of the input.

Program Output:

In this case, the given radius is “22” to calculate the area of circle.

```\$ javac Circle.java
\$ java Circle

Enter radius of circle: 22
Area of circle: 1519.76```

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.