Write a menu driven program to calculate area of different shapes and asks the user to
input calculate the area of selected parameters to shape.
Answers
Explanation:
Java Program to calculate area and circumference of circle
BY CHAITANYA SINGH | FILED UNDER: JAVA EXAMPLES
In this tutorial we will see how to calculate area and circumference of circle in Java. There are two ways to do this:
1) With user interaction: Program will prompt user to enter the radius of the circle
2) Without user interaction: The radius value would be specified in the program itself.
Program 1:
/**
* @author: BeginnersBook.com
* @description: Program to calculate area and circumference of circle
* with user interaction. User will be prompt to enter the radius and
* the result will be calculated based on the provided radius value.
*/
import java.util.Scanner;
class CircleDemo
{
static Scanner sc = new Scanner(System.in);
public static void main(String args[])
{
System.out.print("Enter the radius: ");
/*We are storing the entered radius in double
* because a user can enter radius in decimals
*/
double radius = sc.nextDouble();
//Area = PI*radius*radius
double area = Math.PI * (radius * radius);
System.out.println("The area of circle is: " + area);
//Circumference = 2*PI*radius
double circumference= Math.PI * 2*radius;
System.out.println( "The circumference of the circle is:"+circumference) ;
}
}
Output:
Enter the radius: 1
The area of circle is: 3.141592653589793
The circumference of the circle is:6.283185307179586
Program 2:
/**
* @author: BeginnersBook.com
* @description: Program to calculate area and circumference of circle
* without user interaction. You need to specify the radius value in
* program itself.
*/
class CircleDemo2
{
public static void main(String args[])
{
int radius = 3;
double area = Math.PI * (radius * radius);
System.out.println("The area of circle is: " + area);
double circumference= Math.PI * 2*radius;
System.out.println( "The circumference of the circle is:"+circumference) ;
}
}
Output:
The area of circle is: 28.274333882308138
The circumference of the circle is:1
Hope it helps ^_^
This program is in Java
_____________________________________
import java.util.Scanner;
public class Area_Waala_Program{
public static void main(String [] args){
double a, b, c, s, area = 0;
//Taking input from the user
Scanner sc = new Scanner(System.in);
System.out.println("Area Calculator for Simple Closed Figures");
System.out.println("Press 1 for Circle");
System.out.println("Press 2 for Square");
System.out.println("Press 3 for Rectangle");
System.out.println("Press 4 for Triangle");
double choice = sc.nextDouble();
//Main part comes here
//Circle
if (choice == 1){
System.out.println("Circle");
System.out.print("Enter the Radius: ");
a = sc.nextDouble();
area = Math.PI*a*a;
System.out.println("The area of this Circle is " + area + " sq.unit/s");
}
//Square
if (choice == 2){
System.out.println("Square");
System.out.print("Enter the Side: ");
a = sc.nextDouble();
area = a*a;
System.out.println("The area of this Square is " + area + " sq.unit/s");
}
//Rectangle
if (choice == 3){
System.out.println("Rectangle");
System.out.print("Enter the Length: ");
a = sc.nextDouble();
System.out.print("Enter the Breadth: ");
b = sc.nextDouble();
area = a*b;
System.out.println("The area of this Rectangle is " + area + " sq.unit/s");
}
//Triangle
if (choice == 4){
System.out.println("Triangle");
System.out.print("Enter the measure of the First side: ");
a = sc.nextDouble();
System.out.print("Enter the measure of the Second side: ");
b = sc.nextDouble();
System.out.print("Enter the measure of the Third side: ");
c = sc.nextDouble();
boolean truth = (a+b) > c && (a + c) > b && (b + c) > a;
if (truth == true)
{
s = (a + b + c)/2;
area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
System.out.println("The area of this Triangle is " + area + " sq.unit/s");
}
else if (truth == false){
System.out.println("These sides cannot form a triangle *_*");
}
}
if (choice > 4 || choice < 1){
System.out.println("Valid choices are only 1-4");
}
}
}
_____________________________________
The underlined values are the inputs
Output:
(i)
Area Calculator for Simple Closed Figures
Press 1 for Circle
Press 2 for Square
Press 3 for Rectangle
Press 4 for Triangle
1
Circle
Enter the Radius: 5
The area of this Circle is 78.53981633974483 sq.unit/s
(ii)
Area Calculator for Simple Closed Figures
Press 1 for Circle
Press 2 for Square
Press 3 for Rectangle
Press 4 for Triangle
2
Square
Enter the Side: 7
The area of this Square is 49.0 sq.unit/s
(iii)
Area Calculator for Simple Closed Figures
Press 1 for Circle
Press 2 for Square
Press 3 for Rectangle
Press 4 for Triangle
3
Rectangle
Enter the Length: 10
Enter the Breadth: 13
The area of this Rectangle is 130.0 sq.unit/s
(iv)
Area Calculator for Simple Closed Figures
Press 1 for Circle
Press 2 for Square
Press 3 for Rectangle
Press 4 for Triangle
4
Triangle
Enter the measure of the First side: 52
Enter the measure of the Second side: 48
Enter the measure of the Third side: 20
The area of this Triangle is 480.0 sq.unit/s
(vi)
Area Calculator for Simple Closed Figures
Press 1 for Circle
Press 2 for Square
Press 3 for Rectangle
Press 4 for Triangle
4
Triangle
Enter the measure of the First side: 6
Enter the measure of the Second side: 7
Enter the measure of the Third side: 18
These sides cannot form a triangle *_*
(v)
Area Calculator for Simple Closed Figures
Press 1 for Circle
Press 2 for Square
Press 3 for Rectangle
Press 4 for Triangle
6
Valid choices are only 1-4