Velocity Reviews > simple intrest program(beginner)

# simple intrest program(beginner)

c_beginner
Guest
Posts: n/a

 02-16-2006
yes, this is my how work question. Since I am lack in getting
an assistance with my lab work I put this in this advance group.
Sorry for the trouble I am making.

Write a program to calculate the simple interest.

#include<stdio.h>
int intrest(int rate,float amount);
int main(void)
{
int intrest_rate, /* the percentage intrest*/
return_year ; /* year of returing the amt*/
float money, /* amount which has been browed*/
intrest_amt, /* intrest to be paid */
si; /* calcuation of simple intrest*/

printf("input the intrest,amount,return year\n");
scanf("%d%f%d",&intrest_rate,&money,&return_year);
if((return_year < 1) && (return_year > 50))

printf("enter the year again.The year should not be greater than 50");

intrest_rate = intrest(intrest_rate,money);
if(intrest_rate)
si = money * intrest_amt * return_year;
printf("The simple intrest is %f\n",si);

}
int intrest(int rate,float amount)
{
int temp;

if((rate < 0) || (rate > 100))
{
printf("the intrest rate is either too high or low\n");
exit(1);
}
return temp = ((amount * rate) / 100);
}

Antonio Contreras
Guest
Posts: n/a

 02-16-2006
c_beginner wrote:
> yes, this is my how work question. Since I am lack in getting
> an assistance with my lab work I put this in this advance group.
> Sorry for the trouble I am making.
>
> Write a program to calculate the simple interest.

Nice try for a beginner. See comments below.

>
> #include<stdio.h>
> int intrest(int rate,float amount);
> int main(void)
> {
> int intrest_rate, /* the percentage intrest*/
> return_year ; /* year of returing the amt*/

I find splitting declarations between various lines like you just did
ugly. IMHO this is more readable:

int interest_rate; /*the percentage interest*/
int return_year; /*year of returning the amt*/

BTW, why do you declare the interest rate as an integer. You can have
interest rates such as 2.5% A float or preferably a double makes more
sense.

> float money, /* amount which has been browed*/
> intrest_amt, /* intrest to be paid */
> si; /* calcuation of simple intrest*/

Again, I would repeat the type in each line.
money is somehow too general a name for a variable. money_borrowed is
more explicit. Get into the habit of using meaningful names for your
variables and you'll save yourself the pain of trying to remember what
mbzhjkil stands for.
The float type runs out of precission really quick. There's no reason
to not using double.

> printf("input the intrest,amount,return year\n");
> scanf("%d%f%d",&intrest_rate,&money,&return_year);

scanf is dangerous. Use fgets and sscanf instead. Wheter you stick with
scanf or change to the fgets/sscanf combination, check the return value
of (s)scanf, it tells you how many parameters were successfully parsed
and assigned. Ask yourself what will happen to your program if the user
inputs something like

13af hello world!!!

Also I would ask for each variable at a time, it makes it less
confusing for the user.

> if((return_year < 1) && (return_year > 50))

I think you want an || instead of an &&. As it's written the condition
will allways be false.

> printf("enter the year again.The year should not be greater than 50");

Here you ask the user to enter the year again, but you do not provide
code for reading the input again.

> intrest_rate = intrest(intrest_rate,money);
> if(intrest_rate)

What is this if suppossed to do? The follwing code will only execute if
intrest_rate is non_zero. But what's the problem with intrest_rate
being zero? Furthermore, if it is zero your program will not assign to
si, but it will use its value in the following printf. This is
undefined behaviour, anything may happen.

> si = money * intrest_amt * return_year;
> printf("The simple intrest is %f\n",si);

You declared main as returning int (well done, BTW) but you fail to
provide a return statement. In C99 is legal to fall off the end of a
function without returning a value, but it is still poor style.

> }
> int intrest(int rate,float amount)
> {
> int temp;
>
> if((rate < 0) || (rate > 100))
> {
> printf("the intrest rate is either too high or low\n");
> exit(1);

You did not provide a prototype for exit(). include the stdlib.h

On a side note, wouldn't it be nicer asking the user to enter the

> }
> return temp = ((amount * rate) / 100);
> }