Thread: Factorial with while loop

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    113

    Factorial with while loop

    I have written a part of codes for a factorial calculation but I cannot find my error. When I run it, I don't get a result. Can you have a look?

    Code:
    #include <stdio.h>
    
    int main()
    {
    int x,n,a;
    x=1;
    a=1;
    printf("Enter a number: ");
    scanf("%d\n",&n);
    while (x<=n)
    {
      a=x*a;
      x=x+1;
      printf("%d",a);
    }
    
    getchar();
    return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should compare and contrast this:
    Code:
    scanf("%d", &b);
    (from your last program) and this:
    Code:
    scanf("%d\n",&n);

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Quote Originally Posted by tabstop View Post
    You should compare and contrast this:
    Code:
    scanf("%d", &b);
    (from your last program) and this:
    Code:
    scanf("%d\n",&n);

    Yes, I didn't notice this. I should focus on differences of writing styles in scanf and printf commands.

    Thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  2. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM