C Program To Calculate Amount In Compound Interest


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



YouTube Link: https://www.youtube.com/watch?v=7q8yFP6S3MA [Watch the Video In Full Screen.]


Logic To Find Compounded Amount

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.

Source Code: C Program To Calculate Amount In Compound Interest

  1.    
  2. #include<stdio.h>  
  3. #include<math.h>  
  4.   
  5. int main()  
  6. {  
  7.     float p, n, r, q, a;  
  8.     int count;  
  9.   
  10.     for(count = 1; count <= 10; count++)  
  11.     {  
  12.         printf("Enter principal amount\n");  
  13.         scanf("%f", &p);  
  14.   
  15.         printf("Enter number of years\n");  
  16.         scanf("%f", &n);  
  17.   
  18.         printf("Enter rate of interest\n");  
  19.         scanf("%f", &r);  
  20.   
  21.         r = r / 100;  
  22.   
  23.         printf("Enter no of times you compound per year\n");  
  24.         scanf("%f", &q);  
  25.   
  26.         a = p * pow( (1 + (r/q)), n * q );  
  27.   
  28.         printf("Compounded amount is %f\n\n", a);  
  29.     }  
  30.   
  31.     return 0;  
  32. }  

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

Leave a Reply

Your email address will not be published. Required fields are marked *