# My little program... help pls!

• 01-02-2007
soundwave
My little program... help pls!
Hi everyone!

As i have left my programming module to the last minute, I have been programming now for a few hours(literally) and I must say I am pretty damn good at it!

This is a program I need to tackle, it is a basic loan repayment thing.

I have corrected all the errors so now it runs, but how do i get it to stop on the remaining months?

you'll see what i mean when you run it:

Code:

```#include <stdio.h> #include <conio.h>                 int main()         {           float loan, rate, payment;       int month=0;     printf ("Amount of loan                      =                ");     scanf("%f", & loan);     printf("Monthly interest rate                =                ");     scanf("%f", & rate);     printf("Payment per month                        =                ");     scanf("%f", & payment);     while (loan>0.0)     {     month++;             loan=loan+(loan*rate/100);       loan=loan-payment;       printf("In month %d the sum remaining is =  %f\n", month, loan);     }     printf("It will take %d months to pay\n", month);     getch();   }```
• 01-02-2007
rynoon
Take a look at what your loop does. Say you enter the following data.

loan = 3.33
rate = 3.1
payment = 0.33

(I like three's)

So on month twelve, loan will equal 0.093329. The next iteration of the loop will begin and since 0.093329 is greater than 0 it will perform the calculations again and print a negative value (in this case, ~-0.233). What you want to do is check to make sure that your loan is greater than 0 BEFORE you make your calculations, instead of afterwards.

What you could do is add a little if statement in your loop. Something like...

Code:

```if(loan <= 0.0f)   printf("You have completed your loan payments! FREEDOM!\n"); else   printf("In month %d the sum remaining is =  %f\n", month, loan);```
• 01-02-2007
DavidP
>What you want to do is check to make sure that your loan is greater than 0 BEFORE you make your calculations

I echo that.

>I have been programming now for a few hours(literally)

Try going for 48 or 72 hours without stopping except for sleeping and eating :)

About your code. I used to use conio.h as well, but it is a non-standard header file, so you might want to not include that file. Instead of using getch(), which is a function inside conio.h, you can use getc() or getchar() in stdio.h.

Oh....and remember to return 0 at the end of your main function. :)
• 01-03-2007
Salem
Indentation - why is int main the most indented line of the program?
• 01-03-2007
vart
You also should think about the case when the payment is not enough to pay the loan...
You should somehow prevent the infinite loop
• 01-04-2007
soundwave
cheers
an IF statement solves it very easily indeed.

cheers guys
• 01-04-2007
robatino
Quote:

Originally Posted by DavidP
Oh....and remember to return 0 at the end of your main function. :)

If main() gets to the closing brace and there's no explicit return statement, it returns 0 automatically.