simple intrest program(beginner)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • c_beginner

    #1

    simple intrest program(beginner)

    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_y ear);
    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

    #2
    Re: simple intrest program(beginne r)

    c_beginner wrote:[color=blue]
    > 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.[/color]

    Nice try for a beginner. See comments below.
    [color=blue]
    >
    > #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*/[/color]

    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.
    [color=blue]
    > float money, /* amount which has been browed*/
    > intrest_amt, /* intrest to be paid */
    > si; /* calcuation of simple intrest*/[/color]

    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.
    [color=blue]
    > printf("input the intrest,amount, return year\n");
    > scanf("%d%f%d", &intrest_rate,& money,&return_y ear);[/color]

    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.
    [color=blue]
    > if((return_year < 1) && (return_year > 50))[/color]

    I think you want an || instead of an &&. As it's written the condition
    will allways be false.
    [color=blue]
    > printf("enter the year again.The year should not be greater than 50");[/color]

    Here you ask the user to enter the year again, but you do not provide
    code for reading the input again.
    [color=blue]
    > intrest_rate = intrest(intrest _rate,money);
    > if(intrest_rate )[/color]

    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.
    [color=blue]
    > si = money * intrest_amt * return_year;
    > printf("The simple intrest is %f\n",si);[/color]

    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.
    [color=blue]
    > }
    > 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);[/color]

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

    On a side note, wouldn't it be nicer asking the user to enter the
    interest again, instead of exiting?
    [color=blue]
    > }
    > return temp = ((amount * rate) / 100);
    > }[/color]

    Comment

    Working...