This Java program is used to calculate the area of a circle.
Example:
import java.util.Scanner;
public class CircArea {
public static void main(String ag[]) {
int rad;
double pie = 3.14, ar;
Scanner s = new Scanner(System.in);
System.out.print("Enter radius of circle:");
rad = s.nextInt();
ar = pie * rad * rad;
System.out.println("Area of circle:" + ar);
}
}
Explanation:
This program is used to calculate the area of a circle where the radius will be fetched from user. So, first you have to import the util package of Java so that you can use the Scanner class in this program which will help programmers to fetch input from users. Then define a class name CircArea. Inside the class define the main() function. All Java program needs one main() function from where it starts executing program. Next declare an integer type variable rad which will hold the radius of the circle and two double type variable pie and ar, where pie will store the decimal value which is 3.14 and ar will be used to store the calculated area of circle. Also define an object name s of Scanner class using which the value will be fetched from input device. Then System.out.println(); will be used to display a message - " Enter radius of circle:". Then the statement rad = s.nextInt(); will be used to fetch the value from keyboard, and parse it to integer from string and store to variable rad. Then calculating the result value of multiplication of pie and rad * rad will get assigned to ar variable.
Then the next System.out.println() will be used to display the message - "Area of circle:" along with the calculated output which is stored in the variable ar.