Examples of C part 43 calculate simple interest in all ways
Program :
Output:
Enter
1 to find out Simple Interest
2 to find out Principal
3 to find out rate in
4 to find out Time
2
Enter Simple Interest
37.500
Enter Rate in percentage
5
Enter Time in years(decimals)
0.25
Principal is 3000.000000Explanation:
Formulae used in Program:
#include<stdio.h> main() { float p,r,t,simpleInterest; int choice; printf("Enter\n1 to find out Simple Interest\n2 to find out Principal\n3 to find out rate in % \n4 to find out Time\n"); scanf("%d",&choice); switch(choice) { case 1: { printf("Enter Pricipal\n"); scanf("%f",&p); printf("Enter Rate in percentage \n"); scanf("%f",&r); printf("Enter Time in years(decimals)\n"); scanf("%f",&t); simpleInterest=(float)(p*r*t)/100.0; printf("Simple Interest is %f\n",simpleInterest); break; } case 2: { printf("Enter Simple Interest\n"); scanf("%f",&simpleInterest); printf("Enter Rate in percentage\n"); scanf("%f",&r); printf("Enter Time in years(decimals)\n"); scanf("%f",&t); p=(float)(100.0*simpleInterest)/(r*t); printf("Principal is %f\n",p); break; } case 3: { printf("Enter Simple Interest\n"); scanf("%f",&simpleInterest); printf("Enter Principal\n"); scanf("%f",&p); printf("Enter Time in years(decimals)\n"); scanf("%f",&t); r=(float)(100.0*simpleInterest)/(p*t); printf("Rate is %f%\n",r); break; } case 4: { printf("Enter Simple Interest\n"); scanf("%f",&simpleInterest); printf("Enter Principal\n"); scanf("%f",&p); printf("Enter Rate in percentage\n"); scanf("%f",&r); t=(float)((100.0*simpleInterest)/(p*r)); printf("Time is %f years\n",t); break; } } }
Enter
1 to find out Simple Interest
2 to find out Principal
3 to find out rate in
4 to find out Time
2
Enter Simple Interest
37.500
Enter Rate in percentage
5
Enter Time in years(decimals)
0.25
Principal is 3000.000000Explanation:
Formulae used in Program:
1.Simple Interest=(Principal*Rate in %* Time in Years)/100 2.Principal=(100*Simple Interest)/(Rate in %* Time in Years) 3.Rate(%)=(100*Simple Interest)/(Principal* Time in Years) 4.Time(Yrs)=(100*Simple Interest)/(Principal* Rate(%))
- This program starts with initializing :
- p → To store principal in Rupees
- r → To store rate in %
- t → To store time in years
- simpleInterest → To store output
printf("Enter\n1 to find out Simple Interest\n2 to find out Principal\n 3 to find out rate in % \n4 to find out Time\n"); scanf("%d",&choice);
To prompt user to choose one of the following as shown in the output.- Lets run by giving choice as 2 which is to find out Principal.
- As the choice is 2. Case 2: will be executed. Learn Switch case in detail:"https://beginnerlanguage.blogspot.in/p/switch-case-statement.html"
- As per case formula 2 is used for execution
printf("Enter Simple Interest\n"); scanf("%f",&simpleInterest);
Taking Simple Interest from userprintf("Enter Rate in percentage\n"); scanf("%f",&r);
Taking Rate from userprintf("Enter Time in years(decimals)\n"); scanf("%f",&t);
Taking Time from userp=(float)(100.0*simpleInterest)/(r*t); printf("Principal is %f\n",p); break;
From formula 2 the above will calculate and stores result in p which is then printed.