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.
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
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.
/*
* Java program to find the area of a circle
*/
public class Circle
{
public static void main(String[] args)
{
int r;
double pi = 3.14, area;
Scanner s = new Scanner(System.in);
System.out.print("Enter radius of circle: ");
r = s.nextInt();
area = pi * r * r;
System.out.println("Area of circle: "+area);
}
}
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.
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.
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”.
- Practice BCA MCQs
- Check Programming Books
- Check Java Books
- Apply for Java Internship
- Practice Information Technology MCQs