Thursday, September 14, 2023

How to calculate Perimeter or Circumference of Circle in Java? Example

One of the common programming exercises for beginners is to write a program to calculate the perimeter or circumference of a circle in Java. This exercise is not just common in Java but also in other programming language courses like C, C++, or Python. I first did this exercise on C++ long ago, but that time the joy of a working program was something different. I was quite happy to see my program compiled without error and can print the correct circumference after entering radius. 

That program taught me how to take input from a user and how to display output into the console, I didn't know how to create methods to encapsulate code. For us, only the main method matters at that time. Anyway, let's focus on the job at hand i.e. how to find the circumference of the circle of a given radius. You have to accept the radius from the user and show the output to the console.

In order to calculate the perimeter of a circle, you must first remember the formula to calculate the circumference of a circle. Well, the formula is not difficult to remember, it's  2*PI*R, where PI is a well-known Mathematics constant and R is the radius of the circle.

You can get the value of PI in Java by accessing Math.PI constant. Some programmers like to put the value of PI as 3.14 which is Ok for testing and demo, but not good for anything else. You should use Math.PI wherever you need the value of PI in the Java program.





Java Program to calculate Circumference of a circle

Now we need a value of radius which can be a user-provided value, alternatively, you can also pass in the program itself.

Let see the first option if we go by user value our program will prompt the user to enter a value for the radius. The Scanner class is used to take user input from the console it has many utility methods to get data from the console later will convert user input to the desired form and then pass the radius to perimeter() function to calculate the circumference of the circle and store the result and valid variable.

now if we go to the second option it's very simple in the program itself we can pass the value of radius and then use that value to calculate the perimeter of the circle and then display the result.

Here is our complete Java program to accept input from the user and print output into the console. We have used the perimeter() function to calculate the circumference of the circle. This method returns a double value because PI is a floating-point constant.


mport java.util.Scanner;

/*
* Java Program to calculate circumference of circle
*/
public class Main {

public static void main(String args[]) {

//creating scanner to accept radius of circle
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome in Java program to calculate Perimeter of circle");
System.out.println("Formula for calculating perimeter of circle is '2*PI*R' ");

System.out.println("Please enter radius of circle:");
float radius = scanner.nextFloat();


double perimeter = perimeter(radius);

System.out.println("Perimeter of Circle calculate by Java program is : "
 + perimeter);

scanner.close();
}

/*
* @return perimeter of a circle. 
*/
public static double perimeter(float radius){
return 2*Math.PI*radius;

}
}

You can run this program by copying the code into your favorite editors like NotePad++ or IDE like Eclipse. You can also run it from the command line if you are not familiar with any IDE. In order to run from the command line, you also need to compile the class using the javac command. This will produce the class file which you can run using the java command as shown below:

$javac Main.java
$java Main

Remember, when we compile we provide the name of the source file but when we run we provide the name of the class which contains the main() method, This is the key difference between compiling and running a Java program. When you run, the program will produce the following output:

Welcome in Java program to calculate Perimeter of circle
Formula for calculating perimeter of circle is '2*PI*R' 
Please enter radius of circle:
4
Perimeter of Circle calculate by Java program is : 25.132741228718345

You can see that user-entered value of radius as 4 and the program printed out the circumference of the circle as 25.13, which is correct as per the following formula:

How to calculate Perimeter/Circumference of Circle


That's all about how to calculate the circumference or perimeter of the circle in Java. It's one of the simple Java programming exercises but really help you build your coding skill. You learn and understand how to use basic operators, API, and functions to solve some common mathematics problems. I strongly recommend Java beginners solve as many problems as possible.


Other Java Coding Exercises for Beginners for Practice
  • How to count vowels and consonants in a given String in Java? (solution)
  • How to transpose a matrix in Java? (solution)
  • How to implement binary search using recursion in Java? (solution)
  • How to reverse a String in place in Java? (solution)
  • Java Program to print Prime numbers from 1 to 100? (program)
  • How to implement Linear Search in Java? (solution)
  • How to find the largest prime factors of a given integer in Java? (program)
  • How to reverse words in a given String in Java? (solution)
  • How to print Fibonacci series in Java (solution)
  • How to calculate the square root of a given number in Java? (solution)
  • How to find the highest occurring word from a given file in Java? (solution)
  • How to check if two rectangles intersect with each other in Java? (solution)
  • How to find all permutations of a given String in Java? (solution)
  • How to check if a String contains duplicate characters in Java? (solution)
  • How to calculate the sum of all elements of an array in Java? (program)
  • How to reverse an array in place in Java? (solution)
  • How to print prime numbers up to a given number in Java? (solution)
  • How to find if given Integer is Palindrome in Java? (solution)
  • How to calculate the average of all numbers of an array in Java? (program)

Thanks for reading this article so far, if you like this article then please share it with your friends and colleagues. If you have any questions or feedback then please drop a comment.  If you have just started learning Java and looking for a good resource as a companion, I suggest you join these free Java courses to learn core Java from scratch. 

Also, what is your favorite Java programming exercise? Palindrom, Prime Number, Fibonacci series, Binary Search, or this one?  Do let me know in comments. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.