1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <iostream> using namespace std; int main() { double principal, rate, time, simpleInterest; int choice; // Input principal, rate, and time cout << "Enter the principal amount: "; cin >> principal; cout << "Enter the rate of interest: "; cin >> rate; cout << "Enter the time period in years: "; cin >> time; // Display calculation options cout << "Choose the calculation method:" << endl; cout << "1. Calculate Simple Interest" << endl; cout << "2. Calculate Total Amount (Principal + Simple Interest)" << endl; cout << "Enter your choice (1-2): "; cin >> choice; // Calculate simple interest using a switch statement switch (choice) { case 1: simpleInterest = (principal * rate * time) / 100; cout << "The Simple Interest is: $" << simpleInterest << endl; break; case 2: simpleInterest = (principal * rate * time) / 100; cout << "The Total Amount after " << time << " years is: $" << principal + simpleInterest << endl; break; default: cout << "Invalid choice! Please enter 1 or 2." << endl; break; } return 0; } |
Explanation:
- Input:
- The program prompts the user to enter the principal amount, rate of interest, and time period in years.
- Switch Statement:
- The
switch
statement checks the value ofchoice
:case 1
: Calculates and displays the simple interest using the formula:case 2
: Calculates the simple interest and then adds it to the principal to display the total amount after the specified time period.default
: Handles invalid choices by displaying an error message.
- The
- Output:
- Depending on the user’s choice, the program either displays the simple interest or the total amount after the interest is added to the principal.
Example Outputs:
Example 1: Simple Interest Calculation
Input:
- Principal:
1000
- Rate:
5
- Time:
3
- Choice:
1
Output:
1 2 3 4 5 6 7 8 |
Enter the principal amount: 1000 Enter the rate of interest: 5 Enter the time period in years: 3 Choose the calculation method: 1. Calculate Simple Interest 2. Calculate Total Amount (Principal + Simple Interest) Enter your choice (1-2): 1 The Simple Interest is: $150 |
Example 2: Total Amount Calculation
Input:
- Principal:
2000
- Rate:
4
- Time:
2
- Choice:
2
Output:
1 2 3 4 5 6 7 8 |
Enter the principal amount: 2000 Enter the rate of interest: 4 Enter the time period in years: 2 Choose the calculation method: 1. Calculate Simple Interest 2. Calculate Total Amount (Principal + Simple Interest) Enter your choice (1-2): 2 The Total Amount after 2 years is: $2160 |
Example 3: Invalid Choice
Input:
- Principal:
1500
- Rate:
6
- Time:
1
- Choice:
3
Output:
1 2 3 4 5 6 7 8 |
Enter the principal amount: 1500 Enter the rate of interest: 6 Enter the time period in years: 1 Choose the calculation method: 1. Calculate Simple Interest 2. Calculate Total Amount (Principal + Simple Interest) Enter your choice (1-2): 3 Invalid choice! Please enter 1 or 2. |