Here, we have a basic program example to calculate area of different shapes driven by a menu approach using different languages. This program is created in c language, c++, Java, and Python.
Program to calculate are of different shapes in C language
#include <stdio.h>
void main ()
{
int choice,r,l,w,b,h,s;
float area;
printf("Input 1 for area of circle\n");
printf("Input 2 for area of rectangle\n");
printf("Input 3 for area of triangle\n");
printf("Input 4 for area of square\n");
printf("Input your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Input radious of the circle : ");
scanf("%d",&r);
area=3.14*r*r;
break;
printf("The area is : %f\n",area);
case 2:
printf("Input length and width of the rectangle : ");
scanf("%d%d",&l,&w);
area=l*w;
break;
printf("The area is : %f\n",area);
case 3:
printf("Input the base and hight of the triangle :");
scanf("%d%d",&b,&h);
area=.5*b*h;
break;
printf("The area is : %f\n",area);
case 4:
printf("Input the side of the square :");
scanf("%d",&s);
area=s*s;
break;
printf("The area is : %f\n",area);
default:
printf("please choose from the given options.");
break;
}
}
Program to calculate are of different shapes in C++ language
#include<iostream>
using namespace std;
int main()
{
int choice,r,l,w,b,h,s;
float area;
cout<<"Input 1 for area of circle\n";
cout<<"Input 2 for area of rectangle\n";
cout<<"Input 3 for area of triangle\n";
cout<<"Input 4 for area of square\n";
cout<<"Input your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Input radious of the circle : ";
cin>>r;
area=3.14*r*r;
cout<<"The area is : "<<area;
break;
case 2:
cout<<"Input length and width of the rectangle : ";
cin>>l>>w;
area=l*w;
cout<<"The area is : "<<area;
break;
case 3:
cout<<"Input the base and hight of the triangle :";
cin>>b>>h;
area=.5*b*h;
cout<<"The area is : "<<area;
break;
case 4:
cout<<"Input the side of the square :";
cin>>s;
area=s*s;
cout<<"The area is : "<<area;
break;
default:
cout<<"please choose from the given options.";
break;
}
}
Program to calculate are of different shapes in Python language
print("Input 1 for area of circle");
print("Input 2 for area of rectangle");
print("Input 3 for area of triangle");
print("Input 4 for area of square");
choice=int(input("Input your choice : "))
if(choice == 1):
r=int(input("Input radius of the circle"))
area=r*r
print("The area of circle is ", area)
elif(choice == 2):
l=int(input("Input the length of retangle: "))
w=int(input("Input the width of rectangle: "))
area=l*w
print("The area of rectangle is: ", area)
elif(choice == 3):
b=int(input("Input the base of triangle: "))
h=int(input("Input the height of triangle: "))
area=0.5*b*h
print("The area of rectangle is: ", area)
elif(choice == 4):
s=int(input("Input the side of square: "))
area=s*s
print("The area of rectangle is: ", area)
else:
print("please choose from the given options.")
Program to calculate are of different shapes in Java language
import java.util.*;
public class calc {
public static void main(String[] args) {
int choice,r,l,w,b,h,s;
double area;
System.out.println("Input 1 for area of circle");
System.out.println("Input 2 for area of rectangle");
System.out.println("Input 3 for area of triangle");
System.out.println("Input 4 for area of square");
Scanner sc=new Scanner(System.in);
System.out.println("Input your choice : ");
choice = sc.nextInt();
switch(choice)
{
case 1:
System.out.println("Input radious of the circle : ");
r=sc.nextInt();
area=3.14*r*r;
System.out.println("The area is : "+area);
break;
case 2:
System.out.println("Input length and width of the rectangle : ");
l=sc.nextInt();
w=sc.nextInt();
area=l*w;
System.out.println("The area is :"+area);
break;
case 3:
System.out.println("Input the base and hight of the triangle :");
b=sc.nextInt();
h=sc.nextInt();
area=.5*b*h;
System.out.println("The area is :"+area);
break;
case 4:
System.out.println("Input the side of the square :");
s=sc.nextInt();
area=s*s;
System.out.println("The area is :"+area);
break;
default:
System.out.println("please choose from the given options.");
break;
}
}
}