0% found this document useful (0 votes)
37 views4 pages

Java Recursion Programs for Beginners

The document contains a series of Java programs that demonstrate the use of recursion to solve various problems, including calculating factorials, checking for prime numbers, printing digits and their sum, reversing a number, checking for Armstrong numbers, generating Fibonacci series, and checking for palindromes. Each program includes a solution with code and sample output. The examples illustrate the application of recursion in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views4 pages

Java Recursion Programs for Beginners

The document contains a series of Java programs that demonstrate the use of recursion to solve various problems, including calculating factorials, checking for prime numbers, printing digits and their sum, reversing a number, checking for Armstrong numbers, generating Fibonacci series, and checking for palindromes. Each program includes a solution with code and sample output. The examples illustrate the application of recursion in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

RECURSION ASSIGNMENT

[Link] to calculate factorial of a number.

SOL: package Recursion;


import [Link].*;
public class ANS1 {
static int fact(int n){
if(n<=1) return 1;
return n*fact(n-1);
}
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link]();
[Link](fact(n));
}
}
OUTPUT: 6

720

2. WAP to check whether a number is prime or not.

SOL: package Recursion;


import [Link].*;
public class ANS2 {
static boolean isPrime(int n,int i){
if(n<=2) return n==2;
if(n%i==0) return false;
if(i*i>n) return true;
return isPrime(n,i+1);
}
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link]();
[Link](isPrime(n,2)?"Prime":"Not Prime");
}
}
OUTPUT: 31

Prime
3. WAP to print all digits of a number and their sum.

SOL: package Recursion;


import [Link].*;
public class ANS3 {
static int sumDigits(int n){
if(n==0) return 0;
[Link](n%10);
return n%10+sumDigits(n/10);
}
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link]();
int s=sumDigits(n);
[Link]("Sum="+s);
}
}
OUTPUT: 12345

15

4. WAP to print reverse of a number.

SOL: package Recursion;


import [Link].*;
public class ANS4 {
static int rev=0;
static void reverse(int n){
if(n==0) return;
rev=rev*10+n%10;
reverse(n/10);
}
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link]();
reverse(n);
[Link](rev);
}
}
OUTPUT: 1256947

7496521

5. WAP to check whether the number is Armstrong or not.

SOL: package Recursion;


import [Link].*;
public class ANS5 {
static int pow(int n,int p){
if(p==0) return 1;
return n*pow(n,p-1);
}
static int countDigits(int n){
if(n==0) return 0;
return 1+countDigits(n/10);
}
static int armSum(int n,int p){
if(n==0) return 0;
return pow(n%10,p)+armSum(n/10,p);
}
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link]();
int p=countDigits(n);
[Link](armSum(n,p)==n?"Armstrong":"Not Armstrong");
}
}
OUTPUT: 153

Armstrong

6. WAP to print the Fibonacci series in a given range.

SOL: package Recursion;


import [Link].*;
public class ANS6 {
static int fib(int n){
if(n<=1) return n;
return fib(n-1)+fib(n-2);
}
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int a=[Link](),b=[Link]();
for(int i=0;i<=b;i++){
int f=fib(i);
if(f>=a && f<=b) [Link](f+" ");
}
}
}
OUTPUT: 10 100

13 21 34 55 89

7. WAP to check whether the number entered is palindrome or not.

SOL: package Recursion;


import [Link].*;
public class ANS7 {
static int rev=0;
static void reverse(int n){
if(n==0) return;
rev=rev*10+n%10;
reverse(n/10);
}
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link]();
reverse(n);
[Link](n==rev?"Palindrome":"Not Palindrome");
}
}
OUTPUT: 20202

Palindrome

You might also like