In this blog post, we will write C, C++, Python, and Java programs to calculate the area of different geometrical shapes based on the user's choice and input. We will have five geometrical shapes: circle, rectangle, triangle, square, and parallelogram. Users can choose the shape and provide the required input fields to calculate its area.
We will use the following formulas to calculate the area of the circle, rectangle, triangle, square, and parallelogram:
- Area of circle = 3.1459* radius*radius
The logic to calculate the area of the circle will be
area = (float)3.14159*radius*radius;
- Area of the rectangle = length* breadth
The logic to calculate the area of the circle will be
area = (float)length*breadth;
- Area of a triangle (with three sides given a, b, and c) = Square root of [s(s-a)(s-b)(s-c)] , where s = (a+b+c)/2
The logic to calculate the area of the circle will be
s = (float)(a+b+c)/2;
area = (float)(sqrt(s*(s-a)*(s-b)*(s-c)));
- Area of a square = side*side
The logic to calculate the area of the circle will be
area = (float)side*side;
- Area of Parallelogram = base*height
The logic to calculate the area of the circle will be:
area = (float)base*height;
C Program
Here is a C program to calculate the area of a circle, rectangle, triangle, square, or parallelogram depending on a user's choice:
#include <stdio.h>
#include <math.h>
int main()
{
float area, radius, width, length, breadth, base, height, side;
float a,b,c,s;
int choice;
printf("**********Choose an Option:*********\n ");
printf("1: Area of the Circle\n ");
printf("2: Area of the Rectangle\n ");
printf("3: Area of the Triangle\n ");
printf("4: Area of the Square\n ");
printf("5: Area of the Parallelogram\n ");
//choose the desired shape
scanf("%d", &choice);
//area of a circle
switch(choice)
{
case 1:
{
printf("Enter the radius of the Circle: ");
scanf("%f", &radius);
area = 3.1459*radius*radius;
printf("The area of the circle is %.2f", area);
break;
}
//area of a rectangle
case 2:
{
printf("Enter the Width of the rectangle: ");
scanf("%f", &width);
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
area = width*length;
printf("The area of the rectangle is %.2f", area);
break;
}
//area of a triangle using Heron's formula
case 3:
{
printf("Enter the 3 sides of a triangle. Eg, (12 13 14): ");
scanf("%f %f %f", &a, &b, &c);
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("The area of the triangle is %.2f", area);
break;
}
//area of a square
case 4:
{
printf("Enter the side of the Square: ");
scanf("%f", &side);
area = side*side;
printf("The area of the square is %.2f", area);
break;
}
//area of a parallelogram
case 5:
{
printf("Enter the base and height of the Parallelogram: ");
scanf("%f %f", &base, &height);
area = base*height;
printf("The area of the parallelogram is %.2f", area);
break;
}
default:
{
printf("Wrong Choice");
break;
}
}
return 0;
}
Output
**********Choose an Option:*********
1: Area of the Circle
2: Area of the Rectangle
3: Area of the Triangle
4: Area of the Square
5: Area of the Parallelogram
5
Enter the base and height of the Parallelogram: 3 4
The area of the parallelogram is 12.00
C++ Program
Here is a C++ program to calculate the area of a rectangle, triangle, square, or parallelogram depending on a user's choice:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float area, radius, width, length, breadth, base, side, height;
float a,b,c,s;
int choice;
cout<<"**********Choose an Option:*********\n ";
cout<<"1: Area of the Circle\n ";
cout<<"2: Area of the Rectangle\n ";
cout<<"3: Area of the Triangle\n ";
cout<<"4: Area of the Square\n ";
cout<<"5: Area of the Parallelogram\n ";
//input the desired shape
cin>>choice;
//area of a circle
switch(choice)
{
case 1:
{
cout<<"Enter the radius of the Circle: ";
cin>>radius;
area = 3.1459*radius*radius;
cout<<"The area of the circle is "<< area;
break;
}
//area of a rectangle
case 2:
{
cout<<"Enter the Width of the rectangle:";
cin>>width;
cout<<"Enter the length of the rectangle: ";
cin>>length;
area = width*length;
cout<<"The area of the rectangle is "<< area;
break;
}
//area of a triangle using Heron's formula
case 3:
{
cout<<"Enter the 3 sides of a triangle. Eg, (12 13 14): ";
cin>>a>>b>>c;
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"The area of the triangle is "<<area;
break;
}
//area of a square
case 4:
{
cout<<"Enter the side of the Square: ";
cin>>side;
area = side*side;
cout<<"The area of the square is "<<area;
break;
}
//area of a parallelogram
case 5:
{
cout<<"Enter the base and height of the Parallelogram: ";
cin>>base>>height;
area = base*height;
cout<<"The area of the parallelogram is "<<area;
break;
}
default:
{
cout<<"Wrong Choice";
break;
}
}
return 0;
}
Output
**********Choose an Option:*********
1: Area of the Circle
2: Area of the Rectangle
3: Area of the Triangle
4: Area of the Square
5: Area of the Parallelogram
3
Enter the 3 sides of a triangle. Eg, (12 13 14): 7 8 6
The area of the triangle is 20.3332
Python Program
Here is a Python program to calculate the area of a rectangle, triangle, square, or parallelogram depending on a user's choice:
import math
print("**********Choose an Option:********* ");
print("1: Area of the Circle");
print("2: Area of the Rectangle ");
print("4: Area of the Triangle ");
print("5: Area of the Square ");
print("6: Area of the Parallelogram ");
# input the desired shape
choice = int(input(""))
# area of a circle
if choice==1:
# input circle radius
radius = float(input("Enter the radius of the Circle: "))
area = math.pi*pow(radius,2)
print(f"The area of the circle with radius {radius} is : {round(area,2)}")
#area of a rectangle
elif choice==2:
width =float(input("Enter the width of the rectangle: "))
length = float(input("Enter the length of the rectangle: "))
area = width*length
print(f"The area of the rectangle is {round(area,2)}")
# area of a triangle using Heron's formula
elif choice ==3:
a, b , c = map(float, input("Enter the 3 sides of triangle eg (12 13 14):").split())
s= (a+b+c)/2
area = math.sqrt(s*(s-a)*(s-b)*(s-c))
print(f"The area of the triangle is {round(area,2)}")
#area of a Square
elif choice ==4:
side =float(input("Enter the side of the square: "))
area = side*side
print(f"The area of the square is {round(area,2)}")
#area of a Parallelogram
elif choice ==5:
base =float(input("Enter the base of the parallelogram: "))
height = float(input("Enter the height of the parallelogram: "))
area = base*height
print(f"The area of the parallelogram is {round(area,2)}")
else:
print("Wrong Choice")
Output
**********Choose an Option:*********
1: Area of the Circle
2: Area of the Rectangle
3: Area of the Triangle
4: Area of the Square
5: Area of the Parallelogram
2
Enter the width of the rectangle: 5
Enter the length of the rectangle: 8
The area of the rectangle is 40.0
Java Program
Here is a Java program to calculate the area of a rectangle, triangle, square, or parallelogram depending on a user's choice:
import java.util.Scanner;
import java.lang.Math;
public class ShapeAreaCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Which shape do you want to calculate the area for?");
System.out.println("1. Circle");
System.out.println("2. Rectangle");
System.out.println("3. Triangle");
System.out.println("4. Square");
System.out.println("5. Parallelogram");
int choice = input.nextInt();
switch (choice) {
//area of a circle
case 1:
System.out.println("Enter the radius of the circle:");
double radius = input.nextDouble();
double circleArea = Math.PI * Math.pow(radius, 2);
System.out.println("The area of the circle is: " + circleArea);
break;
//area of a rectangle
case 2:
System.out.println("Enter the length of the rectangle:");
double length = input.nextDouble();
System.out.println("Enter the width of the rectangle:");
double width = input.nextDouble();
double rectangleArea = length * width;
System.out.println("The area of the rectangle is: " + rectangleArea);
break;
//area of a triangle using Heron's Formula
case 3:
System.out.println("Enter the sides of the triangle Eg. (13 14 15):");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double s;
s = (a+b+c)/2;
double p = Math.sqrt(s * (s-a) * (s-b) * (s-c));
double triangleArea = p;
System.out.println("The area of the triangle is: " + triangleArea);
break;
//area of a square
case 4:
System.out.println("Enter the side of the square:");
double side = input.nextDouble();
double squareArea = side*side;
System.out.println("The area of the square is: " + squareArea);
break;
//area of a parallelogram
case 5:
System.out.println("Enter the base of the parallelogram:");
double base1 = input.nextDouble();
System.out.println("Enter the height of the parallelogram:");
double height1 = input.nextDouble();
double parallelogramArea = 0.5 * base1 * height1;
System.out.println("The area of the triangle is: " + parallelogramArea);
break;
default:
System.out.println("Invalid choice.");
}
}
}
Output
Which shape do you want to calculate the area for?
1. Circle
2. Rectangle
3. Triangle
4. Square
5. Parallelogram
3
Enter the sides of the triangle Eg. (13 14 15):
12 13 14
The area of the triangle is: 72.30793524918272
Wrapping Up!
Here ends our tutorial on writing C, C++, Python, and Java programs to determine the area of a rectangle, triangle, square, or parallelogram. We have used a switch case in all programs and calculated the area of a shape depending on the input provided by users. You can also try without using a switch case.
We hope these programs have helped you understand how to calculate the area of other shapes.
People are also reading:
- Print the truth table for XY+Z
- Create a loading bar
- Print the Following Pattern
- Print the Following Triangle
- Calculate sum and average of three numbers
- Print three numbers in descending order
- Print given series:1 2 4 8 16 32 64 128
- Find quotient and remainder of two numbers
- Find Cube of a Number using Functions
- Convert a lowercase alphabet to uppercase or vice-versa using ASCII code
Leave a Comment on this Post