Compute Simple Interest in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to compute simple interest in C programming. The lab covers the step-by-step process of reading the principal amount, interest rate, and time period from the user, and then calculating the simple interest using the formula: Interest = Principal _ Rate _ Time. The program will then display the calculated interest amount.

The lab provides a clear and concise approach to understanding the fundamental concepts of financial math and interest calculations using C programming. By the end of this lab, you will have a solid understanding of how to implement simple interest calculations in C and apply these skills to more complex financial applications.


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-435352{{"`Compute Simple Interest in C`"}} c/variables -.-> lab-435352{{"`Compute Simple Interest in C`"}} c/user_input -.-> lab-435352{{"`Compute Simple Interest in C`"}} c/math_functions -.-> lab-435352{{"`Compute Simple Interest in C`"}} end

Read Principal, Rate, Time

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

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

cd ~/project
nano simple_interest.c

Now, enter the following code to read the principal, rate, and time:

#include <stdio.h>

int main() {
    float principal, rate, time;

    // Prompt user to input principal amount
    printf("Enter the principal amount: ");
    scanf("%f", &principal);

    // Prompt user to input interest rate
    printf("Enter the annual interest rate (%): ");
    scanf("%f", &rate);

    // Prompt user to input time period
    printf("Enter the time period (in years): ");
    scanf("%f", &time);

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

    return 0;
}

Compile and run the program:

gcc simple_interest.c -o simple_interest
./simple_interest

Example output:

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

Input Values:
Principal: $1000.00
Interest Rate: 5.00%
Time Period: 2.00 years
Explanation
  • We use float data type to store decimal values for principal, rate, and time
  • printf() is used to display prompts and instructions to the user
  • scanf() reads user input for each variable
  • %.2f format specifier displays floating-point numbers with 2 decimal places

Compute Interest = PRT

In this step, you will learn how to calculate simple interest using the formula: Interest = Principal _ Rate _ Time. We'll modify the previous program to compute the interest amount.

Open the existing C file:

cd ~/project
nano simple_interest.c

Update the program with the interest calculation:

#include <stdio.h>

int main() {
    float principal, rate, time, interest;

    // Prompt user to input principal amount
    printf("Enter the principal amount: ");
    scanf("%f", &principal);

    // Prompt user to input interest rate
    printf("Enter the annual interest rate (%): ");
    scanf("%f", &rate);

    // Prompt user to input time period
    printf("Enter the time period (in years): ");
    scanf("%f", &time);

    // Calculate simple interest
    interest = principal * (rate / 100) * time;

    // Print the input values and calculated interest
    printf("\nInput Values:\n");
    printf("Principal: $%.2f\n", principal);
    printf("Interest Rate: %.2f%%\n", rate);
    printf("Time Period: %.2f years\n", time);
    printf("\nCalculated Simple Interest: $%.2f\n", interest);

    return 0;
}

Compile and run the updated program:

gcc simple_interest.c -o simple_interest
./simple_interest

Example output:

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

Input Values:
Principal: $1000.00
Interest Rate: 5.00%
Time Period: 2.00 years

Calculated Simple Interest: $100.00
Explanation
  • We added a new variable interest to store the calculated simple interest
  • The simple interest formula is: Interest = Principal _ (Rate/100) _ Time
  • We divide the rate by 100 to convert percentage to decimal
  • The calculated interest is printed with two decimal places

Print the Interest

In this step, you will learn how to format and print the calculated simple interest with clear, professional output. We'll enhance the previous program to provide a more comprehensive financial calculation display.

Open the existing C file:

cd ~/project
nano simple_interest.c

Update the program with improved interest printing:

#include <stdio.h>

int main() {
    float principal, rate, time, interest;

    // Prompt user to input principal amount
    printf("Simple Interest Calculator\n");
    printf("-------------------------\n");
    printf("Enter the principal amount: ");
    scanf("%f", &principal);

    // Prompt user to input interest rate
    printf("Enter the annual interest rate (%): ");
    scanf("%f", &rate);

    // Prompt user to input time period
    printf("Enter the time period (in years): ");
    scanf("%f", &time);

    // Calculate simple interest
    interest = principal * (rate / 100) * time;

    // Print detailed financial summary
    printf("\n--- Financial Calculation Summary ---\n");
    printf("Principal Amount:     $%10.2f\n", principal);
    printf("Annual Interest Rate: %10.2f%%\n", rate);
    printf("Time Period:          %10.2f years\n", time);
    printf("Total Simple Interest:$%10.2f\n", interest);
    printf("Total Amount:         $%10.2f\n", principal + interest);

    return 0;
}

Compile and run the updated program:

gcc simple_interest.c -o simple_interest
./simple_interest

Example output:

Simple Interest Calculator
-------------------------
Enter the principal amount: 1000
Enter the annual interest rate (%): 5
Enter the time period (in years): 2

--- Financial Calculation Summary ---
Principal Amount:     $    1000.00
Annual Interest Rate:        5.00%
Time Period:               2.00 years
Total Simple Interest:$     100.00
Total Amount:         $    1100.00
Explanation
  • Added a title and separator for better user experience
  • Used %10.2f format specifier to align decimal numbers
  • Displayed additional information like total amount
  • Improved readability of financial calculation output

Summary

In this lab, you will learn how to read the principal, rate, and time from user input, and then calculate the simple interest using the formula: Interest = Principal _ Rate _ Time. You will also learn how to print the computed interest value. The key steps include prompting the user for the financial parameters, performing the interest calculation, and displaying the final result.

The program first reads the principal amount, annual interest rate, and time period from the user. It then computes the simple interest using the provided formula and prints the result. This lab covers the fundamental concepts of input/output, arithmetic operations, and output formatting in the C programming language.

Other C Tutorials you may like