# Practice questions on Have your own methods

## Level 1

1.

### Write a program to print the sum of two numbers entered by user by defining your own method.

```import java.util.*;

class Ans{

public static void Sum(){
Scanner s = new Scanner(System.in);
int x,y;
System.out.println("Enter a number");
x = s.nextInt();
System.out.println("Enter the another number");
y = s.nextInt();
System.out.println("Sum is "+(x+y));
}

public static void main(String[] args) {
Sum();
}
}
```

2.

3.

### Write a program to print the circumference and area of a circle of radius entered by user by defining your own method.

```import java.util.*;

class Ans{

public static void Circle(){
Scanner s = new Scanner(System.in);
}

public static void main(String[] args) {
Circle();
}
}
```

4.

5.

6.

7.

### Define a method to find out if number is prime or not.

```import java.util.*;

class Ans{

public static boolean isPrime(int x){
boolean prime = true;
if(x>1){
for(int i = 2;i<x;i++){
if(x%i==0){
prime = false;
break;
}
}
}
else{
prime = false;
}
return prime;
}

public static void main(String[] args) {
System.out.println(isPrime(5));
System.out.println(isPrime(10));
}
}
```

8.

9.

### Write a program to print the factorial of a number by defining a method named 'Factorial'.Factorial of any number n is represented by n! and is equal to 1*2*3*....*(n-1)*n. E.g.-4! = 1*2*3*4 = 243! = 3*2*1 = 62! = 2*1 = 2Also,1! = 10! = 0

```import java.util.*;

class Ans{

public static int fact(int x){
if(x == 0 || x == 1){
return 1;
}
else{
return fact(x-1)*x;
}
}

public static void main(String[] args) {
System.out.println(fact(5));
System.out.println(fact(10));
System.out.println(fact(1));
System.out.println(fact(0));
}
}
```

## Level 2

1.

### Print the multiplication table of 15 using recursion.

```import java.util.*;

class Ans{

public static void table(int x, int y){
if(y != 1){
table(x,y-1);
}
System.out.println(x*y);
}

public static void main(String[] args) {
table(15, 10);
}
}
```

2.

3.

### Using recursion, define a method to know nth term of a Fibonacci series. Nth term of Fibonacci series is F(n) = F(n-1)+(n-2) F(0) = 0 F(1) = 1 F(2) = F(1)+(0) = 1+0 = 1 F(3) = F(2)+(1) = 1+1 = 2 F(4) = F(3)+(2) = 2+1 = 3

```import java.util.*;

class Ans{

public static int fib(int x){
if(x == 0){
return 0;
}
else if(x==1){
return 1;
}
else{
return fib(x-1)+fib(x-2);
}
}

public static void main(String[] args) {
System.out.println(fib(0));
System.out.println(fib(1));
System.out.println(fib(2));
System.out.println(fib(3));
System.out.println(fib(4));
}
}
```

4.

5.

### Define a method to calculate power of a number raised to other i.e. ab using recursion where the numbers 'a' and 'b' are to be entered by the user

```import java.util.*;

class Ans{

public static int pow(int a, int b){
if(b==1){
return a;
}
else{
return(a*pow(a,b-1));
}
}

public static void main(String[] args) {
int a,b;
Scanner s = new Scanner(System.in);
System.out.println("Enter the value of a and b for a^b");
a = s.nextInt();
b = s.nextInt();
System.out.println(pow(a,b));
}
}
```

6.

## Level 3  