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

Java Recursive Methods Examples

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)
17 views13 pages

Java Recursive Methods Examples

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

2.

Code:
package [Link];

import [Link].*;
import [Link].*;

public class Main {

public static void meth(int i,int n)


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

/* [Link]("Enter");
int n = [Link]();*/
meth(2,1);
}
}

Output:
2 5 8 11 14 17 20 23 26

[Link]:
package [Link];

import [Link].*;
import [Link].*;

public class Main {

public static void meth(int i)


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

/* [Link]("Enter");
int n = [Link]();*/
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

[Link]:
package [Link];

import [Link].*;
import [Link].*;

public class Main {

public static void meth(int i)


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

/* [Link]("Enter");
int n = [Link]();*/
meth(1);
}
}

Output:
1 2 4 8 16 32 64

[Link]:
package [Link];

import [Link].*;
import [Link].*;

public class Main {

public static void meth(int i,int n)


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

/* [Link]("Enter");
int n = [Link]();*/
meth(1,1);
}
}
Output:
1 2 4 8 16 32 64 128 256 512

[Link]:
package [Link];

import [Link].*;
import [Link].*;

public class Main {

public static void meth(int i,int n)


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

/* [Link]("Enter");
int n = [Link]();*/
meth(5,1);
}
}

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

[Link]:
package [Link];

import [Link].*;
import [Link].*;

public class Main {

public static void meth(int i,int n)


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

/* [Link]("Enter");
int n = [Link]();*/
meth(1,1);
}
}

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

[Link]:
package [Link];

import [Link].*;
import [Link].*;

public class Main {

public static void meth(int i,int n)


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

/* [Link]("Enter");
int n = [Link]();*/
meth(1,1);
}
}

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

[Link]:
package [Link];

import [Link].*;
import [Link].*;

public class Main {

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


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

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

[Link]:
package [Link];

import [Link].*;
import [Link].*;

public class Main {

public static void meth(int x,int n)


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

/* [Link]("Enter");
int n = [Link]();*/
meth(1,1);
}
}

Output:
1 11 111 1111 11111

[Link]:
package [Link];

import [Link].*;
import [Link].*;

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([Link]);

[Link]("Enter");
int n = [Link]();

if(meth(n,n/2))
[Link]("Prime");
else
[Link]("Not prime");

}
}

Output:Enter
7
Prime

[Link]:
package [Link];

import [Link].*;
import [Link].*;

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([Link]);

[Link]("Enter");
int n = [Link]();
int i=0;
[Link](meth(n));

}
}

Output:
Enter
27
9

[Link]:
package [Link];
import [Link].*;
import [Link].*;

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([Link]);

[Link]("Enter");
int n = [Link]();
int i=0;
[Link](meth(n));

}
}

Output:
Enter
27
14

[Link]:
package [Link];

import [Link].*;
import [Link].*;

public class Main {

public static void meth(int n)


{
if(n>0)
{

[Link](n%10);
meth(n/10);
}

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

[Link]("Enter");
int n = [Link]();
meth(n);

}
}

Output:
Enter
245
542

[Link]:
package [Link];

import [Link].*;
import [Link].*;

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([Link]);

[Link]("Enter");
int n = [Link]();
int c=count(n);
int m =meth(n,c);
if(m==n)
[Link]("Armstrong");
else
[Link]("Not Armstrong");

}
}

Output:
Enter
153
Armstrong

[Link]:
package [Link];

import [Link].*;
import [Link].*;

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([Link]);

[Link]("Enter");
int n = [Link]();
if (meth(n)==1)
[Link]("Emirp");
else
[Link]("Not Emirp");

}
}

Output:
Enter
31
Emirp

[Link]:
package [Link];

import [Link].*;
import [Link].*;

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([Link]);

[Link]("Enter");
int n = [Link]();
if (meth(n)==n)
[Link]("palindrome");
else
[Link]("Not Palindrome");

}
}

Output
Enter
121
palindrome

[Link]:
package [Link];

import [Link].*;
import [Link].*;

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([Link]);

[Link]("Enter");
int n = [Link]();
[Link](meth(n));

}
}

Output:
Enter
111
3

[Link]:
package [Link];

import [Link].*;
import [Link].*;

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([Link]);

[Link]("Enter");
int n = [Link]();
[Link](meth(n));

}
}

Output:
Enter
45862
5

[Link]:
package [Link];

import [Link].*;
import [Link].*;

public class Main {


static int c=0;
public static void meth(int n,int i)
{
if (i<=n)
{
if(n%i==0)
[Link](i+" ");
meth(n,i+1);
}

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

[Link]("Enter");
int n = [Link]();
meth(n,1);

}
}

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

[Link]:
package [Link];

import [Link].*;
import [Link].*;

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([Link]);

/*[Link]("Enter");
int n = [Link]();*/
[Link]( meth(60,36));

}
}

Output:
12

[Link]:
package [Link];

import [Link].*;
import [Link].*;

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([Link]);

[Link]("Enter");
int n = [Link]();
int i= [Link]();
[Link]( meth(n,i));

}
}

Output:
Enter
5 3
125

You might also like