Factorial program c using recursive function in C with while loop












2















Factorial program using recursion in c with while loop.In this program once the execution reaches the function return statement it will not go back to the function call. Instead,it executes the function repeatedly. can anybody please tell me what's wrong in this program.



#include<stdio.h>    
int fact(int n)
{
int x=1;

while(n>1)
{
x=n*fact(n-1);
}

return(x);
}

void main()
{
int n,fact1;
scanf("%d",&n);
fact1=fact(n);
printf("%d",fact1);
}









share|improve this question




















  • 5





    Does changing the while to an if solve the problem?

    – Peter de Rivaz
    Feb 11 '12 at 19:25






  • 1





    Your program has an infinite loop when n > 1, see Peter's comment

    – Seth Carnegie
    Feb 11 '12 at 19:27













  • BTW, if this is a homework problem, please tag it with homework.

    – Caleb
    Feb 11 '12 at 19:30






  • 2





    Just thought you might appreciate some input. Your approach to the problem (implementing factorial recursively?) is wrong. There are two ways to implement factorial, and it seems like you're combining them -- rethink how it works.

    – Pochi
    Feb 11 '12 at 19:58
















2















Factorial program using recursion in c with while loop.In this program once the execution reaches the function return statement it will not go back to the function call. Instead,it executes the function repeatedly. can anybody please tell me what's wrong in this program.



#include<stdio.h>    
int fact(int n)
{
int x=1;

while(n>1)
{
x=n*fact(n-1);
}

return(x);
}

void main()
{
int n,fact1;
scanf("%d",&n);
fact1=fact(n);
printf("%d",fact1);
}









share|improve this question




















  • 5





    Does changing the while to an if solve the problem?

    – Peter de Rivaz
    Feb 11 '12 at 19:25






  • 1





    Your program has an infinite loop when n > 1, see Peter's comment

    – Seth Carnegie
    Feb 11 '12 at 19:27













  • BTW, if this is a homework problem, please tag it with homework.

    – Caleb
    Feb 11 '12 at 19:30






  • 2





    Just thought you might appreciate some input. Your approach to the problem (implementing factorial recursively?) is wrong. There are two ways to implement factorial, and it seems like you're combining them -- rethink how it works.

    – Pochi
    Feb 11 '12 at 19:58














2












2








2








Factorial program using recursion in c with while loop.In this program once the execution reaches the function return statement it will not go back to the function call. Instead,it executes the function repeatedly. can anybody please tell me what's wrong in this program.



#include<stdio.h>    
int fact(int n)
{
int x=1;

while(n>1)
{
x=n*fact(n-1);
}

return(x);
}

void main()
{
int n,fact1;
scanf("%d",&n);
fact1=fact(n);
printf("%d",fact1);
}









share|improve this question
















Factorial program using recursion in c with while loop.In this program once the execution reaches the function return statement it will not go back to the function call. Instead,it executes the function repeatedly. can anybody please tell me what's wrong in this program.



#include<stdio.h>    
int fact(int n)
{
int x=1;

while(n>1)
{
x=n*fact(n-1);
}

return(x);
}

void main()
{
int n,fact1;
scanf("%d",&n);
fact1=fact(n);
printf("%d",fact1);
}






c recursion factorial






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 11 '12 at 19:27









Seth Carnegie

60.4k14149215




60.4k14149215










asked Feb 11 '12 at 19:23









MeiMei

50234




50234








  • 5





    Does changing the while to an if solve the problem?

    – Peter de Rivaz
    Feb 11 '12 at 19:25






  • 1





    Your program has an infinite loop when n > 1, see Peter's comment

    – Seth Carnegie
    Feb 11 '12 at 19:27













  • BTW, if this is a homework problem, please tag it with homework.

    – Caleb
    Feb 11 '12 at 19:30






  • 2





    Just thought you might appreciate some input. Your approach to the problem (implementing factorial recursively?) is wrong. There are two ways to implement factorial, and it seems like you're combining them -- rethink how it works.

    – Pochi
    Feb 11 '12 at 19:58














  • 5





    Does changing the while to an if solve the problem?

    – Peter de Rivaz
    Feb 11 '12 at 19:25






  • 1





    Your program has an infinite loop when n > 1, see Peter's comment

    – Seth Carnegie
    Feb 11 '12 at 19:27













  • BTW, if this is a homework problem, please tag it with homework.

    – Caleb
    Feb 11 '12 at 19:30






  • 2





    Just thought you might appreciate some input. Your approach to the problem (implementing factorial recursively?) is wrong. There are two ways to implement factorial, and it seems like you're combining them -- rethink how it works.

    – Pochi
    Feb 11 '12 at 19:58








