Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem in printing factorial.............

problem in printing factorial.............

Scheduled Pinned Locked Moved C / C++ / MFC
helplearning
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chaudhari hemant1
    wrote on last edited by
    #1

    hello every one I am a beginner in c language I am trying to print factorial of number using the program given below...... #include<stdio.h> void main() {      int fact=1,counter=5;      clrscr();      while(counter>0)      {           fact = fact * counter;           counter--;      }      printf("fact of %d is: %d",counter,fact);      getch(); } I need output as <b>Fact of 5 is 120.</b>

    A L _ P 4 Replies Last reply
    0
    • C chaudhari hemant1

      hello every one I am a beginner in c language I am trying to print factorial of number using the program given below...... #include<stdio.h> void main() {      int fact=1,counter=5;      clrscr();      while(counter>0)      {           fact = fact * counter;           counter--;      }      printf("fact of %d is: %d",counter,fact);      getch(); } I need output as <b>Fact of 5 is 120.</b>

      A Offline
      A Offline
      Anurag Gandhi
      wrote on last edited by
      #2

      The program seems fine. What is the error you are getting?

      Anurag Gandhi. http://www.gandhisoft.com Life is a computer program and every one is the programmer of his own life.

      1 Reply Last reply
      0
      • C chaudhari hemant1

        hello every one I am a beginner in c language I am trying to print factorial of number using the program given below...... #include<stdio.h> void main() {      int fact=1,counter=5;      clrscr();      while(counter>0)      {           fact = fact * counter;           counter--;      }      printf("fact of %d is: %d",counter,fact);      getch(); } I need output as <b>Fact of 5 is 120.</b>

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        chaudhari.hemant1 wrote:

        printf("fact of %d is: %d",counter,fact);

        You are trying to print the value of counter as 5, but you have decremented it in your loop so its value now is zero.

        1 Reply Last reply
        0
        • C chaudhari hemant1

          hello every one I am a beginner in c language I am trying to print factorial of number using the program given below...... #include<stdio.h> void main() {      int fact=1,counter=5;      clrscr();      while(counter>0)      {           fact = fact * counter;           counter--;      }      printf("fact of %d is: %d",counter,fact);      getch(); } I need output as <b>Fact of 5 is 120.</b>

          _ Offline
          _ Offline
          __erfan__
          wrote on last edited by
          #4

          void main()
          {
          clrscr();
          int fact=1, counter=5;
          int term = 1;
          while(term <= counter)
          {
          fact = fact * term;
          term++;
          }
          printf("fact of %d is: %d", counter, fact);

           getch();
          

          }

          modified on Saturday, December 12, 2009 4:55 AM

          K 1 Reply Last reply
          0
          • _ __erfan__

            void main()
            {
            clrscr();
            int fact=1, counter=5;
            int term = 1;
            while(term <= counter)
            {
            fact = fact * term;
            term++;
            }
            printf("fact of %d is: %d", counter, fact);

             getch();
            

            }

            modified on Saturday, December 12, 2009 4:55 AM

            K Offline
            K Offline
            krmed
            wrote on last edited by
            #5

            And just how does this loop end?

            while(counter <= counter)

            Perhaps you meant

            while(term <= counter)

            Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

            _ 1 Reply Last reply
            0
            • C chaudhari hemant1

              hello every one I am a beginner in c language I am trying to print factorial of number using the program given below...... #include<stdio.h> void main() {      int fact=1,counter=5;      clrscr();      while(counter>0)      {           fact = fact * counter;           counter--;      }      printf("fact of %d is: %d",counter,fact);      getch(); } I need output as <b>Fact of 5 is 120.</b>

              P Offline
              P Offline
              Patcher32
              wrote on last edited by
              #6

              #include < stdio.h >

              void main()
              {
              int counter,fact=1,num=5;
              counter = num;
              clrscr();
              while(counter > 0)
              {
              fact = fact * counter;
              counter--;
              }
              printf("fact of %d is: %d",num,fact);

               getch();
              

              }

              1 Reply Last reply
              0
              • K krmed

                And just how does this loop end?

                while(counter <= counter)

                Perhaps you meant

                while(term <= counter)

                Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

                _ Offline
                _ Offline
                __erfan__
                wrote on last edited by
                #7

                yes. sorry. :) the code has been edited.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups