0% found this document useful (0 votes)
8 views

Recursion Void

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Recursion Void

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

7.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/2)
{
if(n%i==0)
System.out.println("Not prime");
else
meth(n,i+1);
}
else
System.out.println("prime Number");

}
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,2);

}
}

Output:
Enter
7
prime Number

8.Code:
package com.company;

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

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
System.out.println("sum is "+s);
}
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,s);

}
}

Output:
Enter
153
sum is 9

9.Code:
package com.company;

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

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
System.out.println("product is "+s);

}
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,s);

}
}

Output:
Enter
212
product is 4

10.Code:
package com.company;

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

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
System.out.println("Factorial is "+s);

}
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,s);

}
}

Output:Enter
5
Factorial is 120

11.Code:
package com.company;

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

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
System.out.println("reverse is "+s);
}
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,s);

}
}

Output:
Enter
245
reverse is 542

12.Code:
package com.company;

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

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)Math.pow(n%10,c);
meth(n/10,s,c,n1);
}
else
{
if(s==n1)
System.out.println("Armstrong");
else
System.out.println("Not armstrong");
}
}
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 n1=n;
meth(n,s,count(n),n1);

}
}

Output:
Enter
153
Armstrong

14.Code:
package com.company;

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

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)
System.out.println("Palindrome");
else System.out.println("Not Palindrome");
}

}
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 n1=n;
meth(n,s,n1);

}
}

Output:Enter
121
Palindrome

15.Code: package com.company;

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

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 {
System.out.println("sum of even digits "+s);
}

}
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 n1=n;
meth(n,s);

}
}

Output:
Enter
121
sum of even digits 2

16.Code:
package com.company;

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

public class Main {


static int s=0;

public static void meth(int n,int s)


{
if (n>0)
{
s++;
meth(n/10,s);
}else {
System.out.println("Count "+s);
}

}
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 n1=n;
meth(n,s);

}
}

Output:
Enter
56412
Count 5

17.Code:
package com.company;

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

public class Main {


static int s=1;

public static void meth(int n,int s)


{
if (s<=n)
{
if(n%s==0)
System.out.print(s+" ");
meth(n,s+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 n1=n;
System.out.println("Factors are");
meth(n,s);
}
}

Output:
Enter
6
Factors are
1 2 3 6

18.Code:
package com.company;

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

public class Main {


static int s=1;

public static void meth(int a,int b)


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

}
else
System.out.println("HCF "+a);

}
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 n1=n;*/

meth(60,36);

}
}

Output:
HCF 12

19.Code:
package com.company;

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

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
System.out.println("power is "+s);

}
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 n1=sc.nextInt();

meth(n,n1,s);

}
}

Output:
Enter
5 3
power is 125

You might also like