Browse Source

Create function to calculate monthly interest

remotes/origin/feature/loan-eligibility
fdlt3817 2 years ago
parent
commit
1d25ff8ecf
  1. 43
      src/interestCalculator.c
  2. 3
      src/interestCalculator.h

43
src/interestCalculator.c

@ -1,7 +1,7 @@
#include "interestCalculator.h" #include "interestCalculator.h"
void calculateInterest(){
void calculateYearlyInterest(){
float principalAmount; float principalAmount;
float interestPerYear; float interestPerYear;
float timeInYears; float timeInYears;
@ -29,17 +29,48 @@ void calculateInterest(){
float result= principalAmount*(1+(interestDecimal*timeInYears)); float result= principalAmount*(1+(interestDecimal*timeInYears));
printf("\nAmount with the interest is %.2f.",result); printf("\nAmount with the interest is %.2f.",result);
} }
void calculateMonthlyInterest(){
float principalAmount;
float interestPerMonth;
float timeInMonths;
printf("Please enter the principal amount:");
scanf("%f",&principalAmount);
if(principalAmount<=0){
return;
}
printf("\nPlease enter interest per month (percentage):");
scanf("%f",&interestPerMonth);
if(interestPerMonth<=0){
return;
}
printf("\nPlease enter interest time in months:");
scanf("%f",&timeInMonths);
if(timeInMonths<=0){
return;
}
float interestDecimal=interestPerMonth/100;
float result= principalAmount*(1+(interestDecimal*timeInMonths));
printf("\nAmount with the interest is %.2f.",result);
}
void initiateCalculator(){ void initiateCalculator(){
int input; int input;
char c; char c;
printf("Welcome to the interest calculator. Please select an option:\n[1]Calculate yearly interest\n");
printf("Welcome to the interest calculator. Please select an option:\n[1]Calculate yearly interest\n[2]Calculate monthly interest\n");
scanf("%d", &input); scanf("%d", &input);
switch(input){ switch(input){
case 1: case 1:
calculateInterest();
calculateYearlyInterest();
break;
case 2:
calculateMonthlyInterest();
break; break;
default: default:
break; break;
@ -53,8 +84,8 @@ void initiateCalculator(){
} }
} }
int main(){
// int main(){
initiateCalculator();
// initiateCalculator();
}
// }

3
src/interestCalculator.h

@ -3,5 +3,6 @@
#include <stdio.h> #include <stdio.h>
void calculateInterest();
void calculateYearlyInterest();
void calculateMonthlyInterest();
#endif #endif
Loading…
Cancel
Save