Thread: Factorial

  1. #1
    Registered User alireza beygi's Avatar
    Join Date
    Dec 2011
    Location
    USA
    Posts
    17

    Factorial

    Hello Guys!
    What's the problem with this simple code?!
    Thanks.
    Code:
    #include <stdio.h>
    #include <conio.h>
    int main ()
    {
        int n;
        int fact = 1;
        for (;;)
        {
            printf ("enter a number:\n");
            scanf ("%d", &n);
            for (int i = 1; i <= n; i++)
            fact *= i;
            printf ("Factorial is :%d\n", fact);
            }
        getch ();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    Quote Originally Posted by alireza beygi View Post
    Hello Guys!
    What's the problem with this simple code?!
    Thanks.
    Code:
    #include <stdio.h>
    #include <conio.h>
    int main ()
    {
        int n;
        int fact = 1;
        for (;;)
        {
            printf ("enter a number:\n");
            scanf ("%d", &n);
            for (int i = 1; i <= n; i++)
            fact *= i;
            printf ("Factorial is :%d\n", fact);
            }
        getch ();
        return 0;
    }
    After each loop "for" value "fact" not restart to 1.In addtion,the loop will run forever.
    Last edited by cauberong09; 01-06-2012 at 04:37 AM.

  3. #3
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    106
    Quote Originally Posted by alireza beygi View Post
    Hello Guys!
    What's the problem with this simple code?!
    Thanks.
    Code:
    #include <stdio.h>
    #include <conio.h>
    int main ()
    {
        int n;
        int fact = 1;
        for (;;)
        {
            printf ("enter a number:\n");
            scanf ("%d", &n);
            for (int i = 1; i <= n; i++)
            fact *= i;
            printf ("Factorial is :%d\n", fact);
            }
        getch ();
        return 0;
    }
    it has a infinite loop problem.
    also, don't use <conio.h> and getch() , use getchar(); and <stdio.h> would suffice

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What's the problem with this simple code?!
    The problem is that you're not checking the result of the scanf call, so if the user types in something non-numeric then n will be uninitialised and the loop will loop an unknown number of times.

    If that's not the answer you were looking for then you need to explain the problem!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Factorial
    By dpp in forum C++ Programming
    Replies: 7
    Last Post: 02-02-2009, 02:00 AM
  2. Factorial
    By foxman in forum Contests Board
    Replies: 27
    Last Post: 07-11-2008, 06:59 PM
  3. FACTORIAL. Can you hel me about this
    By Melody in forum C Programming
    Replies: 57
    Last Post: 11-20-2007, 08:32 AM
  4. Factorial
    By DonW in forum C Programming
    Replies: 10
    Last Post: 09-19-2004, 04:53 PM
  5. how to add a factorial ()
    By correlcj in forum C++ Programming
    Replies: 14
    Last Post: 10-18-2002, 02:28 PM