Recursion in Java
Results 1 to 2 of 2

Thread: Recursion in Java

  1. #1
    Senior Member
    Join Date
    May 2002
    Posts
    344

    Exclamation Recursion in Java

    hey guys i wrote up a quick program and it is suppose to take in the values from an array called int [] list and then return the product of all of the numbers stored in int [] list. Anyways, i did this using recursion and my compiler died on me so i was wondering if someone could compile this and tell me if it works...thanks

    Code:
    public class Car
    {
        public void main(String args[])
        {
            int list[]=null;
            list[0]=1;
            list[1]=2;
            list[2]=3;
            wrapper_Mystery(list);
        }
        
        public void wrapper_Mystery (int [] list)
        {
            int n=0;
            mystery(list,n);
        }
        public int mystery(int [] list, int n)
        {
            if (n<=list.length)
            {
                return list[n]*mystery(list,n+1);
            }
            else
                return 0;
        }
    }
    my compiler said that no errors were found, but i just want to check that there is no logic errors. i have never really studied or learned recursion so i am new to all of this h3r3tic if you can help me out i would be appriciative


    Edit:

    Also while you are at it could someone try to compile the program bellow. All it does is find the biggest number in an array of integers and returns it...
    Code:
    public class Car
    {
        public void main(String args[])
        {
            int list[]=null;
            list[0]=1;
            list[1]=2;
            list[2]=3;
            wrapper_Mystery(list);
        }
        
        public void wrapper_Mystery (int [] list)
        {
            int n=0;
            mystery(list,n);
        }
        public int mystery(int [] list, int n)
        {
            if (n==list.length-1)
                return list[n];
            else
            {
                if (list[n]>mystery(list,n+1))
                    return list[n];
                else 
                { 
                    return mystery(list, n+1);
                }
            }
        }
    }
    thanks guys
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  2. #2
    Senior Member
    Join Date
    May 2003
    Posts
    226
    I've a book on recursion examples. P.S i didn't learn recursion before. Let me type out the examples

    Code:
    public class Factorial
    {
    	public long factorial(long number)
    	{
    		// base case
    		if (number <= 1)
    		{
    			return 1;
    		}
    		//recursive step
    		else
    		{
    			return number * factorial(number - 1);
    		}
    	}
    
    	public static void main(String args[])
    	{
    		Factorial ft = new Factorial();
    
    		for (long counter = 0; counter <= 10; ++counter)
    		{
    			System.out.println(counter + "! = " +
    			   ft.factorial(counter) + "\n");
    		}
    	}
    }
    Hope it will helps

Posting Permissions

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

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides