Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Factorial of 1-20

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Factorial of 1-20

    I have to display the factorial's of numbers 1 - 20. I believe my code should work, but i keep getting the error:


    FactorialTest.java:6: cannot find symbol
    symbol : method factorial(int)
    location: class FactorialTest
    System.out.println( factorial( x ) );;
    ^(pointing under the "f" in "factorial")

    Any help on what's causing my error, and how to fix it, would be greatly appreciated. Thank's in advance.


    public class Factorial
    {
    	public static long factorial(int n)
    	{
    		if(n<=1)
    			return 1;
    		else
    			return n*factorial(n-1);
    	}
    }

    public class FactorialTest
    {
    	public static void main(String[] args)
    	{		
    		for( int x=1; x<=20; x++)
    			System.out.println( factorial( x ) );;
    	}
    }


  2. #2
    Junior Member Kakashi's Avatar
    Join Date
    Oct 2009
    Posts
    29
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Factorial of 1-20

    Im just think about this and could be wrong but try putting the int x out of the for loop and use and int i in the for loop instead: like this
    int x;
    for(int i; i<20;i++)
    {
          System.out.println(factor(x));
    }
    That might work

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Factorial of 1-20

    The factorial method cannot be accessed from the FactorialTest class because the factorial method is inside of the Factorial class. There are two ways to fix this:
    1) Create a Factorial object and invoke the factorial(int) method on that (and send it x).
    2) Use the static approach, where you would say new Factorial().factorial(x); instead of just factorial(x);

    It is a scope issue that is occurring because you are accessing methods between differing classes.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. [SOLVED] Is it possible to get factorial of negative number
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: August 4th, 2011, 05:45 PM