Factorial Program Not Printing Answer!?

This is a discussion on Factorial Program Not Printing Answer!? within the C Programming forums, part of the General Programming Boards category; I am trying to create a factorial program, to release on my site under the CPL. I have tried switching ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    37

    Unhappy Factorial Program Not Printing Answer!?

    I am trying to create a factorial program, to release on my site under the CPL. I have tried switching the code around in various different ways, but I cannot get the .exe to print the answer (variable), although the code breaks!! Could you give me some advice? The code is below. Thanks in advance!!!!

    Code:
    main()
    { 
    int i;
    int n;
    int z;
    scanf("%d",&i);
    z = i - 1;
    for
    	(n = i * z; z>0);
    {
    if (z = 1);
    break;
    putchar ('\n');
    putchar ('\n');
    printf("Your Answer is %d",n);
    putchar ('\n');
    putchar ('\n');
    system ("pause");
    }
    }
    P.S.- I ran out of time to fix the syntax error(s) in this set up, but the other simular and error free programs did the same thing: get to the printf and skip the variable

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You breaking out of the loop before printing anything.

    Either remove the break and see all the process or move your printout out of the loop altogether.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    37
    okay, thanks for the response! But how will the loop stop before 0!?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    for
    	(n = i * z; z>0);
    {
    if (z = 1);
    break;
    }
    
    
    putchar ('\n');
    putchar ('\n');
    printf("Your Answer is %d",n);
    putchar ('\n');
    putchar ('\n');
    system ("pause");
     return 0; }
    You need to fix that for loop too....

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    ran out of time to fix the syntax error(s) in this set up
    Rude.

    PLEASE, clean up .

    This looks bad:
    Code:
    for	(n = i * z; z>0);
    As does this:
    Code:
    if (z = 1);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Factorial program problem
    By fredz0003 in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2010, 10:29 PM
  2. factorial program
    By emblem4ever in forum C Programming
    Replies: 4
    Last Post: 03-29-2010, 04:44 AM
  3. Factorial Computation Program
    By ts9818a in forum C++ Programming
    Replies: 3
    Last Post: 05-11-2008, 07:01 PM
  4. Factorial program
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 09-21-2007, 03:01 AM
  5. unpredictable answer output of factorial
    By Mathsniper in forum C Programming
    Replies: 5
    Last Post: 12-14-2006, 09:55 AM

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