Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
36 views13 pages

Recursion

The document contains 18 code snippets showing different examples of recursive functions in Java. Each code snippet demonstrates a recursive method to solve a problem and prints the output. The problems include printing patterns, checking if a number is prime, reversing digits, checking if a number is palindrome, checking if a number is Armstrong, and checking if a number is an emirp number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views13 pages

Recursion

The document contains 18 code snippets showing different examples of recursive functions in Java. Each code snippet demonstrates a recursive method to solve a problem and prints the output. The problems include printing patterns, checking if a number is prime, reversing digits, checking if a number is palindrome, checking if a number is Armstrong, and checking if a number is an emirp number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 13

2.

Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {

public static void meth(int i,int n)


{
if(n<10)
{
System.out.print(i+ " ");
meth(i+3,n+1);
}
}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

/* System.out.println("Enter");
int n = sc.nextInt();*/
meth(2,1);
}
}

Output:
2 5 8 11 14 17 20 23 26

1.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {

public static void meth(int i)


{
if(i<100)
{
System.out.print(i+ " ");
meth(i+3);
}
}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

/* System.out.println("Enter");
int n = sc.nextInt();*/
meth(2);
}
}

Output:
2 5 8 11 14 17 20 23 26 29 32 35 38 41 44 47 50 53 56 59 62 65 68 71 74 77 80 83 86
89 92 95 98

3.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {

public static void meth(int i)


{
if(i<100)
{
System.out.print(i+ " ");
meth(i*2);
}
}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

/* System.out.println("Enter");
int n = sc.nextInt();*/
meth(1);
}
}

Output:
1 2 4 8 16 32 64

4.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {

public static void meth(int i,int n)


{
if(n<=10)
{
System.out.print(i+ " ");
meth(i*2,n+1);
}
}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

/* System.out.println("Enter");
int n = sc.nextInt();*/
meth(1,1);
}
}
Output:
1 2 4 8 16 32 64 128 256 512

5.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {

public static void meth(int i,int n)


{
if(n<=10)
{
System.out.print(i+ " ");
meth(i+5,n+1);
}
}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

/* System.out.println("Enter");
int n = sc.nextInt();*/
meth(5,1);
}
}

Output:
5 10 15 20 25 30 35 40 45 50

6.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {

public static void meth(int i,int n)


{
if(n<=10)
{
if(n%2==1)
System.out.print(i+ " ");
else
System.out.print(-i+" ");
meth(i+1,n+1);
}
}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

/* System.out.println("Enter");
int n = sc.nextInt();*/
meth(1,1);
}
}

Output:
1 -2 3 -4 5 -6 7 -8 9 -10

7.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {

public static void meth(int i,int n)


{
if(n<=10)
{
if(n%2==1)
System.out.print(-i+ " ");
else
System.out.print(i+" ");
meth(i+1,n+1);
}
}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

/* System.out.println("Enter");
int n = sc.nextInt();*/
meth(1,1);
}
}

Output:-1 2 -3 4 -5 6 -7 8 -9 10

8.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {

public static void meth(int x,int y,int n)


{
if(n<=10)
{
System.out.print(x+" ");
meth(y,x+y,n+1);
}
}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);
/* System.out.println("Enter");
int n = sc.nextInt();*/
meth(0,1,1);
}
}

Output:
0 1 1 2 3 5 8 13 21 34

9.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {

public static void meth(int x,int n)


{
if(n<=5)
{
System.out.print(x+" ");
meth((x*10)+1,n+1);
}
}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

/* System.out.println("Enter");
int n = sc.nextInt();*/
meth(1,1);
}
}

Output:
1 11 111 1111 11111

10.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {

public static boolean meth(int n,int i)


{
if(i>=2)
{
if(n%i==0)
return false;
meth(n,i-1);
}
return true;
}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

System.out.println("Enter");
int n = sc.nextInt();

if(meth(n,n/2))
System.out.println("Prime");
else
System.out.println("Not prime");

}
}

