Java program to calculate area and circumference of circle17 Mar 2025 | 3 min read In this section, we will create a Java program to calculate the area and circumference of the circle. Area of Circle FormulasWhen the radius is known: ![]() When the diameter is known: ![]() When the circumference is known: ![]() Where, A: is an area of a circle π: is a constant, whose value is 3.1415 or 22/7. r: is the radius of a circle d: is the diameter of a circle C: is the circumference of a circle Let's implement the above formulas in a Java program and find the area of the circle. In the following Java program, we have used a Java switch case because we have used three scenarios to find the area of the circle. The user provides an option and the switch statement executes the corresponding case. The three scenarios are: if the radius is known, if the diameter is known, if the circumference is known. Note: Throughout this section, we have used a constant Pi (π). To access the value of the constant, we have used a static field PI of the Math class. It is defined in Math class as follows:The double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter. Its value is 3.141592653589793. Instead of using Math.PI we can also write the value of pi (3.14 or 22/7) directly. AreaOfCircle.java Output 1: 1. If the radius is known 2. If the diameter is known 3. If the circumference is known Enter your choice: 1 Enter the radius of the circle: 6 The area of the circle is: 113.09733552923255 Output 2: 1. If the radius is known 2. If the diameter is known 3. If the circumference is known Enter your choice: 2 Enter the diameter of the circle: 16 The area of the circle is: 201.06192982974676 Output 3: 1. If the radius is known 2. If the diameter is known 3. If the circumference is known Enter your choice: 3 Enter the circumference of the circle: 30 The area of the circle is: 71.6197243913529 Output 4: 1. If the radius is known 2. If the diameter is known 3. If the circumference is known Enter your choice: 4 invalid choice! In the following Java program, we have calculated the circumference of the circle. The formula that we have used in the program is: Circumference (C) =2πr CircumferenceOfCircle.java Output: Enter the radius of the circle: 20 The circumference of the circle is: 125.66370614359172 In the following program, we have calculated the area and circumference of the circle in a single program. AreaAndCircumferenceOfCircle.java Output: Enter the radius of the circle: 6 The area of the circle is: 113.09733552923255 The circumference of the circle is: 37.69911184307752 Next TopicHow to set classpath in java |
Eclipse is the most popular IDE for Java application development. In order to use eclipse effectively, we must familiar with the workbench, concepts, and components of Eclipse IDE. Before moving ahead in this section, we will understand what is eclipse in Java, eclipse platform overview, and...
5 min read
A is a special type of decompiler which takes a class file as input and produces Java source code as output. The decompilation is exactly the reverse process of compilation. Thus, decompiler does not produce a replica of the source code. It is because a...
3 min read
Java extends keywords enable classes to inherit properties and behaviours from superclasses. It establishes an inheritance relationship between two classes (subclass and superclass). A subclass inherits all non-private characteristics and procedures from its superclass, which serves as both parent and base class. Syntax: class Subclass extends Superclass...
5 min read
A number is prime if it is divisible by 1 and itself. In other words, a prime number is a natural number with exactly two distinct natural number divisors 1 and itself. For example, 2, 3, 5, 7, 11, etc. are the prime numbers. Note that...
5 min read
In object-oriented programming, a class that stores and manages a single instance is referred to as a "Mono Class". The concept aligns with the Java Singleton Design Pattern, where a class provides a global point of access to a single instance and ensures its generation. Singleton Design...
4 min read
Representing KN as the sum of exactly N numbers in Java requires careful consideration of mathematical principles and programming techniques. Problem Statement We have given two integers N (exponent integer) and K (base integer). We have to represent KN as the sum of exactly N numbers. Print...
6 min read
The repaint method in java is accessible in java.applet.Applet class is a final method utilized at whatever point we need to call update technique alongside the call to paint method; the call to refresh method clears the ongoing window, plays out an update, and a short...
3 min read
The LocalDate class in Java offers a mechanism to interact with dates without the time or time zone component as part of the Java 8 Date and Time API. This immutable class represents a date (year, month, and day) but not its time. It is frequently necessary...
4 min read
Problem Statement Given a binary string we need to find the maximum difference of 0s and 1s inside the given binary string. Here, you treat 0 as +1 and 1 as -1, and then seek the maximum value of contiguous subarray. This maximum sum of a subarray...
4 min read
Programming deals with real-world problems by using arithmetic functions, including addition, subtraction, multiplication, division and modulus. The object-oriented nature of Java allows developers to put arithmetic operations inside methods, making both code reusability and understandability easier to achieve. In this section, we will create Java programs to...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India