Results 1 to 5 of 5
  1. #1
    jk91 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Factorial beginner

    Hi all,

    I lately started learning programming, mostly Java and a little bit of PHP too.

    I've been tryin to figure out some school assignments. In one of these assignments I have to use factorial.

    I googled it and here's what I came up with (we've got to use methods):


    Java Code:
    package Factorial2;
    import java.util.Scanner; // not used
    import java.io.PrintStream;
    
    class Factorial2 {
    	
    	int factorialofnumber;
    	
    	PrintStream out;
    	
    	Factorial2() {
    		
    		out = new PrintStream(System.out);
    		
    	}
    		
    	void Factorial(int x) {
    		
    		for (int p = 1; p <= x; p++) {
    			
    		factorialofnumber *= p;
    		
    		out.printf("Factorial:%.1f", factorialofnumber);
    		
    		}
    	}
    	
    	void Start(){
    		
    		Factorial(5);
    		
    	}
    	
    	public static void main(String[] args) {
    		
    		new Factorial2().Start();
    
    	}
    
    }
    Now, this doesn't work, but I don't see why. Can anyone of you guys help me on this one?

    Thanks in advance.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Factorial beginner

    If you don't initialize a member variable (such as factorialofnumber) it is implicitly initialized to zero for you. You don't want that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jk91 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: Factorial beginner

    Thanks. Now my method looks like this:

    Java Code:
    void Factorial(int x) {
    		
    		int factorialofnumber = 1;
    		
    		for (int p = 1; p <= x; p++) {
    			
    		factorialofnumber *= p;
    		
    		out.printf("Factorial:%.1f", factorialofnumber);
    		
    		}
    There's no need to make factorialofnumber global. It still doesn't work and I'm getting the same errors.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Factorial beginner

    What errors? We don't read minds. Copy and paste the full error messages you get.

  5. #5
    jk91 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: Factorial beginner

    Hey,

    I resolved the issue.

    I used "%f" instead of "%d", while trying to output an integer.

    Here's the code for if anyone has this problem in the future:

    Java Code:
    void Factorial(int x) {
    		
    		int factorialofnumber = 1;
    		
    		for (int p = 1; p <= x; p++) {
    			
    		factorialofnumber *= p;
    		
    		}
    		
    		out.printf("Factorial is %d",factorialofnumber);

Similar Threads

  1. Factorial in java
    By spidey32 in forum New To Java
    Replies: 18
    Last Post: 04-09-2011, 01:04 PM
  2. factorial sum in java
    By java157 in forum New To Java
    Replies: 9
    Last Post: 03-17-2011, 10:07 AM
  3. Factorial console program
    By er1c550n20 in forum New To Java
    Replies: 6
    Last Post: 04-13-2010, 02:07 PM
  4. Factorial
    By Anindo in forum New To Java
    Replies: 4
    Last Post: 07-28-2009, 09:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •