0% found this document useful (0 votes)
5 views9 pages

Java Recursive Functions Examples

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)
5 views9 pages

Java Recursive Functions Examples

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

[Link]: package com.

company;

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

public class Main {


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

if (i<=n/2)
{
if(n%i==0)
[Link]("Not prime");
else
meth(n,i+1);
}
else
[Link]("prime Number");

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

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

}
}

Output:
Enter
7
prime Number

[Link]:
package [Link];

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

public class Main {


static int s=0;
public static void meth(int n,int s)
{
if(n>0)
{
s=s+n%10;
meth(n/10,s);
}
else
[Link]("sum is "+s);
}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner([Link]);

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

}
}

Output:
Enter
153
sum is 9

[Link]:
package [Link];

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

public class Main {


static int s=1;
public static void meth(int n,int s)
{
if(n>0)
{
s=s*n%10;
meth(n/10,s);
}
else
[Link]("product is "+s);

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

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

}
}

Output:
Enter
212
product is 4

[Link]:
package [Link];

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

public class Main {


static int s=1;
public static void meth(int n,int s)
{
if(n>0)
{
s=s*n;
meth(n-1,s);
}
else
[Link]("Factorial is "+s);

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

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

}
}

Output:Enter
5
Factorial is 120

[Link]:
package [Link];

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

public class Main {


static int s=0;
public static void meth(int n,int s)
{
if(n>0)
{
s=s*10+n%10;
meth(n/10,s);
}
else
[Link]("reverse is "+s);
}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner([Link]);

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

}
}

Output:
Enter
245
reverse is 542

[Link]:
package [Link];

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

public class Main {


static int s=0;
public static int count(int n)
{
int c=0;
while (n>0)
{
c++;
n=n/10;
}
return c;
}
public static void meth(int n,int s,int c,int n1)
{
if(n>0)
{
s=s+(int)[Link](n%10,c);
meth(n/10,s,c,n1);
}
else
{
if(s==n1)
[Link]("Armstrong");
else
[Link]("Not armstrong");
}
}
public static void main(String[] args) {
// write your code here
// Characters pattern
Scanner sc = new Scanner([Link]);

[Link]("Enter");
int n = [Link]();
int n1=n;
meth(n,s,count(n),n1);

}
}

Output:
Enter
153
Armstrong

[Link]:
package [Link];

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

public class Main {


static int s=0;

public static void meth(int n,int s,int n1)


{
if (n>0)
{
s=s*10+n%10;
meth(n/10,s,n1);
}else {
if (s == n1)
[Link]("Palindrome");
else [Link]("Not Palindrome");
}

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

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

}
}

Output:Enter
121
Palindrome

[Link]: package [Link];

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

public class Main {


static int s=0;

public static void meth(int n,int s)


{
if (n>0)
{
if((n%10)%2==0) {
s = s + n % 10;
}
meth(n/10,s);

}else {
[Link]("sum of even digits "+s);
}

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

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

}
}

Output:
Enter
121
sum of even digits 2

[Link]:
package [Link];

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

public class Main {


static int s=0;

public static void meth(int n,int s)


{
if (n>0)
{
s++;
meth(n/10,s);
}else {
[Link]("Count "+s);
}

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

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

}
}

Output:
Enter
56412
Count 5

[Link]:
package [Link];

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

public class Main {


static int s=1;

public static void meth(int n,int s)


{
if (s<=n)
{
if(n%s==0)
[Link](s+" ");
meth(n,s+1);

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

[Link]("Enter");
int n = [Link]();
int n1=n;
[Link]("Factors are");
meth(n,s);
}
}

Output:
Enter
6
Factors are
1 2 3 6

[Link]:
package [Link];

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

public class Main {


static int s=1;

public static void meth(int a,int b)


{
if (b>0)
{
meth(b,a%b);

}
else
[Link]("HCF "+a);

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

/*[Link]("Enter");
int n = [Link]();
int n1=n;*/

meth(60,36);

}
}

Output:
HCF 12

[Link]:
package [Link];

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

public class Main {


static int s=1;

public static void meth(int a,int b,int s)


{
if (b>0)
{
s=s*a;
meth(a,b-1,s);
}
else
[Link]("power is "+s);

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

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

meth(n,n1,s);

}
}

Output:
Enter
5 3
power is 125

You might also like