Write a menu driven program that reads the radius of a circle and displays options to calculate area or perimeter of the circle depending upon choice by the user. Write the full java code
Answers
Program:
import java.util.Scanner;
public class something
{
public static void main(String []args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter radius : ");
int radius = sc.nextInt();
System.out.print("Menu: 1-Area 2-Perimeter");
System.out.print("Choose what you wish to calculate : ");
int choice = sc.nextInt();
if(choice==1)
System.out.println("Area of the circle is "+(3.14*radius*radius));
else if(choice==2)
System.out.println("Perimeter of the circle is "+(2*3.14*radius));
else
System.out.println("Wrong Input");
sc.close();
}
}
Input:
Enter radius : 5
Menu: 1-Area 2-Perimeter
For Area:
Choose what you wish to calculate : 1
For Perimeter:
Choose what you wish to calculate : 2
Giving wrong input:
Choose what you wish to calculate : 9
Output:
For Area:
Area of the circle is 78.5 Sq.units
For Perimeter:
Perimeter of the circle is 31.400000000000002 Sq.units
For wrong input:
Wrong Input
Explanation:
Simple! All you need to do is, to let user know the menu using print statement and let them enter their choice. Once done, Check what the user's input matches and evaluate that part with the input parameter given.
Hope it helps. Cheers!!!