compound intrest program(beginner)

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

    #1

    compound 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 compound interest.

    #include<stdio. h>
    #include<stdlib .h>
    double intrest(double tot_amount,floa t percent)
    {
    double temp;
    if(percent < 0.0 || percent > 100.0)
    {
    printf("un-reasonable intrest rate, enter again\n");
    fscanf(stdin,"% f",percent);
    }
    temp = (tot_amount * percent) / 100;
    return temp;
    }
    int main(void)
    {
    double intrest_rate,cp nd_intrest,prnc _amount,percent ;
    unsigned short year,i;
    printf("enter the percent of intrest,amount, year\n");
    fscanf(stdin,"% f%f%d",&percent ,&prnc_amount,& year);
    if(year > 50)
    {
    printf("kindly input the reasonable year of return\n");
    fscanf(stdin,"% d",&year);
    }
    else
    {
    intrest_rate = intrest(prnc_am ount,percent);
    cpnd_intrest = prnc_amount*(1+ intrest_rate);
    for(i = year ; i< year ; i++)
    cpnd_intrest = cpnd_intrest * i;
    }
    printf("the compound intrest is %f\n",cpnd_intr est);
    return 0;
    }

    I am getting a strange output when I run the program.Kindly help.


  • Eric Sosman

    #2
    Re: compound intrest program(beginne r)



    c_beginner wrote On 02/16/06 10:44,:[color=blue]
    > [...]
    > int main(void)
    > {
    > double intrest_rate,cp nd_intrest,prnc _amount,percent ;
    > unsigned short year,i;
    > printf("enter the percent of intrest,amount, year\n");
    > fscanf(stdin,"% f%f%d",&percent ,&prnc_amount,& year);[/color]

    Here's at least one problem (I haven't looked carefully
    for others): the conversion specifiers in the format string
    don't match the corresponding pointer arguments. With "%f"
    you're telling fscanf() to produce a `float', but you've
    supplied pointers to `double' variables instead. Similarly,
    "%d" tells fscanf() to produce an `int', but you've provided
    a pointer to an `unsigned short'. fscanf() trusts you (not
    the best policy, it appears ;-), and when it tries to store
    `float' values in `double' variables or an `int' value in
    an `unsigned short' variable, there's no telling what will
    happen.

    Hint #1: Go back to your C textbook and read the scanf()
    section again. Although the format strings look similar to
    those used with printf(), they are *not* the same at all.

    Hint #2: Some compilers are able to detect and warn
    about mismatches of this kind, if you ask them to do so.
    For example, if you're using gcc try the "-W -Wall" flags
    when you compile your code. Other compilers that can do
    this have their own incantations; consult the documentation.

    --
    Eric.Sosman@sun .com

    Comment

    • Mara Guida

      #3
      Re: compound 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 compound interest.
      >
      > #include<stdio. h>
      > #include<stdlib .h>
      > double intrest(double tot_amount,floa t percent)
      > {
      > double temp;
      > if(percent < 0.0 || percent > 100.0)
      > {
      > printf("un-reasonable intrest rate, enter again\n");
      > fscanf(stdin,"% f",percent);
      > }
      > temp = (tot_amount * percent) / 100;
      > return temp;
      > }
      > int main(void)
      > {
      > double intrest_rate,cp nd_intrest,prnc _amount,percent ;
      > unsigned short year,i;
      > printf("enter the percent of intrest,amount, year\n");
      > fscanf(stdin,"% f%f%d",&percent ,&prnc_amount,& year);[/color]
      You have a type mismatch: %f <= float; percent <= double
      You have a type mismatch: %f <= float; prnc_amount <= double
      You have a type mismatch: %d <= int; year <= unsigned short
      [color=blue]
      > if(year > 50)
      > {
      > printf("kindly input the reasonable year of return\n");
      > fscanf(stdin,"% d",&year);[/color]
      You have a type mismatch: %d <= int; year <= unsigned short
      [color=blue]
      > }
      > else
      > {
      > intrest_rate = intrest(prnc_am ount,percent);
      > cpnd_intrest = prnc_amount*(1+ intrest_rate);
      > for(i = year ; i< year ; i++)[/color]
      The body of the for loop is never executed
      [color=blue]
      > cpnd_intrest = cpnd_intrest * i;
      > }
      > printf("the compound intrest is %f\n",cpnd_intr est);
      > return 0;
      > }
      >
      > I am getting a strange output when I run the program.Kindly help.[/color]


      Set your compiler to output warning messages and treat them as errors.
      Correct the errors/warnings and rerun the program.

      Comment

      • August Karlstrom

        #4
        Re: compound 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 compound interest.
        >
        > #include<stdio. h>
        > #include<stdlib .h>
        > double intrest(double tot_amount,floa t percent)
        > {
        > double temp;
        > if(percent < 0.0 || percent > 100.0)
        > {
        > printf("un-reasonable intrest rate, enter again\n");
        > fscanf(stdin,"% f",percent);
        > }
        > temp = (tot_amount * percent) / 100;
        > return temp;
        > }
        > int main(void)
        > {
        > double intrest_rate,cp nd_intrest,prnc _amount,percent ;
        > unsigned short year,i;
        > printf("enter the percent of intrest,amount, year\n");
        > fscanf(stdin,"% f%f%d",&percent ,&prnc_amount,& year);
        > if(year > 50)
        > {
        > printf("kindly input the reasonable year of return\n");
        > fscanf(stdin,"% d",&year);
        > }
        > else
        > {
        > intrest_rate = intrest(prnc_am ount,percent);
        > cpnd_intrest = prnc_amount*(1+ intrest_rate);
        > for(i = year ; i< year ; i++)
        > cpnd_intrest = cpnd_intrest * i;
        > }
        > printf("the compound intrest is %f\n",cpnd_intr est);
        > return 0;
        > }
        >
        > I am getting a strange output when I run the program.Kindly help.[/color]

        Why do you post the same question twice? I think you should instead
        thank Antonio for the help you got with your previous posting.


        August

        --
        I am the "ILOVEGNU" signature virus. Just copy me to your
        signature. This email was infected under the terms of the GNU
        General Public License.

        Comment

        • Antonio Contreras

          #5
          Re: compound intrest program(beginne r)

          August Karlstrom wrote:[color=blue]
          > c_beginner wrote:[color=green]
          > > 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 compound interest.
          > >[/color][/color]

          <snip actual code>
          [color=blue][color=green]
          > > I am getting a strange output when I run the program.Kindly help.[/color]
          >
          > Why do you post the same question twice? I think you should instead
          > thank Antonio for the help you got with your previous posting.[/color]

          Actually the program is not exactly the same and he took into account
          some things I said (he changed some variables from float to double and
          added some code after my hints). This program also claims to compute a
          compound interest, the other one claimed to compute a simple interest.
          (And I say claimed because I did not check the program logic.)

          Anyway you're right in saying that some kind of acknowledgement
          would've been nice.

          Comment

          • hammrGRS@hotmail.com

            #6
            Re: compound 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 compound interest.
            >
            > #include<stdio. h>
            > #include<stdlib .h>
            > double intrest(double tot_amount,floa t percent)
            > {
            > double temp;
            > if(percent < 0.0 || percent > 100.0)
            > {
            > printf("un-reasonable intrest rate, enter again\n");[/color]
            printf("Invalid rate you dolt. Try again
            nimrod\n");[color=blue]
            > fscanf(stdin,"% f",percent);
            > }
            > temp = (tot_amount * percent) / 100;
            > return temp;
            > }
            > int main(void)
            > {
            > double intrest_rate,cp nd_intrest,prnc _amount,percent ;
            > unsigned short year,i;
            > printf("enter the percent of intrest,amount, year\n");
            > fscanf(stdin,"% f%f%d",&percent ,&prnc_amount,& year);
            > if(year > 50)
            > {
            > printf("kindly input the reasonable year of return\n");[/color]
            printf("please please please enter the year of
            return\n");
            printf("please? n");[color=blue]
            > fscanf(stdin,"% d",&year);
            > }
            > else
            > {
            > intrest_rate = intrest(prnc_am ount,percent);
            > cpnd_intrest = prnc_amount*(1+ intrest_rate);
            > for(i = year ; i< year ; i++)
            > cpnd_intrest = cpnd_intrest * i;
            > }
            > printf("the compound intrest is %f\n",cpnd_intr est);
            > return 0;
            > }
            >
            > I am getting a strange output when I run the program.Kindly help.[/color]

            That should correct the output problems.

            Comment

            • Martin Ambuhl

              #7
              Re: compound 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.[/color]

              See if this much shorter program helps you figure out your problems.
              Note specifically that many of your scanf specifiers are wrong.

              #include <stdio.h>
              #include <math.h>

              int main(void)
              {
              double principal, rate, periods;
              printf("enter interest rate per period, principal amount,\n"
              " & number of periods, separated by spaces: ");
              fflush(stdout);
              scanf("%lf%lf%l f", &rate, &principal, &periods);
              printf("the compound interest is %f\n",
              principal * (pow(1 + rate / 100, periods) - 1));
              return 0;
              }

              Comment

              Working...