Java Program to Calculate Area of Circle

In this Java programming tutorial, we will make a Java program to calculate the area of a circle, if any users enters the radius of that circle. It is a very simple and basic Java problem, in which we will take radius as a input from the user and after that just simply apply the mathematical formula to calculate radius in the program code, which is  π * r² that is pi times the square of the radius of circle.

Java Program to Calculate Area of Circle

/*
Program to find the area of any circle by Codext
*/
import java.util.Scanner;
 // using Scanner class to get input from user
public class CalculateCircleArea {

        public static void main(String[] args) {

		System.out.println("Enter the radius of circle:");
		Scanner num = new Scanner(System.in);
        int radius = num.nextInt();
        System.out.println("The radius of the circle is " + radius);

        double area = Math.PI * radius * radius;

        System.out.println("Area of a circle is " + area);
        }
}

            /*
            Area of a circle is pi * r * r where r is a radius of a circle.
            */

            // NOTE :We have used Math.PI constant to get value of pi

Output of Java Program for Calculating Area of Circle

In Output firstly, we have compiled the Java program by "javac" command in cmd, after the successful compilation of our source code, we have run it through "java" command, and as you can see in the screen shot of program, here it asks for the radius of the circle, after entering the radius our java program displays the correct area of circle.

0 comments:

Post a Comment