factorial problem

This is a discussion on factorial problem within the C Programming forums, part of the General Programming Boards category; write a program that evaluates the factorial of the integer 1 to 5. print result in tabular format. Code: #include ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    7

    factorial problem

    write a program that evaluates the factorial of the integer 1 to 5. print result in tabular format.

    Code:
    #include <stdio.h>
    
    long factorial(long number);
    int main()
    {
    	int i,number;
    
    	for (i=1;i<=5; i++) {
    		printf("%2d!=%1d\n", i, factorial (number));
    	}
    
    	return 0;
    }
    
    long factorial(long number)
    {
    	if (number <=2) {
    		return 1;
    	else {
    		return (number*factorial(number-1)); }
       }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Try
    printf("%2d!=%li\n", i, factorial (i));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User coolshyam's Avatar
    Join Date
    Mar 2005
    Posts
    26

    Thumbs up Solved

    Well the logic given by our friend is right
    use factorial(i)
    i took the pain of compiling it in TurboC
    here is the debugged program
    Code:
    
    #include <stdio.h>
    #include<conio.h>
    long factorial(long number);
    int main()
    {
    	int i,number;
    
    	for (i=1;i<=5; i++) {
    		printf("%2d!=%1d\n", i, factorial (i));
    	}
    
    	getch();
    	return 0;
    }
    
    long factorial(long number)
    {
    	if (number <=2)
    	{
    		return 1;
    	}
    	else
    	{
    		return (number*factorial(number-1));
    	}
    }

    any questions any type in programming

    a ready made answer
    -----------------------------------------------------------------------------------
    FORTUNE FAVOURS THE BOLD!
    -----------------------------------------------------------------------------------

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    7
    thanks for the reply but all the number need to mutiply by 2 to be correct.

    example from your program

    6! = 60 but it suppose ot equal 120

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > if (number <=2)
    Probably because this 2 should be 1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    7
    Thanks everyone for the help, I finally got the program to run

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

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