Compute Compound Interest in C

CCBeginner
Practice Now

Introduction


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435342{{"`Compute Compound Interest in C`"}} c/variables -.-> lab-435342{{"`Compute Compound Interest in C`"}} c/user_input -.-> lab-435342{{"`Compute Compound Interest in C`"}} c/math_functions -.-> lab-435342{{"`Compute Compound Interest in C`"}} end

Read Principal, Rate, Time

In this step, you will learn how to read the principal amount, interest rate, and time period for calculating compound interest in a C program. We'll create a simple program that takes user input for these financial parameters.

First, let's create a new C file in the project directory:

cd ~/project
nano compound_interest.c

Now, add the following code to read the input parameters:

#include <stdio.h>

int main() {
    // Declare variables for principal, rate, and time
    float principal, rate, time;

    // Prompt and read principal amount
    printf("Enter the principal amount: ");
    scanf("%f", &principal);

    // Prompt and read interest rate
    printf("Enter the annual interest rate (in percentage): ");
    scanf("%f", &rate);

    // Prompt and read time period
    printf("Enter the time period (in years): ");
    scanf("%f", &time);

    // Print the input values to verify
    printf("\nInput Parameters:\n");
    printf("Principal Amount: $%.2f\n", principal);
    printf("Annual Interest Rate: %.2f%%\n", rate);
    printf("Time Period: %.2f years\n", time);

    return 0;
}

Compile and run the program:

gcc compound_interest.c -o compound_interest
./compound_interest

Example output:

Enter the principal amount: 1000
Enter the annual interest rate (in percentage): 5
Enter the time period (in years): 3

Input Parameters:
Principal Amount: $1000.00
Annual Interest Rate: 5.00%
Time Period: 3.00 years
Explanation
  • We use float variables to store decimal values for financial calculations
  • printf() is used to display prompts and instructions to the user
  • scanf() reads user input for principal, rate, and time
  • %.2f format specifier displays numbers with two decimal places

Compute Amount = P*(1+R)^T

In this step, you will learn how to calculate the compound interest using the mathematical formula A = P * (1 + R)^T, where:

  • A = Final Amount
  • P = Principal Amount
  • R = Annual Interest Rate (in decimal)
  • T = Time Period (in years)

Let's modify the previous program to include the compound interest calculation:

cd ~/project
nano compound_interest.c

Update the program with the compound interest calculation:

#include <stdio.h>
#include <math.h>

int main() {
    // Declare variables for principal, rate, time, and amount
    float principal, rate, time, amount;

    // Prompt and read principal amount
    printf("Enter the principal amount: ");
    scanf("%f", &principal);

    // Prompt and read annual interest rate
    printf("Enter the annual interest rate (in percentage): ");
    scanf("%f", &rate);

    // Prompt and read time period
    printf("Enter the time period (in years): ");
    scanf("%f", &time);

    // Convert rate to decimal
    rate = rate / 100;

    // Calculate compound interest
    amount = principal * pow(1 + rate, time);

    // Print the results
    printf("\nInput Parameters:\n");
    printf("Principal Amount: $%.2f\n", principal);
    printf("Annual Interest Rate: %.2f%%\n", rate * 100);
    printf("Time Period: %.2f years\n", time);
    printf("\nCompound Interest Calculation:\n");
    printf("Final Amount: $%.2f\n", amount);
    printf("Total Interest Earned: $%.2f\n", amount - principal);

    return 0;
}

Compile the program with the math library:

gcc compound_interest.c -o compound_interest -lm

Run the program:

./compound_interest

Example output:

Enter the principal amount: 1000
Enter the annual interest rate (in percentage): 5
Enter the time period (in years): 3

Input Parameters:
Principal Amount: $1000.00
Annual Interest Rate: 5.00%
Time Period: 3.00 years

Compound Interest Calculation:
Final Amount: $1157.63
Total Interest Earned: $157.63
Explanation
  • #include <math.h> is added to use the pow() function for exponentiation
  • rate = rate / 100 converts percentage to decimal
  • amount = principal * pow(1 + rate, time) calculates the final amount
  • -lm flag is used when compiling to link the math library

Print Final Amount

In this final step, you will enhance the compound interest calculation program by adding formatted output and improving the presentation of financial results.

Let's modify the previous program to improve result formatting:

cd ~/project
nano compound_interest.c

Update the program with enhanced output formatting:

#include <stdio.h>
#include <math.h>

int main() {
    // Declare variables for principal, rate, time, and amount
    float principal, rate, time, amount, interest;

    // Prompt and read principal amount
    printf("Compound Interest Calculator\n");
    printf("----------------------------\n");
    printf("Enter the principal amount: ");
    scanf("%f", &principal);

    // Prompt and read annual interest rate
    printf("Enter the annual interest rate (in percentage): ");
    scanf("%f", &rate);

    // Prompt and read time period
    printf("Enter the time period (in years): ");
    scanf("%f", &time);

    // Convert rate to decimal
    rate = rate / 100;

    // Calculate compound interest
    amount = principal * pow(1 + rate, time);
    interest = amount - principal;

    // Print formatted financial results
    printf("\n===== Financial Calculation Results =====\n");
    printf("Initial Principal:     $%10.2f\n", principal);
    printf("Annual Interest Rate:  %10.2f%%\n", rate * 100);
    printf("Investment Period:     %10.2f years\n", time);
    printf("-------------------------------------------\n");
    printf("Final Amount:          $%10.2f\n", amount);
    printf("Total Interest Earned: $%10.2f\n", interest);
    printf("==========================================\n");

    return 0;
}

Compile the program:

gcc compound_interest.c -o compound_interest -lm

Run the program:

./compound_interest

Example output:

Compound Interest Calculator
----------------------------
Enter the principal amount: 1000
Enter the annual interest rate (in percentage): 5
Enter the time period (in years): 3

===== Financial Calculation Results =====
Initial Principal:     $   1000.00
Annual Interest Rate:         5.00%
Investment Period:          3.00 years
-------------------------------------------
Final Amount:          $   1157.63
Total Interest Earned: $    157.63
==========================================
Explanation
  • Added more descriptive headers and separators
  • Used %10.2f format specifier for aligned, fixed-width decimal output
  • Calculated interest separately for clearer presentation
  • Included a title and structured financial results display

Summary

In this lab, you learned how to read the principal amount, interest rate, and time period for calculating compound interest in a C program. You created a simple program that prompts the user for these financial parameters and prints the input values to verify. Next, you will learn how to calculate the compound interest using the mathematical formula A = P * (1 + R)^T, where A is the final amount, P is the principal amount, R is the interest rate, and T is the time period.

Other C Tutorials you may like