Here is my question:
Write a program that reads from the keyboard an initial balance b, the annual rate of interest r and the projected balance you want to reach.

The program should then display the number of monthe it will take for the balance to reach the projected balance.

I am given this equation, the blance after one month time will be b*(1+r/1200).

My problem here is that i am not able to display the number of months. For example for initial balance i enter 10 projected balance 12 and rate on interest 10/100 my output should be number of months = 2. But it displays 10, 11, 12. I am not able to correct it i am stuck.

here are my codes:

#include<stdio.h>
#include<stdlib.h>
// This program will display the number of month it will take to reach the projected baance.
int main()
{
    int count, n, b, r, p;

    printf("Enter initial balance: ");
    scanf("%d", &b);
     printf("Enter projected balance: ");
    scanf("%d", &p);
    printf("Enter annual rate: ");
    scanf("%d", &r);

    n=b*(1+(r/1200));

    for(count=n;count<=p;count++){

          printf("The number of month to reach the projected balance is %d\n", count);
}


    system("pause");
    return 1;

    }

r/1200 is doing integer math which means all decimals are discarded. So 10/100 is 0 because 10 cannot be divided by 100. You can fix that in one of three ways

(1) Declare r as float instead of int, then use scanf("%f") instead of scanf("%d")

(2) Typecast either the numerator of denominator to float
(float)r/100

(3) Make 100 a float by adding decimal number, such as 100.0

yeah thanks that helped, but to make my program count the number of month im still stuck on this.

You need to do math inside the loop that begins on line 17. The equation on line 15 is only for one month. The balance needs to be recalculated every month until it reaches the target amount.

The loop on line 17 it completly wrong -- n is the balanace after one month. You don't want to increment the balanace each loop but just increament a simple counter that counts the number of months from 0 to ??. I think a while loop would be easier for this assignment. Inside the loop you need to calculate the balance at the current month, b = b*(1.0F+(r/1200.0F));

It should be noted that b should also be a floating point number, or alternatively it should be balance in pennies not dollars (or what ever currency you're using).

Also repeatedly doing b = b*(1.0F+(r/1200.0F)); is going to give you compond interest. For example if your starting balance is $1000 and the annual interest rate is 12% (yes only in my dreams) then you might expect at the end of the year to have $1120. However if you add interest using this formula at the end of every month you would get

1.0F + (r/1200.0F) for r = 12 == 1.0F + (12/1200.0F) == 1.0F + (1/100.0F) == 1.01F

bstart = 1000 * 100 = 100000 # in pennies
b1 = 100000 * 1.01F = 101000
b2 = 101000 * 1.01F = 102010
b3 = 102010 * 1.01F = 103030
b4 = 103030 * 1.01F = 104060
b5 = 104060 * 1.01F = 105101 # I choose to round up I'm a nice bank
b6 = 105101 * 1.01F = 106152
b7 = 106152 * 1.01F = 107214
b8 = 107214 * 1.01F = 108286
b9 = 108286 * 1.01F = 109369
b10 = 109369 * 1.01F = 110463
b11 = 110463 * 1.01F = 111568
b12 = 111568 * 1.01F = 112684

Or $1126.84 which is $6.84 greater than you would expect which no bank would ever allow to happen.

If you are doing simple interest then the same ammount should be being added each month, that is in the example each month you should add bstart * 1.01F not bcurrent * 1.01F and at the end of the year you calculate a new bstart for the next year. Alternatively if you are doing compound interest then then the actual monthly interest rate to achieve an annual interest rate of 12% is not rate / 1200 but rather 0.94888% or in abolute terms 0.94888 / 100 = 0.0094888.

Pumping this interest rate into the above table you get

bstart = 1000 * 100 = 100000 # in pennies
b1 = 100000 * 1.0094888F = 100949
b2 = 101000 * 1.0094888F = 101907
b3 = 102010 * 1.0094888F = 102874
b4 = 103030 * 1.0094888F = 103850
b5 = 104060 * 1.0094888F = 104835
b6 = 105101 * 1.0094888F = 105830
b7 = 106152 * 1.0094888F = 106834
b8 = 107214 * 1.0094888F = 107848
b9 = 108286 * 1.0094888F = 108871
b10 = 109369 * 1.0094888F = 109904
b11 = 110463 * 1.0094888F = 110947
b12 = 111568 * 1.0094888F = 112000

Or $1120 just what we were expecting.

At this point I would like to say that I am not saying that what you are doing is necessaryily wrong for what appears to be a text book/class exercise just that that is not how interest works in the real world.

If you are wondering how I got 1.0094888F I used 12thRoot(1.12) = 1.0094888F take 1 and times by 100 to get the monthly percentage rate of 0.94888%

On and finally, @Ancient Dragon just how did you get that text in red?

Also repeatedly doing b = b*(1.0F+(r/1200.0F)); is going to give you compond interest

I thought calculating compound interest was he whole idea of the assignment.

@Ancient Dragon just how did you get that text in red?

the Inline Code button does that, just highlight the text and hit Inline Code in the grey toolbar. It will also prevent the editor from changing the spacing, just as code button does.

The issue isn't doing compound interest, it is how the queston originally posted converts from an annual interest rate to an equivilent monthly compound interest rate which is completely wrong.

He doesn't have any choice but to use that formula, his instructor gave it to him.

yeah the formula is given and it is a test question of last semester but i did not attempt it well. Thanks for your help guys i have completed it :)

He doesn't have any choice but to use that formula, his instructor gave it to him.

Agreed, just don't get me started on instructors that set 1/2 baked questions that lead their students up the garden path :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.