Objects Recr
Objects Recr
Methods in Java can return objects, allowing for flexible and reusable code
public class Test {
// Returns an array such that first element
// of array is a+b, and second element is a-b
static int[] getSumAndSub(int a, int b)
{
int[] ans = new int[2];
ans[0] = a + b;
ans[1] = a - b;
// Driver method
public static void main(String[] args)
{
int[] ans = getSumAndSub(100, 50);
System.out.println("Sum = " + ans[0]);
System.out.println("Sub = " + ans[1]);
}
}
--------------------
Recursion.
It is the process of defining something in terms of itself.
using recursion
o/p
10 + sum(9)
10 + ( 9 + sum(8) )
10 + ( 9 + ( 8 + sum(7) ) )
...
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0
-------------------------------