Like Tree8Likes
  • 3 Post By iMalc
  • 4 Post By Click_here
  • 1 Post By Click_here

What's wrong with this For Loop in C

This is a discussion on What's wrong with this For Loop in C within the C Programming forums, part of the General Programming Boards category; 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++) ...

  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
    612
    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 09:35 AM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    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,431
    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 12: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,431
    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 04:12 PM.
    Shoaib Khan likes this.
    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, 08: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, 07:11 PM
  5. what's wrong with this? neverending loop
    By jagerhans in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2001, 01:39 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21