Thread: What's wrong with this For Loop in C

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    2

    Question What's wrong with this For Loop in C

    Code:
     
    #include<stdio.h>
    int main ()
    
    {
    int i,number,factorial;
    
    printf("Enter the Number for Factorial\n");
    
    scanf("%d",&number);
                        for(i = 1;i <= number; i++)
                        {factorial = number*i;}
    
    printf("\n %d ! = %d", number , factorial); 
    
    getch();   
    
    }
    I am new to Programming. I was trying to write a code to get factorials. It seems to me that the the above code doesn't work. What am i doing wrong?


  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    factorial=factorial*i is what it should be or else you are just multiplying by number which is a constant.

    EDIT: You should also initialize factorial to equal 1 before you use it. Otherwise it will be some gigantic random number.
    Last edited by camel-man; 12-16-2012 at 10:35 AM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Stop right there... "Doesn't work" is never an appropriate problem description.
    Always describe 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"

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    OK so... The 'for' loop "works" -> It does what it is told. The question is, what did you tell it to do?

    Code:
    /* Let's say number = 8 */
    for(i = 1;i <= number; i++)
    {
      factorial = number*i;
    }
    
    /* First loop */
    // factorial = 8*1 = 8
    
    /* Second loop */
    // factorial = 8*2 = 16
    
    /* Third loop */
    // factorial = 8*3 = 24
    
    /* Forth loop */
    // factorial = 8*4 = 32
    
    /* Fifth loop */
    // factorial = 8*5 = 40
    
    /* Sixth loop */
    // factorial = 8*6 = 48
    
    /* Seventh loop */
    // factorial = 8*7 = 56
    
    /* Eighth loop */
    // factorial = 8*8 = 64
     
    printf("\n %d ! = %d", number , factorial); 
    
    /* Prints "number ! = (number * number)" */
    // 8 ! = 64
    What you need to do is work out an algorithm on paper and go though it like I just did.
    Last edited by Click_here; 12-17-2012 at 01:55 AM.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    2
    Code:
    #include<stdio.h>
    int main ()
    
    {
    
    int i, n, f=1;
    
    printf("Enter the Number for Factorial\n");
    
    scanf("%d",&n);
    
    for(;i<=n;i++)
    f*=i;
    printf("\n Factorial is %ld", f);
    
    getch();
    }

    Thanks Click here, I think you are Right..but I got another way to do it. Thanks all for you responses. I will be more elaborate next time IMALC.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    int i, n, f=1;
    
    printf("Enter the Number for Factorial\n");
    
    scanf("%d",&n);
    
    for(;i<=n;i++)
    f*=i;
    Without the initial assignment of a value to i in the for loop, or before it starts, your program can't be seen as correct - it may work on your system, but it's not standard C, and will fail on other systems and compilers.

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    A few pointers
    Code:
    #include <stdio.h>
    int main (void)
    {
    
      int i=1, n;
      long int f=1; 
    
      printf("Enter the Number for Factorial\n");
    
      scanf("%d",&n);
    
      for(;i<=n;i++)
        f*=i;
    
      printf("Factorial is %ld\n", f);
    
      puts("Press Enter to continue...");
      getchar();
    
      return 0;
    }
    Last edited by Click_here; 12-17-2012 at 05:12 PM.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what could be wrong with this loop!?
    By Masterx in forum C++ Programming
    Replies: 15
    Last Post: 01-31-2009, 09:20 PM
  2. Something wrong with 'for' loop.
    By winsonlee in forum C Programming
    Replies: 5
    Last Post: 05-05-2004, 11:11 AM
  3. What is wrong with my while loop?
    By aspand in forum C Programming
    Replies: 3
    Last Post: 06-19-2002, 12:07 PM
  4. What is wrong with this while loop?
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 08:11 PM
  5. what's wrong with this? neverending loop
    By jagerhans in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2001, 02:39 PM

Tags for this Thread