# Thread: Hard stuck C Primer Plus example. Any help please :)

1. ## Hard stuck C Primer Plus example. Any help please :)

Hi guys new here ^.^ I'm working out some examples from C Primer Plus, Fifth Edition By Stephen Prata and I stumbled upon the following example.

I have managed to do simple and compound interest. But can't seem to do the last part. All help is appreciated.

John invests 200 dollars at 15% simple interest. (That is, every year, the investment earns an interest equal to 15% of the original investment.) Luke invests 200 dollars at 10% interest compounded annually. (That is, interest is 10% of the current balance, including previous addition of interest.) Write a program that finds how many years it takes for luke's invested sum to overtake John's along with a display showing the two values at that time.

Code:
```#include<math.h>
int main()
{
float p,q,r,SI,CI;
int n;
printf("Enter the value of Principal p = ");
scanf("%f",&p);
printf("Enter the value of Rate r = ");
scanf("%f",&r);
printf("Enter the value of Period in year n = ");
scanf("%d",&n);
SI = ((p*r*n)/100);
printf("Simple Interest SI=%f \n",SI);
q = 1+(r/100);
CI=p*pow(q,n)-p;
printf("Compound Interest CI=%f \n",CI);
return 0;
}```

Thanks 2. You just need a loop over n.
Then use your loop variable in place of n in your calculations. 3. What is the input for?
All numbers should be constants according to the exercise.
Only the number of years should be variable. 4. Code:
`#include`
Code:
```<stdio.h>
#include<math.h>
int main()
{
const float p = 200;
const float r =0.15;
const float g =0.1;
float q;
float SI;
float CI;
int n;

printf("Enter the value of Period in year n = ");
scanf("%d",&n);

SI = ((p*r*n)/100);
printf("Simple Interest SI=%f \n",SI);

q = 1+(g/100);
CI=p*pow(q,n)-p;
printf("Compound Interest CI=%f \n",CI);
return 0;
}```
Here is what I ended up with after taking your advice. But it isn't running :/
Salem could you please tell me how and were i should make use of the loop function.
THanks  6. Hi salem thanks again, yes i know how to use a loop etc I just cant seem to understand how to use loops here in this case. 7. Could it be

Code:
```#include<stdio.h>
#include<math.h>
int main()
{
const float p = 200;
const float r =0.15;
const float g =0.1;
float q;
float SI;
float CI;
int n;
int m;

for (int n = 0; n = m ; m++)
printf("n is %d \n", n)

SI = ((p*r*n)/100);
printf("Simple Interest SI=%f \n",SI);

q = 1+(g/100);
CI=p*pow(q,m)-p;
printf("Compound Interest CI=%f \n",CI);
return 0;
}``` 8. Remember that if you do not use braces, the body of the for loop is only the next statement, i.e., your code is equivalent to:
Code:
```#include<stdio.h>
#include<math.h>
int main()
{
const float p = 200;
const float r =0.15;
const float g =0.1;
float q;
float SI;
float CI;
int n;
int m;

for (int n = 0; n = m ; m++)
{
printf("n is %d \n", n);
}

SI = ((p*r*n)/100);
printf("Simple Interest SI=%f \n",SI);

q = 1+(g/100);
CI=p*pow(q,m)-p;
printf("Compound Interest CI=%f \n",CI);
return 0;
}```
Notice that I use indentation to denote scope. 9. So how should it be then please? And regarding the coding is it right?
Thanks 10. Maybe like this:
Code:
```double value_john = 200, value_luke = 200;
int years = 0;
do
{
// calculate interest for luke and add to value_luke
// add interest for john to value_john
// note that interest for john is always the same so can be const
// years++
}while (value_john >= value_luke);```
After 9 years luke should have more money than john 11. Originally Posted by Alfred_Schembri
So how should it be then please?
My statement about braces hints to you that your lack of braces is wrong. Originally Posted by Alfred_Schembri
And regarding the coding is it right?
Have you compiled and tested your code? Does it compile? Does it give you the correct output for various valid test input? Bonus: does it handle invalid test input correctly? These are things you should try before asking other people if it is right, otherwise you might end up with the unhelpful but technically correct answer of "no". Once you have figured out that something is wrong, then you can investigate further, and upon finding that you're stuck, that's when you should ask for more specific help in finding/fixing what is wrong. 12. Originally Posted by OldGuy2 Code:
```   // calculate interest for luke and add to value_luke
// add interest for john to value_john
// note that interest for john is always the same so can be const
// years++
}while (value_john >= value_luke);```
I did it exactly like that. The names and amounts have changed in version six of book.
Code:
```int main(void) {
const float original = 100.0f;
float daphne;
float deirdre = daphne = original;
int year = 0;``` Popular pages Recent additions help in c, interest, investment, programming in c, simple 