simple intrest program(beginner)

Discussion in 'C Programming' started by c_beginner, Feb 16, 2006.

  1. c_beginner

    c_beginner Guest

    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);
    }
     
    c_beginner, Feb 16, 2006
    #1
    1. Advertising

  2. 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
    header.

    On a side note, wouldn't it be nicer asking the user to enter the
    interest again, instead of exiting?

    > }
    > return temp = ((amount * rate) / 100);
    > }
     
    Antonio Contreras, Feb 16, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    595
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Rensjuh
    Replies:
    7
    Views:
    983
    Mabden
    Sep 2, 2004
  3. Replies:
    7
    Views:
    649
    Stig Brautaset
    Jul 23, 2003
  4. c_beginner

    compound intrest program(beginner)

    c_beginner, Feb 16, 2006, in forum: C Programming
    Replies:
    6
    Views:
    408
    Martin Ambuhl
    Feb 16, 2006
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    224
    7stud --
    Apr 12, 2011
Loading...

Share This Page