Simple Interest Program in C using Function | While | Do-while | For loop | Pointers
Simple interest program in C using function, while, do-while, for loop, and pointers.
In this article, you will learn how to make a simple interest program in C using function, while, do-while, for loop, and pointers.
Example
Enter the principal (amount), time, and rate::
5
7
9
Simple Interest = 3.15
You should have knowledge of the following topics in c programming to understand this program:
- C
main()
function - C
printf()
function - C
math.h
library - C Functions
- C Loops
- C Pointers
Simple Interest Formula in C
SI = (P * T * R) / 100
Where
P = Principal Amount
T = Time
R = Rate
SI = Simple Interest
In this article, we solve this problem in six methods:
- Using the normal calculation
- Using the function
- Using the while loop
- Using the do-while loop
- Using the pointers
- Using the for loop
Source Code
// Simple Interest Program in C based on Given Values
#include <stdio.h>
int main() {
float p, t, r, SI;
// `p` = principal, `t` = time, and `r` = rate
// `SI` = value of the simple interest
printf("Enter the principal (amount), time, and rate::\n");
scanf("%f%f%f", &p, &t, &r);
// It will calculate the value of simple interest
SI = (p * t * r) / 100;
// It will produce the final output
printf("\nSimple Interest = %.2f\n", SI);
return 0;
}
Output
Enter the principal (amount), time, and rate::
5
7
9
Simple Interest = 3.15
Explanation
In this given program, we have taken inputs Amount, Time, and Rate amount following: 5
, 7
, and 9
from the user via the system console. Then we applied the standard formula to calculate simple interest.
Then this program will return the 3.15
simple interest value of the above calculation.
Simple Interest Program in C using Function
// Simple Interest Program in C using Function
#include <stdio.h>
// This function will calculate the simple interest
void CalcSimpleInterest(float x, float y, float z) {
float SI; // value of the simple interest
// It will calculate the value of simple interest
SI = (x * y * z) / 100;
// It will produce the final output
printf("\nSimple Interest = %.2f\n", SI);
}
// It's the driver function
int main() {
float p, t, r;
// `p` = principal, `t` = time, and `r` = rate
printf("Enter the principal (amount), time, and rate::\n");
scanf("%f%f%f", &p, &t, &r);
CalcSimpleInterest(p, t, r);
return 0;
}
C Program to Calculate Simple Interest using While loop
// 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;
}
Output
Enter the principal (amount), time, and rate::
23
45
76
Simple Interest = 786.60
C Program to Calculate Simple Interest using do-while loop
// C Program to Calculate Simple Interest using do-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;
do {
scanf("%f", &y);
// It will calculate the value of simple interest
x *= y;
if (count == 3)
SI = x/100;
count++;
} while (count <= 3);
// It will produce the final output
printf("\nSimple Interest = %.2f\n", SI);
return 0;
}
C Program to Calculate Simple Interest using Pointers
// C Program to Calculate Simple Interest using Pointers
#include <stdio.h>
int main() {
float p, t, r, SI;
// `p` = principal, `t` = time, and `r` = rate
// `SI` = value of the simple interest
float *x, *y, *z; // These are the pointer variables
printf("Enter the principal (amount), time, and rate::\n");
scanf("%f%f%f", &p, &t, &r);
x = &p;
y = &t;
z = &r;
// It will calculate the value of simple interest
SI = (*x * *y * *z) / 100;
// It will produce the final output
printf("\nSimple Interest = %.2f\n", SI);
return 0;
}
Output
Enter the principal (amount), time, and rate::
34
6
12
Simple Interest = 24.48
Simple Interest Program in C using For loop
// Simple Interest Program in C using For 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");
for (int count = 1; count <= 3; count++) {
scanf("%f", &y);
// It will calculate the value of simple interest
x *= y;
if (count == 3)
SI = x/100;
}
// It will produce the final output
printf("\nSimple Interest = %.2f\n", SI);
return 0;
}
Also, visit these links
C Program to Enter Marks of Five Subjects and Calculate Percentage and Grade
C++ Program to Calculate Percentage and Grade
Java Program to Calculate Grade of students
Python Program to Calculate Total Marks Percentage and Grade of a Student