9. Develop a program in C to calculate the area of a triangle or rectangle based on user input using 'switch-case'.
Objective:
The objective of this program is to calculate the area of either a triangle or a rectangle based on user input and display the result.
Agenda:
1. Prompt the user to choose between calculating the area of a triangle or a rectangle.
2. Based on the user's choice, take the necessary input (dimensions) from the user.
3. Calculate the area using the appropriate formula.
4. Display the calculated area to the user.
Purpose:
The purpose of this program is to demonstrate the use of switch-case statements in C programming and to calculate the area of geometric shapes based on user input. It also serves as a simple example of how to handle different cases efficiently using switch-case.
Real-Life Applications:
Calculating the area of geometric shapes like triangles and rectangles is a common task in various fields such as architecture, engineering, and computer graphics. This program can be a building block for more complex applications that involve geometric calculations.
write a C program to calculate the area of a triangle or rectangle based on user input using 'switch-case'.
/* a program in C to calculate the area of a triangle
or rectangle based on user input using 'switch-case'. */
#include <stdio.h>
int main()
{
int choice;
float base, height, length, width, area;
printf("Choose a shape to calculate the area:\n");
printf("1. Triangle\n");
printf("2. Rectangle\n");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter the base and height of the triangle: ");
scanf("%f %f", &base, &height);
area = 0.5*base*height;
printf("The area of the triangle is: %.2f\n", area);
break;
case 2:
printf("Enter the length and width of the rectangle: ");
scanf("%f %f", &length, &width);
area = length * width;
printf("The area of the rectangle is: %.2f\n", area);
break;
default:
printf("Invalid choice. Please choose 1 or 2.\n");
}
return 0;
}
Output:
Choose a shape to calculate the area:
1. Triangle
2. Rectangle;
1
Enter the base and height of the triangle:
20
20
The area of the triangle is: 200
Explanation
In this program, we first prompt the user to choose between calculating the area of a triangle or a rectangle. Based on the user's input, we use a switch-case statement to perform the corresponding calculations.
If the user chooses 1 (triangle), the program prompts the user to enter the base and height of the triangle, calculates the area using the formula (0.5 base height), and then displays the result.
If the user chooses 2 (rectangle), the program prompts the user to enter the length and width of the rectangle, calculates the area using the formula (length * width), and then displays the result.
If the user enters any other choice, the program displays an error message indicating an invalid choice.
This program is a simple demonstration of how you can use a switch-case statement in C to calculate the area of different shapes based on user input. Feel free to modify and expand upon this code as needed for your specific requirements.
Previous :-->> 8.Create a C program to determine the day of the week based on the input number using switch-case.
-->> NEXT:
10. Write a program in C to check if a year is a leap year using 'if-else' statements.
-->>ALL Conditionl statements Programs