Why is it wrong??(About n!)

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

    Why is it wrong??(About n!)

    I want to calculate n!,and my C code is as follows:


    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    int nonnega_int, counter;
    unsigned long result = 1;

    printf("Enter a nonnegative integer (as n)\n");
    printf("and I'll calculate its factorial(EOF to end):\n");
    scanf("%d", &nonnega_int );

    while ( ( nonnega_int = getchar() ) != EOF ){
    if ( nonnega_int 0){
    for ( counter = 1; counter <= nonnega_int; counter++){
    result *= counter;
    }
    }

    else if ( nonnega_int = 0)
    result = 0;

    printf("n! = %lu\n", result);
    }

    system("pause") ;

    return 0;
    }


    but the result is always 3628800
    I don't know why it is wrong.
    Please help me check it out.


  • Ben Bacarisse

    #2
    Re: Why is it wrong??(About n!)

    upyzl <zj262144@163.c omwrites:
    I want to calculate n!,and my C code is as follows:
    I'd expect to see a factorial function but maybe you haven't covered
    functions yet.
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    int nonnega_int, counter;
    unsigned long result = 1;
    >
    printf("Enter a nonnegative integer (as n)\n");
    printf("and I'll calculate its factorial(EOF to end):\n");
    scanf("%d", &nonnega_int );
    You should check the result of this call.
    while ( ( nonnega_int = getchar() ) != EOF ){
    However, now you assign nonnega_int after having just read a number
    into it. The result of the getchar() call is not what you want to
    calculate the factorial of.

    What is the point of the while loop?
    if ( nonnega_int 0){
    for ( counter = 1; counter <= nonnega_int; counter++){
    result *= counter;
    }
    }
    >
    else if ( nonnega_int = 0)
    You meant == not =. Many people like to write these test the otehr
    way round (0 == nonnega_int) so you get a diagnostic when you misspell
    ==. Personally, I hate it, but then its years since I've made this
    particular mistake.
    result = 0;
    >
    printf("n! = %lu\n", result);
    }
    >
    system("pause") ;
    There must be a better way to see your output!
    return 0;
    }
    --
    Ben.

    Comment

    • Pilcrow

      #3
      Re: Why is it wrong??(About n!)

      On Wed, 29 Oct 2008 19:15:01 -0700 (PDT), upyzl <zj262144@163.c om>
      wrote:
      >I want to calculate n!,and my C code is as follows:
      >
      >
      >#include <stdio.h>
      >#include <stdlib.h>
      >int main()
      >{
      > int nonnega_int, counter;
      > unsigned long result = 1;
      >
      > printf("Enter a nonnegative integer (as n)\n");
      > printf("and I'll calculate its factorial(EOF to end):\n");
      > scanf("%d", &nonnega_int );
      >
      > while ( ( nonnega_int = getchar() ) != EOF ){
      > if ( nonnega_int 0){
      > for ( counter = 1; counter <= nonnega_int; counter++){
      > result *= counter;
      > }
      > }
      >
      > else if ( nonnega_int = 0)
      > result = 0;
      >
      > printf("n! = %lu\n", result);
      > }
      >
      > system("pause") ;
      >
      > return 0;
      >}
      >
      >
      >but the result is always 3628800
      >I don't know why it is wrong.
      >Please help me check it out.
      >
      notice that 10! = 3628800, which is your invariable result.


      Comment

      • Keith Thompson

        #4
        Re: Why is it wrong??(About n!)

        upyzl <zj262144@163.c omwrites:
        I want to calculate n!,and my C code is as follows:
        >
        >
        #include <stdio.h>
        #include <stdlib.h>
        int main()
        {
        int nonnega_int, counter;
        unsigned long result = 1;
        >
        printf("Enter a nonnegative integer (as n)\n");
        printf("and I'll calculate its factorial(EOF to end):\n");
        scanf("%d", &nonnega_int );
        Here you read an int value from stdin, storing it in nonnega_int.
        while ( ( nonnega_int = getchar() ) != EOF ){
        And here you clobber whatever value you just stored in nonnega_int,
        replacing it with the value of the next character read from stdin. If
        the next character happens to be a new-line ('\n'), and if the value
        of '\n' happens to be 10 on your system (which it almost certainly
        is), then you compute '\n'!.

        Presumably you want to read a sequence of integer values. Why do you
        have a loop reading individual characters?

        [snip]

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        Nokia
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • upyzl

          #5
          Re: Why is it wrong??(About n!)

          Now I change it into:

          #include <stdio.h>
          #include <stdlib.h>
          int main()
          {
          int nonnega_int, counter;
          unsigned long result = 1;

          printf("Enter a nonnegative integer\n");
          printf("and I'll calculate its factorial(EOF to end):\n");
          scanf("%d", &nonnega_int );

          while ( ( nonnega_int = getchar() ) != EOF ){
          if ( nonnega_int 0){
          for ( counter = 1; counter <= nonnega_int; counter++){
          result *= counter;
          }
          }
          else if ( nonnega_int < 0){
          printf("Error Input\n");
          }
          printf("%d! = %lu\n", nonnega_int, result);
          }

          system("pause") ;

          return 0;
          }

          I know that now my only problem is "while ( ( nonnega_int =
          getchar() ) != EOF )"
          but I want to loop as what user want.
          Should I add "int loop" or?

          I must say, I haven't studied about "goto" and of course I don't know
          how to use it.
          In fact, now I only know use "while ( ( XXXX = getchar() ) != EOF )"
          to control loop times,
          and I think it should work.

          What should I do?

          Comment

          • upyzl

            #6
            Re: Why is it wrong??(About n!)

            Now I change it into:

            #include <stdio.h>
            #include <stdlib.h>
            int main()
            {
            int nonnega_int, counter;
            unsigned long result = 1;


            printf("Enter a nonnegative integer\n");
            printf("and I'll calculate its factorial(EOF to end):\n");
            scanf("%d", &nonnega_int );


            while ( ( nonnega_int = getchar() ) != EOF ){
            if ( nonnega_int 0){
            for ( counter = 1; counter <= nonnega_int;
            counter++){
            result *= counter;
            }
            }
            if ( nonnega_int == 0){
            result = 1;
            }
            else {
            printf("Error Input\n");
            }
            printf("%d! = %lu\n", nonnega_int, result);
            }


            system("pause") ;


            return 0;



            }


            I know that now my only problem is "while ( ( nonnega_int =
            getchar() ) != EOF )"
            but I want to loop as what user want.
            Should I add "int loop" or?

            I must say, I haven't studied about "goto" and of course I don't know
            how to use it.
            In fact, now I only know use "while ( ( XXXX = getchar() ) != EOF )"
            to control loop times,
            and I think it should work.


            What should I do?

            Comment

            • upyzl

              #7
              Re: Why is it wrong??(About n!)

              Now I change it into:

              #include <stdio.h>
              #include <stdlib.h>
              int main()
              {
              int nonnega_int, counter;
              unsigned long result = 1;


              printf("Enter a nonnegative integer\n");
              printf("and I'll calculate its factorial(EOF to end):\n");
              scanf("%d", &nonnega_int );


              while ( ( nonnega_int = getchar() ) != EOF ){
              if ( nonnega_int 0){
              for ( counter = 1; counter <=
              nonnega_int;cou nter++){
              result *= counter;
              }
              }
              else if ( nonnega_int == 0){
              result = 1;
              }
              else {
              printf("Error Input\n");
              }
              printf("%d! = %lu\n", nonnega_int, result);
              }


              system("pause") ;


              return 0;



              }


              I know that now my only problem is "while ( ( nonnega_int =
              getchar() ) != EOF )"
              but I want to loop as what user want.
              Should I add "int loop" or?

              I must say, I haven't studied about "goto" and of course I don't know
              how to use it.
              In fact, now I only know use "while ( ( XXXX = getchar() ) != EOF )"
              to control loop times,
              and I think it should work.


              What should I do?

              Comment

              • Mark McIntyre

                #8
                Re: Why is it wrong??(About n!)

                upyzl wrote:
                I want to calculate n!,and my C code is as follows:
                >
                >
                #include <stdio.h>
                #include <stdlib.h>
                int main()
                {
                int nonnega_int, counter;
                unsigned long result = 1;
                >
                printf("Enter a nonnegative integer (as n)\n");
                printf("and I'll calculate its factorial(EOF to end):\n");
                scanf("%d", &nonnega_int );
                don't use scanf. See hte C FAQ for why.
                while ( ( nonnega_int = getchar() ) != EOF ){
                You just read something into nonnega_int, and now you're overwriting it
                with the retirn from getchar...
                if ( nonnega_int 0){
                for ( counter = 1; counter <= nonnega_int; counter++){
                in C, the normal idiom is to start from zero, not one. You /can/ start
                from one, but your code will be potentially confusing to others and
                you'll run into problems when you start using arrays.
                but the result is always 3628800
                Check the value of nonnega_int immediately before you enter the 'for' loop.

                --
                Mark McIntyre

                CLC FAQ <http://c-faq.com/>
                CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                Comment

                • Fred

                  #9
                  Re: Why is it wrong??(About n!)

                  On Oct 30, 6:54 am, Mark McIntyre <markmcint...@T ROUSERSspamcop. net>
                  wrote:
                  upyzl wrote:
                  I want to calculate n!,and my C code is as follows:
                  >
                  #include <stdio.h>
                  #include <stdlib.h>
                  int main()
                  {
                     int nonnega_int, counter;
                     unsigned long result = 1;
                  >
                     printf("Enter a nonnegative integer (as n)\n");
                     printf("and I'll calculate its factorial(EOF to end):\n");
                     scanf("%d", &nonnega_int );
                  >
                  don't use scanf. See hte C FAQ for why.
                  >
                     while ( ( nonnega_int = getchar() ) != EOF ){
                  >
                  You just read something into nonnega_int, and now you're overwriting it
                  with the retirn from getchar...
                  >
                             if ( nonnega_int 0){
                                     for ( counter = 1; counter <= nonnega_int; counter++){
                  >
                  in C, the normal idiom is to start from zero, not one.  You /can/ start
                  from one, but your code will be potentially confusing to others and
                  you'll run into problems when you start using arrays.
                  >
                  but the result is always 3628800
                  >
                  Check the value of nonnega_int immediately before you enter the 'for' loop.
                  >
                  And unless the value of "result" is reset to one before entering
                  the for-loop, only the first time can produce the desired answer.
                  --
                  Fred Kleinschmidt

                  Comment

                  • user923005

                    #10
                    Re: Why is it wrong??(About n!)

                    On Oct 29, 7:15 pm, upyzl <zj262...@163.c omwrote:
                    I want to calculate n!,and my C code is as follows:
                    [snip]
                    Since the useful domain is very small for any native C data type, use
                    a table.
                    On a system with 128 bit floating point, there are still less than
                    2000 entries in the table.

                    If you are calculating n! to do probability studies, then I suggest
                    creation of Pascal's triangle and storing that.
                    Then all of your calculations are simple lookups.

                    Comment

                    Working...