**Problem Statement **

We need to write a program in C, C++, and Python, that is capable of returning the area of a circle, rectangle, and triangle based on the user choice and input.

First, the user has the choice to select the geometry for which he/she wants to calculate the area. Then based on the geometry shape selected asks the user for the sides or radius of the shape and calculate its area.

To calculate the areas of Circle, Rectangle, and Triangle we will use the following formulas.

area of circle = `π r `

^{2}

area of the rectangle = `width*length`

area of a triangle(with three sides given a, and c) = `√[s(s-a)(s-b)(s-c)]`

, where s = (a+b+c)/2

**C Program to calculate the area of a circle, a rectangle, or a triangle depending upon user’s Choice**

#include <stdio.h> #include <math.h> int main() { float area, radius, width, length, 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 "); //input the geometry shape scanf("%d", &choice); //for circle if(choice==1) { printf("Enter the radius of the Circle: "); scanf("%f", &radius); area = M_PI*radius*radius; printf("The area of the circle with radius %f is %.2f", radius, area); } //for rectangle if(choice ==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); } //area of triangle using heron's formula if(choice ==3) { printf("Enter the 3 sides of 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); } return 0; }

**Output**

**********Choose an Option:********* 1: Area of the Circle 2: Area of the Rectangle 3: Area of the Triangle 3 Enter the 3 sides of triangle eg (12 13 14): 3 7 6 The area of the triangle is 8.94

**C++ Program to calculate the area of a circle, a rectangle, or a triangle depending upon user’s Choice**

#include #include using namespace std; int main() { float area, radius, width, length, 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 "; //input the geometry shape cin>>choice; //for circle if(choice==1) { cout<<"Enter the radius of the Circle: "; cin>>radius; area = M_PI*radius*radius; cout<<"The area of the circle with radius "<< radius<<" is "<< area; } //for rectangle if(choice ==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; } //area of triangle using heron's formula if(choice ==3) { cout<<"Enter the 3 sides of 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; } return 0; }

**Output**

**********Choose an Option:********* 1: Area of the Circle 2: Area of the Rectangle 3: Area of the Triangle 2 Enter the Width of the rectangle: 12 Enter the length of the rectangle: 6 The area of the rectangle is 72

**Python Program to calculate the area of a circle, a rectangle, or a triangle depending upon user’s Choice**

import math print("**********Choose an Option:********* "); print("1: Area of the Circle"); print("2: Area of the Rectangle "); print("3: Area of the Triangle "); # input the geometry shape choice = int(input("")) # area of the 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 the rectangle if 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 ractangle is {round(area,2)}") # area of triangle using heron's formula if 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)}")

**Output**

**********Choose an Option:********* 1: Area of the Circle 2: Area of the Rectangle 3: Area of the Triangle 1 Enter the radius of the Circle: 12 The area of the circle with radius 12.0 is : 452.39

**Wrapping UP!**

In this programming tutorial, we discussed how to write a program in C, C++, and Python to find out the area of a Circle, Rectangle or Triangle. The program is quite simple and works on the user’s choice.

To calculate the circle area it asks the user to enter the radius of the circle. For the rectangle, it asks for width and length. And for the triangle, it accepts the lengths of three sides.

**People are also reading:**

- WAP to print the truth table for XY+Z
- WAP to create a loading bar
- WAP to Print the Following Pattern
- WAP to Print the Following Triangle
- WAP to print three numbers in descending order
- WAP to calculate sum and average of three numbers
- WAP to print given series:1 2 4 8 16 32 64 128
- WAP to find quotient and remainder of two numbers
- WAP to Find Cube of a Number using Functions
- WAP to convert a lowercase alphabet to uppercase or vice-versa using ASCII code