5




5





Does changing the while to an if solve the problem?

– Peter de Rivaz
Feb 11 '12 at 19:25





Does changing the while to an if solve the problem?

– Peter de Rivaz
Feb 11 '12 at 19:25




1




1





Your program has an infinite loop when n > 1, see Peter's comment

– Seth Carnegie
Feb 11 '12 at 19:27







Your program has an infinite loop when n > 1, see Peter's comment

– Seth Carnegie
Feb 11 '12 at 19:27















BTW, if this is a homework problem, please tag it with homework.

– Caleb
Feb 11 '12 at 19:30





BTW, if this is a homework problem, please tag it with homework.

– Caleb
Feb 11 '12 at 19:30




2




2





Just thought you might appreciate some input. Your approach to the problem (implementing factorial recursively?) is wrong. There are two ways to implement factorial, and it seems like you're combining them -- rethink how it works.

– Pochi
Feb 11 '12 at 19:58





Just thought you might appreciate some input. Your approach to the problem (implementing factorial recursively?) is wrong. There are two ways to implement factorial, and it seems like you're combining them -- rethink how it works.

– Pochi
Feb 11 '12 at 19:58












9 Answers
9






active

oldest

votes


















6














The reason that your program gets into an infinite loop is that the loop



while (n > 1)
x = n * fact(n-1);


never decrements n. Since n never decreases, the program will never leave the loop. Peter is correct in the comments: change the while to an if, and you will have a factorial function that handles all positive parameters correctly. However, even after changing while to if, your fact won't have the property that fact(0) == 1, as is required for a correct factorial function.






