Like Tree1Likes
  • 1 Post By Bayint Naung

facutly method isn't working

This is a discussion on facutly method isn't working within the C Programming forums, part of the General Programming Boards category; Hi guys I'm working on the issue to understand pointers and addresses in c programming. Unfortunately I don't see the ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    9

    facutly method isn't working

    Hi guys

    I'm working on the issue to understand pointers and addresses in c programming.
    Unfortunately I don't see the problem here. For a c programmer my question should be easy to response

    Code:
    #include<stdio.h>
    void factorial(int*);
    int main()
    {
    	// the program calculates faculty
    	int n; 
    	int *pn;
    	printf("Enter a positive integer which you want to facultate\n");
    	scanf("&d",&n);
    	pn = &n; 
    	factorial(pn); 
    	printf("result: %d", n);
    	return 0;
    }
    
    void factorial(int *px)
    {	
    	int k,tmp;
    	tmp = *px;
    	
    	if( *px == 0)
    		tmp = 1;
    		
    	for (k=1;k<=*px;k++)
    		tmp = tmp * k;
    		
    	*px = tmp;
    }
    I can build/compile the file. But the result is: 3265920 for every positve integer I want to facultate ..

    thx for helping me!

    Matts

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You might want to initialise to 1 instead of *px

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    9
    It doesn't work either. The result is still: 3265920

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    factorial of number n is simply

    Code:
    factorial = 1;
    for(i=1;i <= n;i++) {
      factorial = factorial * i;
    }
    You are doing it wrong.


    Code:
    	scanf("&d",&n);
    Duh~ check scanf() doc. it should be %d. -.-
    You should use a debugger and step through the code observing the variable state as the code execute...
    Last edited by Bayint Naung; 05-20-2011 at 04:21 AM.
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arguments return method is not working for float
    By girish1026 in forum C Programming
    Replies: 3
    Last Post: 09-23-2010, 01:26 AM
  2. difference between this->method() and method()
    By nacho4d in forum C++ Programming
    Replies: 7
    Last Post: 11-21-2009, 03:11 PM
  3. int to string - found method not working
    By epb in forum C++ Programming
    Replies: 12
    Last Post: 10-26-2009, 08:34 AM
  4. Encrypt method (from decrypt method)
    By mmmmmm in forum C# Programming
    Replies: 3
    Last Post: 09-19-2009, 10:35 AM
  5. Replies: 9
    Last Post: 03-30-2009, 04:09 AM

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