C Program to Calculate Simple Interest

Simple interest is a fundamental concept in finance and is often one of the first programming exercises for beginners learning the C language. This article will guide you through three different ways to write a C program to calculate simple interest, offering detailed explanations for each method. You may also like our article on Compound Interest.

Write a C Program to Calculate Simple Interest

What is Simple Interest?

Simple interest is calculated using the formula:

Simple Interest = (P×R×T​)/100

Where:

  • P is the principal amount.
  • R is the annual interest rate.
  • T is the time the money is invested or borrowed for, in years.

Now, let’s delve into the three different methods of implementing this formula in C.

Method 1) C Program to Calculate Simple Interest using Basic Calculation Using Standard Input / Output

The first method involves writing a straightforward C program that prompts the user to enter the principal amount, rate of interest, and time period. It then calculates the simple interest using the formula provided above.

Code Example

#include <stdio.h>

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

    // Taking input from the user
    printf("Enter the principal amount: ");
    scanf("%f", &principal);
    printf("Enter the rate of interest: ");
    scanf("%f", &rate);
    printf("Enter the time period (in years): ");
    scanf("%f", &time);

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

    // Displaying the result
    printf("The Simple Interest is: %.2f\n", simpleInterest);

    return 0;
}

Explanation of the Code

  1. Input Handling: The program uses scanf to read the principal, rate, and time from the user.
  2. Calculation: It applies the simple interest formula.
  3. Output: The calculated simple interest is printed to the console using printf.

Input:

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

Output:

The Simple Interest is: 100.00

Explanation:

The principal amount is $1000, the rate of interest is 5%, and the time period is 2 years.

Simple Interest = (1000×5×2)/100 = 100.

Method 2) C Program to Calculate Simple Interest using Functions

This method improves code readability and reusability by using a function to calculate simple interest.

Code Example

#include <stdio.h>

// Function declaration
float calculateSimpleInterest(float principal, float rate, float time);

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

    // Taking input from the user
    printf("Enter the principal amount: ");
    scanf("%f", &principal);
    printf("Enter the rate of interest: ");
    scanf("%f", &rate);
    printf("Enter the time period (in years): ");
    scanf("%f", &time);

    // Function call to calculate simple interest
    simpleInterest = calculateSimpleInterest(principal, rate, time);

    // Displaying the result
    printf("The Simple Interest is: %.2f\n", simpleInterest);

    return 0;
}

// Function definition
float calculateSimpleInterest(float principal, float rate, float time) {
    return (principal * rate * time) / 100;
}

Explanation of the Code

  1. Function Declaration and Definition: The calculateSimpleInterest function encapsulates the calculation logic.
  2. Main Function: The main function handles user input and output, and calls the calculation function.
  3. Reusability: By using a function, the calculation logic can be reused without duplicating code.

Input:

Enter the principal amount: 2000
Enter the rate of interest: 3.5
Enter the time period (in years): 4

Output:

The Simple Interest is: 280.00

Explanation:

The principal amount is 2000, the rate of interest is 3.5%, and the time period is 4 years.

Simple Interest = (2000×3.5×4)/100 = 280

Method 3) C Program to Calculate Simple Interest using Command-Line Arguments

The third method demonstrates how to pass input parameters via the command line, which can be useful for automation and testing.

Code Example

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    if (argc != 4) {
        printf("Usage: %s <principal> <rate> <time>\n", argv[0]);
        return 1;
    }

    // Converting command-line arguments to floats
    float principal = atof(argv[1]);
    float rate = atof(argv[2]);
    float time = atof(argv[3]);
    float simpleInterest;

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

    // Displaying the result
    printf("The Simple Interest is: %.2f\n", simpleInterest);

    return 0;
}

Explanation of the Code

  1. Command-Line Arguments: The program checks if the correct number of arguments is provided. If not, it prints a usage message and exits.
  2. Conversion and Calculation: It converts the command-line arguments from strings to floats using atof and calculates the simple interest.
  3. Output: The result is displayed on the console.

Command:

./program_name 1500 4.5 3

Output:

The Simple Interest is: 202.50

Explanation:

The principal amount is $1500, the rate of interest is 4.5%, and the time period is 3 years.

Simple Interest = (1500×4.5×3)/100 = 202.50

These three methods showcase different ways to calculate simple interest in C. The basic input/output method is ideal for beginners. Using functions enhances code readability and reusability. Command-line arguments offer a flexible approach for more advanced users. By understanding and practicing these methods, you can strengthen your C programming skills and apply them to various problems efficiently.

Categories C

Leave a Comment