Output:Enter
7
Prime

11.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {

public static int meth(int n)


{
if(n==0)
return 0;
else
{

return n%10+ meth(n/10);


}

}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

System.out.println("Enter");
int n = sc.nextInt();
int i=0;
System.out.println(meth(n));

}
}

Output:
Enter
27
9

12.Code:
package com.company;
import java.util.*;
import java.lang.*;

public class Main {

public static int meth(int n)


{
if(n==0)
return 1;
else
{

return n%10* meth(n/10);


}

}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

System.out.println("Enter");
int n = sc.nextInt();
int i=0;
System.out.println(meth(n));

}
}

Output:
Enter
27
14

15.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {

public static void meth(int n)


{
if(n>0)
{

System.out.print(n%10);
meth(n/10);
}

}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

System.out.println("Enter");
int n = sc.nextInt();
meth(n);

}
}

Output:
Enter
245
542

16.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {


public static int count(int n)
{
if(n==0)
return 0;
else
{

return 1+count(n/10);
}

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

}
public static int meth(int n,int c)
{
if(n==0)
return 0;

return pow(n%10,c)+meth(n/10,c);

}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

System.out.println("Enter");
int n = sc.nextInt();
int c=count(n);
int m =meth(n,c);
if(m==n)
System.out.println("Armstrong");
else
System.out.println("Not Armstrong");

}
}

Output:
Enter
153
Armstrong

17.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {


static int c=0;
public static int reverse(int n) {
if (n > 0) {

c=c*10+(n%10);
reverse(n/10);
}
return c;
}
public static boolean prime(int n,int i)
{
if(i>=2)
{
if(n%i==0)
return false;
prime(n,i-1);
}
return true;

}
public static int meth(int n)
{
if(prime(n,n/2))
{
int x=reverse(n);
if(prime(x,x/2))
{
return 1;
}
}

return 0;
}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

System.out.println("Enter");
int n = sc.nextInt();
if (meth(n)==1)
System.out.println("Emirp");
else
System.out.println("Not Emirp");

}
}

Output:
Enter
31
Emirp

18.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {


static int c=0;
public static int meth(int n)
{
if (n>0)
{
c=c*10+(n%10);
meth(n/10);
}
return c;
}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

System.out.println("Enter");
int n = sc.nextInt();
if (meth(n)==n)
System.out.println("palindrome");
else
System.out.println("Not Palindrome");

}
}

Output
Enter
121
palindrome

19.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {


static int c=0;
public static int meth(int n)
{
if (n==0)
{
return 0;
}
return (n%10)+meth(n/10);
}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

System.out.println("Enter");
int n = sc.nextInt();
System.out.println(meth(n));

}
}

Output:
Enter
111
3

20.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {


static int c=0;
public static int meth(int n)
{
if (n>0)
{
c++;
meth(n/10);
}
return c;
}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

System.out.println("Enter");
int n = sc.nextInt();
System.out.println(meth(n));

}
}

Output:
Enter
45862
5

21.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {


static int c=0;
public static void meth(int n,int i)
{
if (i<=n)
{
if(n%i==0)
System.out.println(i+" ");
meth(n,i+1);
}

}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

System.out.println("Enter");
int n = sc.nextInt();
meth(n,1);

}
}

Output:
Enter
24
1
2
3
4
6
8
12
24

22.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {


static int c=0;
public static int meth(int n,int i)
{

if(i==0)
return n;
return meth(i,n%i);

}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

/*System.out.println("Enter");
int n = sc.nextInt();*/
System.out.println( meth(60,36));

}
}

Output:
12

23.Code:
package com.company;

import java.util.*;
import java.lang.*;

public class Main {


static int c=0;
public static int meth(int n,int i)
{

if (i==0)
return 1;
return n*meth(n,i-1);

}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner(System.in);

System.out.println("Enter");
int n = sc.nextInt();
int i= sc.nextInt();
System.out.println( meth(n,i));

}
}

Output:
Enter
5 3
125

You might also like