Circle program in Java
Circle program in Java demonstrates the area of the circle and the circumference of the circle.
The area of a 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.
The circumference of a circle is defined as 2*pi*r where pi is a constant whose value is (22/7 or 3.142) and r is the radius of a circle.
The area of a circle is given by = pi*r*r
Circumference of circle is given by = 2*pi*r
Example 1:
Input: Enter the radius of the circle: 5
Output:
Area of the circle is: 78.54 Circumference of the circle: 31.42
Example 2:
Input: Enter the radius of the circle: 7
Output:
Area of the circle is: 153.94 Circumference of the circle: 43.98
Example 3:
Input: Enter the radius of the circle: 10
Output:
Area of the circle is: 314.16 Circumference of the circle: 62.83
Algorithm:
Step 1: Start the program
Step 2: Input the value of radius (float value).
Step 3: Calculate the area using the formula pi*(r2)
Step 4: Calculate circumference using formula 2*pi*r
Step 5: Print the area of the circle
Step 6: Print the Circumference of the circle
Let us try to understand the Java program the radius and circumference of the circle
Filename: Circle.java
import java.util.Scanner; import java.lang.Math; class AreaCircum { void AreaCircumference() { int r; double area,circum; Scanner s = new Scanner(System.in); System.out.print("Enter radius of circle: "); r = s.nextInt(); area = Math.PI * r * r; System.out.println("Area of circle: "+area); circum = 2 * Math.PI * r; System.out.println("Circumference of circle: "+circum); } } public class Circle { public static void main(String[] args) { AreaCircum ac = new AreaCircum(); ac.AreaCircumference(); } }
Output:
Enter radius of circle: 5 Area of circle: 78.53981633974483 Circumference of circle: 31.41592653589793 Enter radius of circle: 7 Area of circle: 153.93804002589985 Circumference of circle: 43.982297150257104 Enter radius of circle: 10 Area of circle: 314.1592653589793 Circumference of circle: 62.83185307179586
Complexity analysis:
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.
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, area, and circum), 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.
https://www.sanfoundry.com/java-program-find-area-circle-given-radius/