Reputation: 115
I think it's obvious I don't understand. How do you tell the computer in C to decide which is the appropriate interest rate and then calculate and display it. This is the best I could come up with and I have to hand this as an assignment tomorrow. I had not clue it would be this difficult.
#include <stdio.h>
int main (void)
{
float time;
float principal;
char response[15];
float rate1, rate2,rate3,rate4;
rate1=.04,rate2=.05,rate3=.06,rate4=.07;
float SimpleInterest1;
float SimpleInterest2;
float SimpleInterest3;
float SimpleInterest4;
SimpleInterest1=principal*time*rate1;
SimpleInterest2=principal*time*rate2;
SimpleInterest1=principal*time*rate3;
SimpleInterest2=principal*time*rate4;
printf("Please enter principal\n");
scanf ("%f",&principal);
printf ("Please enter time\n");
scanf ("%f",&time);
if (principal <= 5000)
{
printf ("%f",&SimpleInterest1);
}
printf ("Do you still want the loan?\n");
scanf ("%s",response);
return 0;
}
Upvotes: 1
Views: 377
Reputation: 1038
As it has been already said: do not forget to ask for principal
value using
scanf
.
Then, use if-else if-else-statements to know in which interval principal
lies.
Then, inside each statement, assign interest
to the right value.
Then assign time
to the right value (you can scanf
it if you have to) before calculating the interest.
Also, check if the interest has to be recomputed each year on the
new debt. If this is the case, then the formula should be
debt = principal * (1 + rate)^time
.
You can #include <math.h>
to use the pow
function that computes the power of a float or a double.
Then just printf("%f", debt);
.
Aparté:
Michael Overton's book "Numerical Computing with IEEE Arithmetic" pp.82-86 explains pretty well how to compute a compound interest with a stable algorithm, because the naive way to compute it using pow
can involve a loss of accuracy.
Upvotes: 2
Reputation:
First off, these two lines are probably a typo:
SimpleInterest1=principal*time*rate3;
SimpleInterest2=principal*time*rate4;
They should become this:
SimpleInterest3=principal*time*rate3;
SimpleInterest4=principal*time*rate4;
Next, if your asking about i/o (input/output), then here's a basic run through:
You use printf( char *format, ...)
to output information.
You use scanf( char *format, ...)
to do basic input.
Where format is one of the following (this is the basics):
%s : Argument is expected to be of type char*
%i : Argument is expected to be signed int
%f : Argument is expected to be float (use also for double in printf)
%u : Argument is expected to be unsinged int.
When you use scanf, you should check the return value and clear the input buffer, examples will follow:
void clear_buffer() {
// Note that ch is int not char, this is important
int ch;
while( (ch = getchar()) != EOF && ch != '\n');
}
int answers = 0;
float value = 0.0;
do {
// Scanf returns the recieved number of args that fit the format string
answers = scanf( "%f", &value );
clear_buffer();
if (answers == 0) {
continue;
}
} while (value > -0.1 && value < 0.1);
The above may not work as I mainly work with unsigned integers, but it should provide a good foundation at the very least.
You use if...else if....else to determine the formula to use.
Lastly, you should calculate the SimpleInterest AFTER you get a value for time and principal; the c parser cannot 'see into the future'
Upvotes: 0