share|improve this answer































    5














    This



    while(n>1)


    is causing the looping. You don't change n inside the loop, so the loop is infinite.



    Change while to if.






    share|improve this answer































      3














      This is the method of factorial:



      public int fact(int n)
      {
      if (n < 1)
      {
      return 1;
      }
      else
      {
      return n * fact(n - 1);
      }
      }





      share|improve this answer

































        2














        #include <stdio.h>
        #include <stdlib.h>

        /** main returns int, use it! */

        int main(int argc, char **argv)
        {

        if (argc <= 2) {
        if (argv) argc = atoi(argv[1] );
        else return argc;
        }

        argc *= main (argc-1, NULL);

        if (argv) {
        printf("=%dn", argc);
        return 0;
        }
        return argc;
        }





        share|improve this answer































          2














          /*
          Write a C++ Program to input a positive number,
          Calculate and display factorial of this number
          by recursion.
          */
          #include<iostream.h>

          #include<conio.h>

          long factorial(int n);
          void main()
          {

          clrscr();

          int number, counter;


          label1:

          cout<<"n Enter the Number = ";

          cin>>number;

          if ( number < 0)
          {
          cout<<"n Enter a non negative number, please!";
          goto label1;
          }
          cout<<"nn ----------- Results ------------";

          cout<<"nn The Factorial of the number "<<number<<"n is "<<factorial(number);

          getch();

          }

          long factorial(int n)

          {

          if ( n == 0 )
          return 1;
          else
          return n * factorial(n-1);

          }





          share|improve this answer
























          • OP asked for C, not c++

            – Mawg
            May 8 '15 at 12:34



















          2














          You can use the simple approach using recursion



          #include <stdio.h>

          int fact(int n)
          {
          if(n==1)
          return 1;
          else
          return n * fact(n-1);
          }
          int main()
          {
          int f;
          f = fact(5);
          printf("Factorial = %d",f);
          return 0;
          }


          Read it more C program to find factorial using recursion






          share|improve this answer

































            1














            /*several versions of a factorial program.*/

            #include<stdio.h>
            int main()
            {
            int n;
            long factorial;
            printf("Compute the factorial of what number? ");
            scanf("%d", &n);
            factorial = 1L;
            while(n > 0)
            factorial *= n--;
            printf("The factorial is %ldn", factorial);
            return 0;
            }

            #include<stdio.h>
            /*the same, but counting up to n instead of down to 0*/
            int main()
            {
            register int count;
            int n;
            long factorial;
            printf("Compute the factorial of what number? ");
            scanf("%d", &n);
            factorial = 1L;
            count = 1;
            while(count <= n)
            factorial *= count++;
            printf("%d! = %ldn", n, factorial);
            return 0;
            }


            #include<stdio.h>
            /*an equivalent loop using 'for' instead of 'while'*/
            int main()
            {
            register int count;
            int n;
            long factorial;
            printf("Compute the factorial of what number? ");
            scanf("%d", &n);
            for(factorial = 1L, count = 1; count <= n; count++)
            factorial *= count;
            printf("%d! = %ldn", n, factorial);
            return 0;
            }





            share|improve this answer































              0














              /*WAP to find factorial using recursion*/

              #include<stdio.h>
              #include<stdlib.h>

              int fact1=1;

              int fact(int no)
              {
              fact1=fact1*no;
              no--;
              if(no!=1)
              {
              fact(no);
              }
              return fact1;
              }

              int main()
              {
              int no,ans;``
              system("clear");
              printf("Enter a no. : ");
              scanf("%d",&no);
              ans=fact(no);
              printf("Fact : %d",ans);
              return 0;
              }





              share|improve this answer































                0














                Factorial program using recursion in C with while loop.



                int fact(int n)
                {
                int x=1;

                while(n>=1)
                {
                return(n*fact(n-1));
                }

                return(1);
                }





                share|improve this answer























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9243312%2ffactorial-program-c-using-recursive-function-in-c-with-while-loop%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  9 Answers
                  9






                  active

                  oldest

                  votes








                  9 Answers
                  9






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  6














                  The reason that your program gets into an infinite loop is that the loop



                  while (n > 1)
                  x = n * fact(n-1);


                  never decrements n. Since n never decreases, the program will never leave the loop. Peter is correct in the comments: change the while to an if, and you will have a factorial function that handles all positive parameters correctly. However, even after changing while to if, your fact won't have the property that fact(0) == 1, as is required for a correct factorial function.






                  share|improve this answer




























                    6














                    The reason that your program gets into an infinite loop is that the loop



                    while (n > 1)
                    x = n * fact(n-1);


                    never decrements n. Since n never decreases, the program will never leave the loop. Peter is correct in the comments: change the while to an if, and you will have a factorial function that handles all positive parameters correctly. However, even after changing while to if, your fact won't have the property that fact(0) == 1, as is required for a correct factorial function.






                    share|improve this answer


























                      6












                      6








                      6







                      The reason that your program gets into an infinite loop is that the loop



                      while (n > 1)
                      x = n * fact(n-1);


                      never decrements n. Since n never decreases, the program will never leave the loop. Peter is correct in the comments: change the while to an if, and you will have a factorial function that handles all positive parameters correctly. However, even after changing while to if, your fact won't have the property that fact(0) == 1, as is required for a correct factorial function.






                      share|improve this answer













                      The reason that your program gets into an infinite loop is that the loop



                      while (n > 1)
                      x = n * fact(n-1);


                      never decrements n. Since n never decreases, the program will never leave the loop. Peter is correct in the comments: change the while to an if, and you will have a factorial function that handles all positive parameters correctly. However, even after changing while to if, your fact won't have the property that fact(0) == 1, as is required for a correct factorial function.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Feb 11 '12 at 19:29









                      Adam MihalcinAdam Mihalcin

                      11.9k32442




                      11.9k32442

























                          5














                          This



                          while(n>1)


                          is causing the looping. You don't change n inside the loop, so the loop is infinite.



                          Change while to if.






                          share|improve this answer




























                            5














                            This



                            while(n>1)


                            is causing the looping. You don't change n inside the loop, so the loop is infinite.



                            Change while to if.






                            share|improve this answer


























                              5












                              5








                              5







                              This



                              while(n>1)


                              is causing the looping. You don't change n inside the loop, so the loop is infinite.



                              Change while to if.






                              share|improve this answer













                              This



                              while(n>1)


                              is causing the looping. You don't change n inside the loop, so the loop is infinite.



                              Change while to if.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Feb 11 '12 at 19:29









                              jpalecekjpalecek

                              40.6k581127




                              40.6k581127























                                  3














                                  This is the method of factorial:



                                  public int fact(int n)
                                  {
                                  if (n < 1)
                                  {
                                  return 1;
                                  }
                                  else
                                  {
                                  return n * fact(n - 1);
                                  }
                                  }





                                  share|improve this answer






























                                    3














                                    This is the method of factorial:



                                    public int fact(int n)
                                    {
                                    if (n < 1)
                                    {
                                    return 1;
                                    }
                                    else
                                    {
                                    return n * fact(n - 1);
                                    }
                                    }





                                    share|improve this answer




























                                      3












                                      3








                                      3







                                      This is the method of factorial:



                                      public int fact(int n)
                                      {
                                      if (n < 1)
                                      {
                                      return 1;
                                      }
                                      else
                                      {
                                      return n * fact(n - 1);
                                      }
                                      }





                                      share|improve this answer















                                      This is the method of factorial:



                                      public int fact(int n)
                                      {
                                      if (n < 1)
                                      {
                                      return 1;
                                      }
                                      else
                                      {
                                      return n * fact(n - 1);
                                      }
                                      }






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Sep 6 '13 at 9:30









                                      Sankumarsingh

                                      7,58274063




                                      7,58274063










                                      answered Sep 6 '13 at 8:57









                                      HieuHieu

                                      311




                                      311























                                          2














                                          #include <stdio.h>
                                          #include <stdlib.h>

                                          /** main returns int, use it! */

                                          int main(int argc, char **argv)
                                          {

                                          if (argc <= 2) {
                                          if (argv) argc = atoi(argv[1] );
                                          else return argc;
                                          }

                                          argc *= main (argc-1, NULL);

                                          if (argv) {
                                          printf("=%dn", argc);
                                          return 0;
                                          }
                                          return argc;
                                          }





                                          share|improve this answer




























                                            2














                                            #include <stdio.h>
                                            #include <stdlib.h>

                                            /** main returns int, use it! */

                                            int main(int argc, char **argv)
                                            {

                                            if (argc <= 2) {
                                            if (argv) argc = atoi(argv[1] );
                                            else return argc;
                                            }

                                            argc *= main (argc-1, NULL);

                                            if (argv) {
                                            printf("=%dn", argc);
                                            return 0;
                                            }
                                            return argc;
                                            }





                                            share|improve this answer


























                                              2












                                              2








                                              2







                                              #include <stdio.h>
                                              #include <stdlib.h>

                                              /** main returns int, use it! */

                                              int main(int argc, char **argv)
                                              {

                                              if (argc <= 2) {
                                              if (argv) argc = atoi(argv[1] );
                                              else return argc;
                                              }

                                              argc *= main (argc-1, NULL);

                                              if (argv) {
                                              printf("=%dn", argc);
                                              return 0;
                                              }
                                              return argc;
                                              }





                                              share|improve this answer













                                              #include <stdio.h>
                                              #include <stdlib.h>

                                              /** main returns int, use it! */

                                              int main(int argc, char **argv)
                                              {

                                              if (argc <= 2) {
                                              if (argv) argc = atoi(argv[1] );
                                              else return argc;
                                              }

                                              argc *= main (argc-1, NULL);

                                              if (argv) {
                                              printf("=%dn", argc);
                                              return 0;
                                              }
                                              return argc;
                                              }






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Jul 11 '12 at 9:31









                                              wildplasserwildplasser

                                              30.9k53968




                                              30.9k53968























                                                  2














                                                  /*
                                                  Write a C++ Program to input a positive number,
                                                  Calculate and display factorial of this number
                                                  by recursion.
                                                  */
                                                  #include<iostream.h>

                                                  #include<conio.h>

                                                  long factorial(int n);
                                                  void main()
                                                  {

                                                  clrscr();

                                                  int number, counter;


                                                  label1:

                                                  cout<<"n Enter the Number = ";

                                                  cin>>number;

                                                  if ( number < 0)
                                                  {
                                                  cout<<"n Enter a non negative number, please!";
                                                  goto label1;
                                                  }
                                                  cout<<"nn ----------- Results ------------";

                                                  cout<<"nn The Factorial of the number "<<number<<"n is "<<factorial(number);

                                                  getch();

                                                  }

                                                  long factorial(int n)

                                                  {

                                                  if ( n == 0 )
                                                  return 1;
                                                  else
                                                  return n * factorial(n-1);

                                                  }





                                                  share|improve this answer
























                                                  • OP asked for C, not c++

                                                    – Mawg
                                                    May 8 '15 at 12:34
















                                                  2














                                                  /*
                                                  Write a C++ Program to input a positive number,
                                                  Calculate and display factorial of this number
                                                  by recursion.
                                                  */
                                                  #include<iostream.h>

                                                  #include<conio.h>

                                                  long factorial(int n);
                                                  void main()
                                                  {

                                                  clrscr();

                                                  int number, counter;


                                                  label1:

                                                  cout<<"n Enter the Number = ";

                                                  cin>>number;

                                                  if ( number < 0)
                                                  {
                                                  cout<<"n Enter a non negative number, please!";
                                                  goto label1;
                                                  }
                                                  cout<<"nn ----------- Results ------------";

                                                  cout<<"nn The Factorial of the number "<<number<<"n is "<<factorial(number);

                                                  getch();

                                                  }

                                                  long factorial(int n)

                                                  {

                                                  if ( n == 0 )
                                                  return 1;
                                                  else
                                                  return n * factorial(n-1);

                                                  }





                                                  share|improve this answer
























                                                  • OP asked for C, not c++

                                                    – Mawg
                                                    May 8 '15 at 12:34














                                                  2












                                                  2








                                                  2







                                                  /*
                                                  Write a C++ Program to input a positive number,
                                                  Calculate and display factorial of this number
                                                  by recursion.
                                                  */
                                                  #include<iostream.h>

                                                  #include<conio.h>

                                                  long factorial(int n);
                                                  void main()
                                                  {

                                                  clrscr();

                                                  int number, counter;


                                                  label1:

                                                  cout<<"n Enter the Number = ";

                                                  cin>>number;

                                                  if ( number < 0)
                                                  {
                                                  cout<<"n Enter a non negative number, please!";
                                                  goto label1;
                                                  }
                                                  cout<<"nn ----------- Results ------------";

                                                  cout<<"nn The Factorial of the number "<<number<<"n is "<<factorial(number);

                                                  getch();

                                                  }

                                                  long factorial(int n)

                                                  {

                                                  if ( n == 0 )
                                                  return 1;
                                                  else
                                                  return n * factorial(n-1);

                                                  }





                                                  share|improve this answer













                                                  /*
                                                  Write a C++ Program to input a positive number,
                                                  Calculate and display factorial of this number
                                                  by recursion.
                                                  */
                                                  #include<iostream.h>

                                                  #include<conio.h>

                                                  long factorial(int n);
                                                  void main()
                                                  {

                                                  clrscr();

                                                  int number, counter;


                                                  label1:

                                                  cout<<"n Enter the Number = ";

                                                  cin>>number;

                                                  if ( number < 0)
                                                  {
                                                  cout<<"n Enter a non negative number, please!";
                                                  goto label1;
                                                  }
                                                  cout<<"nn ----------- Results ------------";

                                                  cout<<"nn The Factorial of the number "<<number<<"n is "<<factorial(number);

                                                  getch();

                                                  }

                                                  long factorial(int n)

                                                  {

                                                  if ( n == 0 )
                                                  return 1;
                                                  else
                                                  return n * factorial(n-1);

                                                  }






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Dec 11 '13 at 10:02









                                                  Perfect Computer NotesPerfect Computer Notes

                                                  313




                                                  313













                                                  • OP asked for C, not c++

                                                    – Mawg
                                                    May 8 '15 at 12:34



















                                                  • OP asked for C, not c++

                                                    – Mawg
                                                    May 8 '15 at 12:34

















                                                  OP asked for C, not c++

                                                  – Mawg
                                                  May 8 '15 at 12:34





                                                  OP asked for C, not c++

                                                  – Mawg
                                                  May 8 '15 at 12:34











                                                  2














                                                  You can use the simple approach using recursion



                                                  #include <stdio.h>

                                                  int fact(int n)
                                                  {
                                                  if(n==1)
                                                  return 1;
                                                  else
                                                  return n * fact(n-1);
                                                  }
                                                  int main()
                                                  {
                                                  int f;
                                                  f = fact(5);
                                                  printf("Factorial = %d",f);
                                                  return 0;
                                                  }


                                                  Read it more C program to find factorial using recursion






                                                  share|improve this answer






























                                                    2














                                                    You can use the simple approach using recursion



                                                    #include <stdio.h>

                                                    int fact(int n)
                                                    {
                                                    if(n==1)
                                                    return 1;
                                                    else
                                                    return n * fact(n-1);
                                                    }
                                                    int main()
                                                    {
                                                    int f;
                                                    f = fact(5);
                                                    printf("Factorial = %d",f);
                                                    return 0;
                                                    }


                                                    Read it more C program to find factorial using recursion






                                                    share|improve this answer




























                                                      2












                                                      2








                                                      2







                                                      You can use the simple approach using recursion



                                                      #include <stdio.h>

                                                      int fact(int n)
                                                      {
                                                      if(n==1)
                                                      return 1;
                                                      else
                                                      return n * fact(n-1);
                                                      }
                                                      int main()
                                                      {
                                                      int f;
                                                      f = fact(5);
                                                      printf("Factorial = %d",f);
                                                      return 0;
                                                      }


                                                      Read it more C program to find factorial using recursion






                                                      share|improve this answer















                                                      You can use the simple approach using recursion



                                                      #include <stdio.h>

                                                      int fact(int n)
                                                      {
                                                      if(n==1)
                                                      return 1;
                                                      else
                                                      return n * fact(n-1);
                                                      }
                                                      int main()
                                                      {
                                                      int f;
                                                      f = fact(5);
                                                      printf("Factorial = %d",f);
                                                      return 0;
                                                      }


                                                      Read it more C program to find factorial using recursion







                                                      share|improve this answer














                                                      share|improve this answer



                                                      share|improve this answer








                                                      edited Aug 28 '17 at 13:55

























                                                      answered Apr 28 '15 at 3:12









                                                      Pankaj PrakashPankaj Prakash

                                                      773919




                                                      773919























                                                          1














                                                          /*several versions of a factorial program.*/

                                                          #include<stdio.h>
                                                          int main()
                                                          {
                                                          int n;
                                                          long factorial;
                                                          printf("Compute the factorial of what number? ");
                                                          scanf("%d", &n);
                                                          factorial = 1L;
                                                          while(n > 0)
                                                          factorial *= n--;
                                                          printf("The factorial is %ldn", factorial);
                                                          return 0;
                                                          }

                                                          #include<stdio.h>
                                                          /*the same, but counting up to n instead of down to 0*/
                                                          int main()
                                                          {
                                                          register int count;
                                                          int n;
                                                          long factorial;
                                                          printf("Compute the factorial of what number? ");
                                                          scanf("%d", &n);
                                                          factorial = 1L;
                                                          count = 1;
                                                          while(count <= n)
                                                          factorial *= count++;
                                                          printf("%d! = %ldn", n, factorial);
                                                          return 0;
                                                          }


                                                          #include<stdio.h>
                                                          /*an equivalent loop using 'for' instead of 'while'*/
                                                          int main()
                                                          {
                                                          register int count;
                                                          int n;
                                                          long factorial;
                                                          printf("Compute the factorial of what number? ");
                                                          scanf("%d", &n);
                                                          for(factorial = 1L, count = 1; count <= n; count++)
                                                          factorial *= count;
                                                          printf("%d! = %ldn", n, factorial);
                                                          return 0;
                                                          }





                                                          share|improve this answer




























                                                            1














                                                            /*several versions of a factorial program.*/

                                                            #include<stdio.h>
                                                            int main()
                                                            {
                                                            int n;
                                                            long factorial;
                                                            printf("Compute the factorial of what number? ");
                                                            scanf("%d", &n);
                                                            factorial = 1L;
                                                            while(n > 0)
                                                            factorial *= n--;
                                                            printf("The factorial is %ldn", factorial);
                                                            return 0;
                                                            }

                                                            #include<stdio.h>
                                                            /*the same, but counting up to n instead of down to 0*/
                                                            int main()
                                                            {
                                                            register int count;
                                                            int n;
                                                            long factorial;
                                                            printf("Compute the factorial of what number? ");
                                                            scanf("%d", &n);
                                                            factorial = 1L;
                                                            count = 1;
                                                            while(count <= n)
                                                            factorial *= count++;
                                                            printf("%d! = %ldn", n, factorial);
                                                            return 0;
                                                            }


                                                            #include<stdio.h>
                                                            /*an equivalent loop using 'for' instead of 'while'*/
                                                            int main()
                                                            {
                                                            register int count;
                                                            int n;
                                                            long factorial;
                                                            printf("Compute the factorial of what number? ");
                                                            scanf("%d", &n);
                                                            for(factorial = 1L, count = 1; count <= n; count++)
                                                            factorial *= count;
                                                            printf("%d! = %ldn", n, factorial);
                                                            return 0;
                                                            }





                                                            share|improve this answer


























                                                              1












                                                              1








                                                              1







                                                              /*several versions of a factorial program.*/

                                                              #include<stdio.h>
                                                              int main()
                                                              {
                                                              int n;
                                                              long factorial;
                                                              printf("Compute the factorial of what number? ");
                                                              scanf("%d", &n);
                                                              factorial = 1L;
                                                              while(n > 0)
                                                              factorial *= n--;
                                                              printf("The factorial is %ldn", factorial);
                                                              return 0;
                                                              }

                                                              #include<stdio.h>
                                                              /*the same, but counting up to n instead of down to 0*/
                                                              int main()
                                                              {
                                                              register int count;
                                                              int n;
                                                              long factorial;
                                                              printf("Compute the factorial of what number? ");
                                                              scanf("%d", &n);
                                                              factorial = 1L;
                                                              count = 1;
                                                              while(count <= n)
                                                              factorial *= count++;
                                                              printf("%d! = %ldn", n, factorial);
                                                              return 0;
                                                              }


                                                              #include<stdio.h>
                                                              /*an equivalent loop using 'for' instead of 'while'*/
                                                              int main()
                                                              {
                                                              register int count;
                                                              int n;
                                                              long factorial;
                                                              printf("Compute the factorial of what number? ");
                                                              scanf("%d", &n);
                                                              for(factorial = 1L, count = 1; count <= n; count++)
                                                              factorial *= count;
                                                              printf("%d! = %ldn", n, factorial);
                                                              return 0;
                                                              }





                                                              share|improve this answer













                                                              /*several versions of a factorial program.*/

                                                              #include<stdio.h>
                                                              int main()
                                                              {
                                                              int n;
                                                              long factorial;
                                                              printf("Compute the factorial of what number? ");
                                                              scanf("%d", &n);
                                                              factorial = 1L;
                                                              while(n > 0)
                                                              factorial *= n--;
                                                              printf("The factorial is %ldn", factorial);
                                                              return 0;
                                                              }

                                                              #include<stdio.h>
                                                              /*the same, but counting up to n instead of down to 0*/
                                                              int main()
                                                              {
                                                              register int count;
                                                              int n;
                                                              long factorial;
                                                              printf("Compute the factorial of what number? ");
                                                              scanf("%d", &n);
                                                              factorial = 1L;
                                                              count = 1;
                                                              while(count <= n)
                                                              factorial *= count++;
                                                              printf("%d! = %ldn", n, factorial);
                                                              return 0;
                                                              }


                                                              #include<stdio.h>
                                                              /*an equivalent loop using 'for' instead of 'while'*/
                                                              int main()
                                                              {
                                                              register int count;
                                                              int n;
                                                              long factorial;
                                                              printf("Compute the factorial of what number? ");
                                                              scanf("%d", &n);
                                                              for(factorial = 1L, count = 1; count <= n; count++)
                                                              factorial *= count;
                                                              printf("%d! = %ldn", n, factorial);
                                                              return 0;
                                                              }






                                                              share|improve this answer












                                                              share|improve this answer



                                                              share|improve this answer










                                                              answered Feb 11 '12 at 19:42









                                                              cMinorcMinor

                                                              12.9k63198335




                                                              12.9k63198335























                                                                  0














                                                                  /*WAP to find factorial using recursion*/

                                                                  #include<stdio.h>
                                                                  #include<stdlib.h>

                                                                  int fact1=1;

                                                                  int fact(int no)
                                                                  {
                                                                  fact1=fact1*no;
                                                                  no--;
                                                                  if(no!=1)
                                                                  {
                                                                  fact(no);
                                                                  }
                                                                  return fact1;
                                                                  }

                                                                  int main()
                                                                  {
                                                                  int no,ans;``
                                                                  system("clear");
                                                                  printf("Enter a no. : ");
                                                                  scanf("%d",&no);
                                                                  ans=fact(no);
                                                                  printf("Fact : %d",ans);
                                                                  return 0;
                                                                  }





                                                                  share|improve this answer




























                                                                    0














                                                                    /*WAP to find factorial using recursion*/

                                                                    #include<stdio.h>
                                                                    #include<stdlib.h>

                                                                    int fact1=1;

                                                                    int fact(int no)
                                                                    {
                                                                    fact1=fact1*no;
                                                                    no--;
                                                                    if(no!=1)
                                                                    {
                                                                    fact(no);
                                                                    }
                                                                    return fact1;
                                                                    }

                                                                    int main()
                                                                    {
                                                                    int no,ans;``
                                                                    system("clear");
                                                                    printf("Enter a no. : ");
                                                                    scanf("%d",&no);
                                                                    ans=fact(no);
                                                                    printf("Fact : %d",ans);
                                                                    return 0;
                                                                    }





                                                                    share|improve this answer


























                                                                      0












                                                                      0








                                                                      0







                                                                      /*WAP to find factorial using recursion*/

                                                                      #include<stdio.h>
                                                                      #include<stdlib.h>

                                                                      int fact1=1;

                                                                      int fact(int no)
                                                                      {
                                                                      fact1=fact1*no;
                                                                      no--;
                                                                      if(no!=1)
                                                                      {
                                                                      fact(no);
                                                                      }
                                                                      return fact1;
                                                                      }

                                                                      int main()
                                                                      {
                                                                      int no,ans;``
                                                                      system("clear");
                                                                      printf("Enter a no. : ");
                                                                      scanf("%d",&no);
                                                                      ans=fact(no);
                                                                      printf("Fact : %d",ans);
                                                                      return 0;
                                                                      }





                                                                      share|improve this answer













                                                                      /*WAP to find factorial using recursion*/

                                                                      #include<stdio.h>
                                                                      #include<stdlib.h>

                                                                      int fact1=1;

                                                                      int fact(int no)
                                                                      {
                                                                      fact1=fact1*no;
                                                                      no--;
                                                                      if(no!=1)
                                                                      {
                                                                      fact(no);
                                                                      }
                                                                      return fact1;
                                                                      }

                                                                      int main()
                                                                      {
                                                                      int no,ans;``
                                                                      system("clear");
                                                                      printf("Enter a no. : ");
                                                                      scanf("%d",&no);
                                                                      ans=fact(no);
                                                                      printf("Fact : %d",ans);
                                                                      return 0;
                                                                      }






                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered Apr 3 '16 at 5:38









                                                                      Aman RaoAman Rao

                                                                      1




                                                                      1























                                                                          0














                                                                          Factorial program using recursion in C with while loop.



                                                                          int fact(int n)
                                                                          {
                                                                          int x=1;

                                                                          while(n>=1)
                                                                          {
                                                                          return(n*fact(n-1));
                                                                          }

                                                                          return(1);
                                                                          }





                                                                          share|improve this answer




























                                                                            0














                                                                            Factorial program using recursion in C with while loop.



                                                                            int fact(int n)
                                                                            {
                                                                            int x=1;

                                                                            while(n>=1)
                                                                            {
                                                                            return(n*fact(n-1));
                                                                            }

                                                                            return(1);
                                                                            }





                                                                            share|improve this answer


























                                                                              0












                                                                              0








                                                                              0







                                                                              Factorial program using recursion in C with while loop.



                                                                              int fact(int n)
                                                                              {
                                                                              int x=1;

                                                                              while(n>=1)
                                                                              {
                                                                              return(n*fact(n-1));
                                                                              }

                                                                              return(1);
                                                                              }





                                                                              share|improve this answer













                                                                              Factorial program using recursion in C with while loop.



                                                                              int fact(int n)
                                                                              {
                                                                              int x=1;

                                                                              while(n>=1)
                                                                              {
                                                                              return(n*fact(n-1));
                                                                              }

                                                                              return(1);
                                                                              }






                                                                              share|improve this answer












                                                                              share|improve this answer



                                                                              share|improve this answer










                                                                              answered Nov 13 '18 at 12:48









                                                                              Eksplorator DanychEksplorator Danych

                                                                              53




                                                                              53






























                                                                                  draft saved

                                                                                  draft discarded




















































                                                                                  Thanks for contributing an answer to Stack Overflow!


                                                                                  • Please be sure to answer the question. Provide details and share your research!

                                                                                  But avoid



                                                                                  • Asking for help, clarification, or responding to other answers.

                                                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                                                  To learn more, see our tips on writing great answers.




                                                                                  draft saved


                                                                                  draft discarded














                                                                                  StackExchange.ready(
                                                                                  function () {
                                                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9243312%2ffactorial-program-c-using-recursive-function-in-c-with-while-loop%23new-answer', 'question_page');
                                                                                  }
                                                                                  );

                                                                                  Post as a guest















                                                                                  Required, but never shown





















































                                                                                  Required, but never shown














                                                                                  Required, but never shown












                                                                                  Required, but never shown







                                                                                  Required, but never shown

































                                                                                  Required, but never shown














                                                                                  Required, but never shown












                                                                                  Required, but never shown







                                                                                  Required, but never shown







                                                                                  Popular posts from this blog

                                                                                  Bressuire

                                                                                  Vorschmack

                                                                                  Quarantine