🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Calculating the area of basic geometric shapes is a fundamental concept in geometry. In this blog post, we will explore a C++ program that allows users to calculate the area of various shapes like circles, rectangles, and triangles.
2. Program Overview
Our program will:
1. Display a menu to the user to choose the shape.
2. Based on the choice, prompt the user for the necessary dimensions.
3. Calculate the area of the chosen shape.
4. Display the calculated area.
3. Code Program
#include<iostream>
#include<cmath> // For using M_PI for value of pi
using namespace std;
int main() {
int choice;
float area;
cout << "Choose a shape:" << endl;
cout << "1. Circle" << endl;
cout << "2. Rectangle" << endl;
cout << "3. Triangle" << endl;
cin >> choice;
switch(choice) {
case 1: {
float radius;
cout << "Enter radius of the circle: ";
cin >> radius;
area = M_PI * radius * radius;
cout << "Area of the circle: " << area << endl;
break;
}
case 2: {
float length, breadth;
cout << "Enter length and breadth of the rectangle: ";
cin >> length >> breadth;
area = length * breadth;
cout << "Area of the rectangle: " << area << endl;
break;
}
case 3: {
float base, height;
cout << "Enter base and height of the triangle: ";
cin >> base >> height;
area = 0.5 * base * height;
cout << "Area of the triangle: " << area << endl;
break;
}
default:
cout << "Invalid choice!" << endl;
}
return 0;
}
Output:
Choose a shape: 1. Circle 2. Rectangle 3. Triangle 1 Enter radius of the circle: 5 Area of the circle: 78.5398
4. Step By Step Explanation
1. The program starts by showing a menu to the user, allowing them to choose between three shapes: circle, rectangle, and triangle.
2. Based on the user's choice, it prompts for the necessary dimensions:
- For circle: radius
- For rectangle: length and breadth
- For triangle: base and height
3. It then calculates the area using the formulas:
- Circle: pi * radius * radius
- Rectangle: length * breadth
- Triangle: 0.5 * base * height
4. Finally, it displays the computed area.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment