Skip to content

Instantly share code, notes, and snippets.

@ashwynh21
Last active December 3, 2021 08:04
Show Gist options
  • Save ashwynh21/b594ad0fd016b96a78574d0751f77676 to your computer and use it in GitHub Desktop.
Save ashwynh21/b594ad0fd016b96a78574d0751f77676 to your computer and use it in GitHub Desktop.
A simple program to compute compound interest using loops and printing the result on CLI.
// in this program we are going to be calculating compound interest using a looping
// algorithm.
//
// firstly, we discuss the solution that we have in mind. since we are required to
// compute this without a formula but using loops we are going to define a function
// that will represent the function call.
//
// let us start by getting the resources we are going to need
#include <iostream>
// then we start defining our function here
double compoundInterest(int invest, double years, double annualRate) {
double interest = 1;
for (; years > 0; years--) {
interest *= (1 + annualRate);
}
return invest * interest;
}
// we define an assertion function that will assert that a number n is real and between
// 0 and 100
void assertReal(int n) {
if (n < 0 || n > 100) {
throw ("Value is Not a Real Number");
}
}
// okay, now with the function defined all we are going to need to do is apply it.
int main()
{
// let us consider the problem statement that we are given. we are required to compute
// the interest of a E100 investment with interst of 5%, we are to print the interest
// accumulated after 10years
// so let us make a functionality to allow a user to enter the values that will then be
// computed here. considering the problem, we are required to enter only the interest rate
// so we will make a prompt for just that
int rate = 0;
// we will cout a prompt so that user knows what to enter
std::cout << "Please enter your anual interest rate below" << std::endl;
std::cin >> rate;
// once the interest rate is entered we should validate it to make sure that the value is a
// real number between 0 and 100 percent;
assertReal(rate);
std::cout << "Compound Interest After 10 Years - " << compoundInterest(100, 10, (double)rate / 100) << std::endl;
return 0;
}
@Zakhe-TH
Copy link

Zakhe-TH commented Dec 3, 2021

I will put the first task 2 for your comments

@ashwynh21
Copy link
Author

Cool cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment