In this article, we are going to study the simple interest program in C. We will study the formula of simple interest and how we can implement the logic in a C program. So, let’s get started.
Simple Interest Formula
Simple Interest is a mathematical concept. It is a kind of interest in the capital that we get if we keep the money invested for a particular period of time. The formula for simple interest is:
Here, SI = Simple Interest
P = Principal amount
R = Rate of interest
T = Time period of investment
So, we will now implement this logic as a C program by writing the Simple interest program in C language.
Simple Interest Program in C – Logic Building
So, the aim is to calculate the simple interest. For calculating the simple interest, we need the Principal amount, Rate of Interest, and Time. So, we need to take these 3 quantities as input from the user. We will store them in 3 variables and then we will multiply them and divide the multiplication by 100 to get the simple interest.
However, if we store the Simple interest in an integer value and then divide by 100, only the integer part will be stored i.e. the floor value will be stored. So, we can use the float or double data types to store the decimal type of values. This will help us get the answer in decimals. So, the algorithm is as follows.
Algorithm to Find Simple Interest in C
- Input three integers and store them in three variables one by one. Let us say the variables are P, R, and T.
- Now, multiply the three variables i.e. PRT and divide this whole result by 100 and store it in a variable say SI.
- Print the resultant variable i.e SI. This will be the simple interest.
Simple Interest Program in C
# include <stdio.h> # include <stdlib.h> int main(){ //Simple interset program int P, R, T; double SI; printf("Enter the principal: "); scanf("%d", &P); printf("Enter the rate: "); scanf("%d", &R); printf("Enter the time: "); scanf("%d", &T); SI = (P * R * T) / 100; printf("The Simple interest is %d", SI); return 0; }
Time Complexity for Simple Interest Program in C:
The time complexity for simple interest program in C is O(1). This is because we are just performing 2 multiplications and 1 division i.e. constant operations. So, the time does not depend upon the size of the input.
Space Complexity for Simple Interest Program in C:
The space complexity of the simple interest program in C is also O(1). This is because we have not used any extra data structure. We have just created P, R, T, and SI variables that take up only constant space. So, the amount of space taken also does not depend upon the input size.
Simple Interest Program in C – Using a Function
The simple interest can also be calculated not inside the main function but rather, taking the input from the user in the main function and calculating the simple interest inside another function and calling that function from the main function. Making a separate function is always considered a better practice because it helps in providing code reusability. The following is the code for the same.
C Program for Simple Interest using Function
# include <stdio.h> # include <stdlib.h> double simple_interest(int P, int R, int T) { return (P*R*T)/100; } 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 = %d",SI); }
Time Complexity of Simple Interest Program in C using a Function:
Since there is no change in the logic of the code, the time complexity remains the same i.e. O(1). However, there will be an extra overhead of function calling this time.
Space Complexity of Simple Interest Program in C using a Function:
Since there is no change in the logic of the code, the space complexity of the program remains the same i.e. O(1). However, there will be a space overhead too for the function call as the function will acquire some memory in the function call stack.
So, this is the simple interest program in C. We hope that you have understood the concept and liked the discussion. We hope to see you again soon at PrepBytes.
Other C Programs
C Program for Binary Search
C Program to Add Two Numbers
C Program to Calculate Percentage of 5 Subjects
C Program to Convert Binary Number to Decimal Number
C Program to Convert Celsius to Fahrenheit
C Program to Convert Infix to Postfix
C Program to Find Area of Circle
C Program to Find Roots of Quadratic Equation
C program to Reverse a Linked List
C program to reverse a number
Ascending Order Program in C
Menu Driven Program For All Operations On Doubly Linked List in C
C Program for Armstrong Number
C Program For Merge Sort For Linked Lists
C program for performing Bubble sort on Linked List
Hello World Program in C
Perfect Number Program in C
Leap Year Program in C
Odd Even Program in C
Selection Sort Program in C
Linear Search Program in C
While Loop Program in C
C Program to Swap Two Numbers
Calculator Program in C Language