Java Program to calculate Perimeter/Circumference of Circle

One of the common programming exercise 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 on other programming language courses e.g. 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 about how to create methods to encapsulate code. For us, only main method matters at that time. Anyway, let's focus on the job in hand i.e. how to find the circumference of Circle of given radius. You have to accept radius from the user and show the output to 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 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 programmer 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 the Math.PI wherever you need the value of PI in Java program.





Java Program to calculate Circumference of 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 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 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 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 editor e.g. 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 javac command. This will produce the class file which you can run using java command as shown below:

$javac Main.java
$java Main

Remember, when we compile we provide the name of source file but when we run we provide the name of the class which contains main() method, This is the key difference between compiling and running a Java program. When you run, the program will produce 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 program printed out the circumference of the circle as 25.13, which is correct as per 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 to solve as many problems as possible.


Other Java Coding Exercises for Beginners for Practice
  • How to count vowels and consonants in 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 largest prime factors of a given integer in Java? (program)
  • How to reverse words in a given String in Java? (solution)
  • How to find all prime factors of a number in Java? (solution)
  • How to check if two given Strings are Anagram in Java? (solution)
  • How to generate prime numbers up to 100 using Sieve of Eratosthenes Algorithm(program)
  • How to remove duplicate characters from String in Java? (solution)
  • How to check if a year is a leap year in Java? (solution)
  • How to find the square root of a number without using a library function in Java? (program)
  • How to remove duplicate elements from the array in Java? (solution)
  • How to check if given number is prime in Java (solution)
  • How to check if a number is binary in Java? (algorithm)
  • How to calculate Area of Triangle in Java? (program)
  • 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 given String is palindrome or not 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 number 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 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 book as a companion, I suggest you buy Head First Java, 2nd Edition, one of the best books to learn core Java from scratch. 

No comments:

Post a Comment