Back

Simple Interest Program In C

06 Dec 2024
5 min read

C is a versatile programming language known for its robust capacity to write everything from scratch. It has an extensive library of built-in functions, data types, and operators. Hence, it enables developers to create everything from basic to highly complex programs. Solving problems like calculating simple interest is a very good way to demonstrate the practical use of C. It also helps strengthen foundational coding skills essential for any programmer. In this article, we will learn all about simple interest program in c and how to write good code for it.

What is Simple Interest?

The amount charged for a financial tool called a ‘loan’ besides its principal amount within a certain period is known as simple interest. It is a calculation in mathematics and in finance when money is involved, both borrowing and lending. In loans whenever a person gets a loan from a particular lender, the initial amount which he or she gets is called principal. They must pay off the principal over time and, in addition, a certain amount, which is referred to as simple interest. This interest is obtained from the principal, the time horizon of the loan and the contracted rate of interest. In other words, simple interest is also known as the price that is paid for borrowing other people’s money. Its formula is given by: 

Simple Interest (SI): (Principle x Time x Rate of interest) / 100

Algorithm to Calculate Simple Interest in C Program

The algorithm for a simple interest program has the following structure: 

  • Begin by writing the code structure in C.
  • Create variables to store the principal amount (P), rate of interest (R), time period (T), and simple interest (SI).
  • Use input functions to get values for the principal amount, rate of interest, and time period from the user.
  • Apply the formula of simple interest. Perform calculations and store results in the SI variable. 
  • Print the calculated simple interest using an output function.
  • Complete the program with a return statement or an end message.

C Program to Calculate Simple Interest

Now, let’s take a look at the program to calculate simple interest in C. There are many ways to write the code. Here, we’ll look at two examples: 

Simple Interest Program in C Using Function

In this program, simple interest is calculated using a separate function. The function simple_interest takes three parameters: principal (P), rate of interest (R), and time (T). It computes the simple interest using the formula SI= (PxRXT)/100 and returns the result.

In the main function you have to input values for P, R, and T. These are passed to the simple_interest function. The returned value is stored in SI and displayed.

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

double simple_interest(int P, int R, int T) {
    return (P * R * T) / 100.0; // Ensure division by 100.0 for correct float result
}

int main() {
    int P, R, T;
    printf("Enter the principal amount\n");
    scanf("%d", &P);
    printf("Enter the rate of interest\n");
    scanf("%d", &R);
    printf("Enter the time\n");
    scanf("%d", &T);

    double SI = simple_interest(P, R, T);
    printf("The Simple Interest = %.2lf\n", SI); // Use %.2lf for 2 decimal places
    return 0;
}

Simple Interest Program in C Using While Loop 

This is a program to calculate simple interest using a while loop. You have to input three values: principal amount, time, and rate of interest. A variable x is initialised to 1 and iteratively multiplied by each input value (y) within the loop. Once all three inputs are processed, the simple interest is computed using the formula SI= (x/100). Here x already holds the product of the inputs. Finally, the calculated simple interest is displayed.

// C Program to Calculate Simple Interest using While loop
#include <stdio.h>

int main() {
    float x = 1, y, SI;
    // `SI` = value of the simple interest

    printf("Enter the principal (amount), time, and rate::\n");

    int count = 1;
    while (count <= 3) {
        scanf("%f", &y);

        // It will calculate the value of simple interest
        x *= y;
        if (count == 3)
            SI = x/100;

        count++;
    }

    // It will produce the final output
    printf("\nSimple Interest = %.2f\n", SI);
    return 0;
}

Conclusion

Understanding how to calculate simple interest from the point of programming is a fundamental skill for software developers. It helps you grasp essential concepts of financial computations and apply logic to solve real-world problems. This program is a simple example of basic programming that introduces developers to using variables, formulas, and input-output functions in C. You can further practice writing code for simple interest programs in c using different loops, pointers and functions. It’s important to practice different ways to write code and become proficient at it. 

If you are looking forward to a successful career in software development, you will have to build a strong foundation and work toward it throughout your college years. Enroll into the CCBP 4.0 Academy to get started on your journey as a software professional.  

Frequently Asked Questions

1. How do you write a program to calculate simple interest in C?

To calculate simple interest in C, you need to input the principal, rate, and time, then use the formula SI = (PxRxT) / 100. The program should output the calculated interest.

2. Why should you use functions in a simple interest program in C?

Using functions to write program to find simple interest makes the code modular and reusable. It helps separate the logic of calculating interest from the rest of the program. Hence it improves readability and allows you to call the function multiple times.

3. What is the role of data types like float and double in simple interest calculations?

In C, float or double data types are used to store decimal values for the principal, rate, and time. Using these is necessary so fractional interest rates are accurate.

4. Can you use a while loop to calculate simple interest in C?

Yes, you can use a while loop to repeatedly take inputs like principal, rate, and time. Then, use it to calculate the simple interest. 

5. How can you handle invalid input in a C program for calculating simple interest?

You can use input validation techniques like if conditions or while loops to ensure that the user enters valid numeric values.

Read More Articles

Chat with us
Chat with us
Talk to career expert