When interest compounds q times per year at an annual rate of r % for n years, the principal p compounds to an amount a as per the following formula:
a = p (1 + r / q) nq
Write a C Program to read 10 sets of p, r, n & q and calculate the corresponding a‘s.
Related Read:
Basic Arithmetic Operations In C
For Loop In C Programming Language
C Program to Calculate the Compound Interest
Video Tutorial: C Program To Calculate Amount In Compound Interest
We ask the user to enter values for floating point variables p, n, r and q. We use the formula:
a = p (1 + r / q) nq
And display the result to the console window.
where,
p – principal amount;
n – number of years.
r – rate of interest;
q – number of times interest compounds per year.
Note: We need to convert the user entered interest into percentage by dividing the integer number by 100.
#include<stdio.h>
#include<math.h>
int main()
{
float p, n, r, q, a;
int count;
for(count = 1; count <= 10; count++)
{
printf("Enter principal amount\n");
scanf("%f", &p);
printf("Enter number of years\n");
scanf("%f", &n);
printf("Enter rate of interest\n");
scanf("%f", &r);
r = r / 100;
printf("Enter no of times you compound per year\n");
scanf("%f", &q);
a = p * pow( (1 + (r/q)), n * q );
printf("Compounded amount is %f\n\n", a);
}
return 0;
}
Output
Enter principal amount
5000
Enter number of years
2
Enter rate of interest
8
Enter no of times you compound per year
4
Compounded amount is 5858.296875
Enter principal amount
1000
Enter number of years
7
Enter rate of interest
5
Enter no of times you compound per year
1
Compounded amount is 1407.100464
Enter principal amount
2000
Enter number of years
5
Enter rate of interest
12
Enter no of times you compound per year
12
Compounded amount is 3633.393311
Enter principal amount
5000
Enter number of years
5
Enter rate of interest
5
Enter no of times you compound per year
5
Compounded amount is 6412.160156
Enter principal amount
Note: Above program keeps on iterating 10 times and asks the input 10 times.
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert