Complementary task for topic: 1
M Nemeth · 2023-08-29 15:21:04.604221'
scanf: double 3.
scanf: double 3.
Task: Calculate the compound interest:
Write a C program that prompts the user to enter the principal amount, interest rate (in percentage), and time period (in years) using scanf(), and calculates the compound interest. Display the compound interest using printf().
The formula to calculate the compound interest is:
compoundInterest = principal * (1 + (interestRate / 100))^timePeriod - principal
where principal is the initial amount, interestRate is the annual interest rate, and timePeriod is the time period in years.
Hint: You need to use %lf to scan double precision floats.
You have to use this time the pow() function in math.h (so include it!)
pow(a,b) gives a^b
Solution
#include
#include
int main() {
double principal, interestRate, timePeriod;
printf("Enter the principal amount: ");
scanf("%lf", &principal);
printf("Enter the interest rate (in percentage): ");
scanf("%lf", &interestRate);
printf("Enter the time period (in years): ");
scanf("%lf", &timePeriod);
double compoundInterest = principal * (pow(1 + (interestRate / 100), timePeriod)) - principal;
printf("The compound interest is %.2lf.\n", compoundInterest);
return 0;
}
Explanation
In this example, the program uses printf() to prompt the user to enter the principal amount, interest rate (in percentage), and time period (in years). The scanf() function is used to scan the input from the user and store it in the principal, interestRate, and timePeriod variables. The program calculates the compound interest using the provided formula, which involves raising (1 + (interestRate / 100)) to the power of timePeriod using the pow() function from the math.h library. The compound interest is stored in the compoundInterest variable. Finally, it uses printf() to display the compound interest in the specified format.