RECURSIVE PROGRAMS
1. Design a class DeciHex
Data Members:
int num : Integer number to convert to hexadecimal.
String hex : String variable to store the hexadecimal equivalent.
char charHexa[] : Character array to store hexadecimal digits (0–15, i.e., 0–9, A–F).
String stringHexa : String variable.
Member Functions:
DeciHex()
Default constructor to initialize the data members with default values.
void getNum()
Accept a positive integer num.
public String convert(int n)
Find the hexadecimal equivalent of the parameter n using recursive technique and return it
as a string.
public void display()
Display the integer num and its hexadecimal equivalent by invoking the convert() function.
import [Link].*;
public class DeciHex
{
String hexa;
String stringHexa;
char charHexa[];
int num;
DeciHex()
{
hexa = "";
stringHexa = "";
num = 0;
charHexa=new char[]
{
'0','1','2','3','4','5','6','7',
'8','9','A','B','C','D','E','F'
};
}
public void getNum()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a positive integer number");
num = [Link]();
}
public String convert(int n)
{
if(n != 0)
{
int t = n % 16;
stringHexa = charHexa[t] + stringHexa;
convert(n / 16); // Calling recursively
}
return stringHexa;
}
public void display()
{
hexa = convert(num);
[Link]("Decimal Number: " + num);
[Link]("Hexadecimal Equivalent: " + hexa);
}
public static void main(String args[])
{
DeciHex obj = new DeciHex();
[Link]();
[Link]();
}
}
2. Class Name: DeciOct
Data Members:
int n : Stores the decimal number.
int oct : Stores the octal equivalent number.
Member Functions:
DeciOct()
Constructor to initialize the data members.
n = 0, oct = 0
void getNum(int nn)
Assign nn to n.
void deci_oct(int x)
Calculates the octal equivalent of x and stores it in oct using recursive technique.
void show()
Displays the decimal number n, calls the function deci_oct(), and displays its octal
equivalent.
import [Link].*;
public class DeciOct
{
int n,oct;
DeciOct()
{
n=0;
oct=0;
}
public void getnum(int nn)
{
n=nn;
}
public void deci_oct(int x)
{
if(x!=0)
{
int t=x%8;
deci_oct(x/8);
oct=oct*10+t;
}
}
public void show()
{
deci_oct(n);
[Link]("the decimal number=" +n);
[Link]("the octal number is" +oct);
}
public static void main(String args[])
{
DeciOct obj=new DeciOct();
[Link](28);
[Link]();
}
}
3. Class Name: Fibo
Data Members:
int start – stores the starting value.
int end – stores the ending value.
Member Functions:
Fibo() – Constructor to initialize the data members with default values.
void read() – To accept the values of start and end.
int fibo(int a) – Returns the ath term of the Fibonacci series using recursion.
void display() – Displays the Fibonacci series from start to end by invoking the fibo()
function.
import [Link].*;
public class Fibo
{
int start, end;
// Constructor
Fibo()
{
start = 0;
end = 0;
}
// Input method
public void read()
{
Scanner in = new Scanner([Link]);
[Link]("Enter start:");
start = [Link]();
[Link]("Enter end:");
end = [Link]();
}
// Recursive Fibonacci function
public int fibo(int a)
{
if(a == 0 || a == 1)
return a;
else
return fibo(a - 1) + fibo(a - 2);
}
// Display Fibonacci numbers between start and end
public void display()
{
for(int i = start; i <= end; i++)
{
int p = fibo(i);
if(p >= start && p <= end)
{
[Link](p + " ");
}
}
}
// Main method
public static void main(String args[])
{
Fibo obj = new Fibo();
[Link]();
[Link]();
}
}
4. Series:
2 4 6 n
x x x x
S= + + +⋯+
1 ! 3 ! 5! (n−1)!
Class Name:
SumSeries
Data Members:
int x – to store the value of x
int n – to store the number of terms in the series
double sum – to store the sum of the series
Member Functions:
SumSeries(int xx, int nn) – Constructor to initialize x = xx and n = nn.
double findfact(int m) – Returns the factorial of m using recursion.
double findpower(int x, int y) – Returns x raised to the power y using recursion.
void calculate() – Calculates the sum of the series by invoking the above two
recursive functions.
void display() – Displays the sum of the series.
import [Link].*;
public class SumSeries
{
int x, n;
double sum;
SumSeries(int xx, int nn)
{
x = xx;
n = nn;
sum = 0.0;
}
double findfact(int m)
{
if(m == 0)
return 1;
else
return (m * findfact(m - 1));
}
double findpower(int x, int y)
{
if(y == 0)
return 1;
else
return (x * findpower(x, y - 1));
}
public void calculate()
{
for(int i = 2; i <= n; i = i + 2)
{
double p = findfact(i - 1);
double q = findpower(x, i);
sum = sum + (q / p);
}
}
public void display()
{
[Link]("The sum of the series = " + sum);
}
public static void main(String args[])
{
SumSeries obj = new SumSeries(2, 10);
[Link]();
[Link]();
}
}
Output for (2,10)
5. Class Name: theString
Data Members:
String str; → Stores a string.
int cap; → Stores the count of capital letters.
int sm; → Stores the count of small letters.
Member Functions:
theString() → Constructor to initialize str, cap, and sm.
void accept() → Accepts the string as input.
void Recursive(int len) → Recursively counts the number of capital and small letters.
void display() → Displays the count of capital and small letters.
import [Link].*;
class theString
{
String str;
int cap, sm;
theString()
{
str = "";
cap = 0;
sm = 0;
}
public void Accept()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a String");
str = [Link]();
}
public void Recursive(int len)
{
if(len >= 0)
{
char ch = [Link](len);
if(ch >= 'A' && ch <= 'Z')
cap++;
if(ch >= 'a' && ch <= 'z')
sm++;
Recursive(len - 1);
}
}
public void display()
{
int q = [Link]();
Recursive(q - 1);
[Link]("The number of capital letters is " + cap);
[Link]("The number of small letters is " + sm);
}
public static void main(String args[])
{
theString obj = new theString();
[Link]();
[Link]();
}
}
6. Class Name: Convert
Data Members (DM)
str : to store a string
newStr : to store the new string
len : int to store the length of the string
Member Functions (MF)
Convert(String ns) : to initialize string str = ns
char changeCase(char ch) : to convert argument ch into its opposite case and return it.
void convertCase(int n) : using recursive technique, convert each letter of string str into the
opposite case except other letters by invoking the function char changeCase(char) and store
the new string in newStr.
Example:
Input => WeLCome@2025
Output => wElcOME@2025
void display() : displays the original string and the new string with suitable messages.
import [Link].*;
class Convert {
private String str;
private String newStr;
private int len;
// Constructor to initialize the original string
public Convert(String ns) {
str = ns;
newStr = "";
len = [Link]();
}
// MF: To convert argument ch into its opposite case
public char changeCase(char ch) {
if ([Link](ch)) {
return [Link](ch);
} else if ([Link](ch)) {
return [Link](ch);
}
return ch; // Return as is if not a letter
}
// MF: Recursive technique to convert case and store in newStr
public void convertCase(int n) {
// Base case: end of string reached
if (n >= len) {
return;
}
// Recursive logic: process current char and move to next
char current = [Link](n);
newStr += changeCase(current);
convertCase(n + 1);
}
// MF: Displays original and modified strings
public void display() {
[Link]("Original String: " + str);
[Link]("New String: " + newStr);
}
public static void main(String[] args) {
Convert obj = new Convert("WelCoMe @ 2025");
[Link](0);
[Link]();
}
}
7. Class Name: Admission
Data Members (DM)
adno[] : int array to store 10 admission numbers.
Member Functions (MF)
Admission() : Constructor to initialize the array elements to their default value.
void fillArray() : Accept the array elements in ascending order.
public int binSearch(int l, int u, int v) : Search for a particular admission number v
using the binary search recursive technique and return:
o the index if the admission number is found.
o -1 if it is not found.
import [Link].*;
public class Admission
{
int adno[];
// Constructor
Admission()
{
adno = new int[10];
for(int i = 0; i < 10; i++)
adno[i] = 0;
}
// Accept array elements in ascending order
public void fillArray()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter 10 admission numbers in ascending order:");
for(int i = 0; i < 10; i++)
adno[i] = [Link]();
}
// Recursive Binary Search
public int binSearch(int l, int u, int v)
{
if(l > u)
return -1;
int mid = (l + u) / 2;
if(v == adno[mid])
return mid;
else if(v > adno[mid])
return binSearch(mid + 1, u, v);
else
return binSearch(l, mid - 1, v);
}
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
Admission obj = new Admission();
[Link]();
[Link]("Enter the admission number to search: ");
int v = [Link]();
int result = [Link](0, 9, v);
if(result == -1)
[Link]("Admission number not found.");
else
[Link]("Admission number found at index " + result + ".");
}
}
8. A class Gcd has been defined to find the GCD b/w 2 no.'s
class name: Gcd
DM:-
num1: int to store 1st num
num2: int to store 2nd num
MF:-
Gcd(): to initialize default value
void accept(): to accept 2 no.'s
int gcd(int x, int y): return the gcd of the 2 no.'s x & y using recursive technique.
void display(): display gcd of the 2 no.'s with an appropriate message
import [Link];
class Gcd {
// Data Members (DM)
int num1;
int num2;
// Member Functions (MF)
// Constructor to initialize default values
Gcd() {
num1 = 0;
num2 = 0;
}
// To accept 2 numbers from the user
void accept() {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
num1 = [Link]();
[Link]("Enter second number: ");
num2 = [Link]();
}
// To return the gcd of x and y using recursive technique
int gcd(int x, int y) {
if (y == 0) {
return x;
} else {
return gcd(y, x % y);
}
}
// Display gcd of the 2 numbers with an appropriate message
void display() {
int result = gcd(num1, num2);
[Link]("The Greatest Common Divisor of " + num1 + " and " + num2 + " is: "
+ result);
}
public static void main(String[] args) {
Gcd obj = new Gcd();
[Link]();
[Link]();
}
}
9. Selection sort
import [Link].*;
public class SelectionSort
{
int arr[];
// Constructor
SelectionSort()
{
arr = new int[10];
for(int i = 0; i < 10; i++)
arr[i] = 0;
}
// Accept array elements
public void fillArray()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter 10 array elements:");
for(int i = 0; i < 10; i++)
arr[i] = [Link]();
}
// Recursive Selection Sort
public void selectionSort(int start)
{
if(start < [Link] - 1)
{
int min = start;
for(int i = start + 1; i < [Link]; i++)
{
if(arr[i] < arr[min])
min = i;
}
int temp = arr[start];
arr[start] = arr[min];
arr[min] = temp;
selectionSort(start + 1);
}
}
// Display the sorted array
public void display()
{
[Link]("Sorted Array:");
for(int i = 0; i < 10; i++)
[Link](arr[i] + " ");
}
public static void main(String args[])
{
SelectionSort obj = new SelectionSort();
[Link]();
[Link](0);
[Link]();
}
}
Question 10
/**A number is said to be emirp if the number is prime backwards and forwards.
* Example: 13 and 31 are both prime numbers. Thus, 13 is an emirp number.
* Design a class Emirp to check if a given number is Emirp number or not.
* Some of the members of the class are given below:
* Class name: Emirp
* Data members:
* n: stores the number
* rev: stores the reverse of the number
* f: stores the divisor
* Member functions:
* Emirp(int nn): to assign n = nn, rev = 0 and f = 2
* int isprime(int x): check if the number is prime using the recursive technique and return 1
if prime otherwise return 0
* void isEmirp(): reverse the given number and check if both the original number and the
reverse number are prime, by invoking the function isprime(int) and display the result with
an appropriate message
* Specify the class Emirp giving details of the constructor(int), int isprime (int) and void
isEmirp().
* Define the main function to create an object and call the methods to check for Emirp
number.
*/
import [Link].*;
public class Emirp
{
int n,rev,f;
Emirp(int nn)
{
n=nn;
rev=0;
f=2;
}
public int isPrime(int x)
{
if(x<2)
return 0;
if(x==f)
return 1;
if(x%f==0)
return 0;
f++;
return isPrime(x);
}
public void isEmirp()
{
for(int i=n;i!=0;i=i/10)
rev=rev*10+i%10;
if(isPrime(n)==1)
{
f=2;
if(isPrime(rev)==1)
[Link](n+" is Emirp");
else
[Link](n+" is not Emirp");
}
}
public static void main(String args[])
{
Emirp obj=new Emirp(13);
[Link]();
}
}
Sample Output:
13 is Emirp
Question 11
/**
* A happy number is a number in which the eventual sum of the square of the digits of the
number is equal to 1.
* Example: 28 = (2)2 + (8)2 = 4 + 64 = 68
* 68 = (6)2 + (8)2 = 36 + 64 = 100
* 100 = (1)2 + (0)2 + (0)2 = 1 + 0 + 0 = 1
* Hence, 28 is a happy number.
* Design a class Happy to check if a given number is a happy number.
* Some of the members of the class are given below:
* Class name: Happy
* Data Members: n: stores the number
* Member functions:
* Happy(): constructor to assign 0 to n.
* void getNum (int nn): to assign the parameter value to the number n = nn.
* int sumSquareDigits (int x): returns the sum of the square of the digits of the number x,
using the recursive technique
* void isHappy (): checks if the given number is a happy number by calling the function
sumSquareDigits(int) and displays an appropriate message.
* Specify the class Happy giving details of the constructor (), void getNum (int), int
sumSquareDigits (int) and void isHappy ().
* Also define a main(s) function to create an object and call the methods to check for happy
number.
*/
import [Link].*;
public class Happy
{
int n;
Happy()
{
n=0;
}
public void getNum(int nn)
{
n=nn;
}
public int sumSquareDigits(int x)
{
if(x<10)
return x;
else
return(((x%10)*(x%10))+sumSquareDigits(x/10)*(x/10));
}
public void isHappy()
{
int res=n;
do
{
res=sumSquareDigits(res);
}while(res>9);
if(res==1)
[Link]("It is a happy number");
else
[Link]("It is not a happy number");
}
public static void main(String args[])
{
Happy obj=new Happy();
[Link](28);
[Link]();
}
}
Sample Output:
It is a happy number
Question 12
/**
* A class Tribo has been defined to generate the Tribonacci series 0, 1, 2, 3, 6, 11,...
* (Tribonacci series are those in which the sum of the previous three terms is equal to the
next term).
* Some of the members of the class are given below:
* Class name:Tribo
* Data members:
* start - integer to store the start value
* end - integer to store the end value
* Member functions:
* Tribo() - default constructor
* void read() - to accept the numbers
* int tribo(int n) - return the nth term of a Tribonacci series using recursive technique
* void display() - displays the Tribonacci series from start to end by invoking the function
tribo()
* Specify the class Tribo, giving details of the Constructor, void read(), int tribo(int), and void
display().
* Define the main() function to create an object and call the functions accordingly to enable
the task.
import [Link].*;
public class Tribo
{
int start,end;
Tribo()
{
start=0;
end=0;
}
public void read()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the start and end values");
start=[Link]();
end=[Link]();
}
public int tribo(int n)
{
if(n==0||n==1||n==2)
return n;
else
return (tribo(n-1)+tribo(n-2)+tribo(n-3));
}
public void display()
{
for(int i=0;i<=end;i++)
{
int res=tribo(i);
if(res>=start&&res<=end)
[Link](res+" ");
}
}
public static void main(String args[])
{
Tribo obj=new Tribo();
[Link]();
[Link]();
}
}
Sample Output:
Enter the start and end values
0
11
0 1 2 3 6 11
Question 13
* A class Fibo has been defined to generate the Fibonacci series 0, 1, 1, 2, 3, 5,...
* (Fibonacci series are those in which the sum of the previous two terms is equal to the next
term).
* Some of the members of the class are given below:
* Class name:Fibo
* Data members:
* start - integer to store the start value
* end - integer to store the end value
* Member functions:
* Fibo() - default constructor
* void read() - to accept the numbers
* int fibo(int n) - return the nth term of a Fibonacci series using recursive technique
* void display() - displays the Fibonacci series from start to end by invoking the function
Fibo()
* Specify the class Fibo, giving details of the Constructor, void read(), int fibo(int), and void
display().
* Define the main() function to create an object and call the functions accordingly to enable
the task.
import [Link].*;
public class Fibo
{
int start,end;
Fibo()
{
start=0;
end=0;
}
public void read()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the start and end values");
start=[Link]();
end=[Link]();
}
public int fibo(int n)
{
if(n==0||n==1)
return n;
else
return (fibo(n-1)+fibo(n-2));
}
public void display()
{
for(int i=0;i<=end;i++)
{
int res=fibo(i);
if(res>=start&&res<=end)
[Link](res+" ");
}
}
public static void main(String args[])
{
Fibo obj=new Fibo();
[Link]();
[Link]();
}
}
Sample Output:
Enter the start and end values
0
8
0112358
14. Class: BinDec
Data Members:
bin → long variable to store binary number.
dec → long variable to store decimal number.
c → to store exponent value.
Member Functions:
BinDec (): Default constructor.
void readBin (): Input binary number.
long convertDec(long): Converts binary to decimal using recursion.
void show (): Displays bin and dec by invoking convertDec(long).
import [Link].*;
public class BinDec
{
long bin, dec;
double c;
BinDec()
{
bin = 0;
dec = 0;
c = 0.0;
}
public void readBin()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter binary number");
bin = [Link]();
}
long convertDec(long bin)
{
if (bin == 0) // Base case
return 0;
int dig = (int)(bin % 10);
return dig + 2 * convertDec(bin / 10);
}
public void show()
{
dec = convertDec(bin);
[Link]("Binary Number = " + bin);
[Link]("Decimal Number = " + dec);
}
public static void main(String args[])
{
BinDec obj = new BinDec();
[Link]();
[Link]();
}
}
15. A class called DigiNumber has been defined to find the frequency of each digit present in
a number & the sum of the digits using recursion.
Class Name: DigiNumber
Data Members:
num : long to store the no.
Member Functions:
default constructor to initialise num = 0
parameterised - DigiNumber(long n) : to initialise
num = n
void DigitFrequency() : find the frequency of each digit present in num & display it
long SumDigits(long N) : returns the sum of the digits of the no. using recursive technique.
void PrintSum() : print the sum of the digits by invoking the recursive function.
import [Link].*;
public class DigiNumber
{
long num;
DigiNumber()
{
num = 0;
}
DigiNumber(long n)
{
num = n;
}
public void DigitFrequency()
{
long dig, c;
for(int i = 0; i <= 9; i++)
{
long m = num;
c = 0;
while(m > 0)
{
dig = m % 10;
if(dig == i)
c++;
m = m / 10;
}
if(c >= 1)
[Link]("Frequency of " + i + " is " + c);
}
}
long SumDigits(long N)
{
if(N == 0) // base case
return 0;
else
return (N % 10 + SumDigits(N / 10));
}
public void PrintSum()
{
long sum = SumDigits(num);
[Link]("Sum of digits is " + sum);
}
public static void main(String args[])
{
DigiNumber obj = new DigiNumber(255775112);
[Link]();
[Link]();
}
}
Input - 255775112
16. A class convert defines a recursive function to perform string related operations. The
details of the class are given below:
Class name: convert
Data Members:
st: a string to store a string
str: a string to store converted string
len: int to store length of string
Member Functions:
convert ():default constructor to assign default values to its data members.
ReadStr () to accept the string from the user.
public char caseconvert(char ch): convert ch to its opposite case and return.
public void RecChange(int l): using recursive technique, extract the characters from the
string st and convert its case by invoking the function caseconvert, and store the converted
string in str.
void printresult(): display the original string and the new string stored in str.
import [Link].*;
public class convert
{
String st, str;
int len;
Convert ()
{
st = "";
str = "";
len = 0;
}
public void ReadStr ()
{
Scanner sc = new Scanner ([Link]);
[Link]("Enter String");
st = [Link]();
len = [Link]();
}
public char caseconvert (char ch)
{
if (ch >= 65 && ch <= 90)
ch = (char) (ch + 32);
else if (ch >= 97 && ch <= 122)
ch = (char) (ch - 32);
return ch;
}
public void recchange (int l)
{
char c;
if (l > -1)
{
c = [Link](l);
recchange(l - 1);
str += caseconvert(c);
}
}
void printresult()
{
[Link]("Original String = " + st);
[Link]("New String = " + str);
}
public static void main (String args[])
{
Convert obj = new Convert ();
[Link]();
[Link]([Link] - 1);
[Link]();
}
}
17.A class Hifact has been defined to find the HCF of two numbers using a recursive
technique. The HCF is used to find the LCM of the two numbers. The details of the class are
given below:
Class Name: Hifact
Data Members:
int a, b, hcf, lcm;
Member Functions:
Hifact (): Default constructor to initialize values to the data members.
public void getdata (): Input values of a and b. Swap a and b if a > b.
public int rechcf (int, int): Find and return the HCF using a recursive technique.
public int findlcm (int, int, int): Find and return the LCM using the values of a, b, and hcf.
public void result (): Invoke rechcf () and findlcm () and print the HCF and LCM of a and b.
import [Link].*;
public class Hifact
{
int a, b, hcf, lcm;
Hifact()
{
a = 0;
b = 0;
hcf = 0;
lcm = 0;
}
public void getdata()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter any 2 integers");
a = [Link]();
b = [Link]();
}
public void change()
{
int temp;
if(a > b)
{
temp = a;
a = b;
b = temp;
}
}
// Recursive HCF using subtraction method
public int rechcf(int a, int b)
{
if(a == b)
return a;
else if(a > b)
return rechcf(a - b, b);
else
return rechcf(a, b - a);
}
public int findlcm(int a, int b, int hcf)
{
int lcm = (a * b) / hcf;
return lcm;
}
public void result()
{
int res1 = rechcf(a, b);
int res2 = findlcm(a, b, res1);
[Link]("HCF = " + res1);
[Link]("LCM = " + res2);
}
public static void main(String args[])
{
Hifact obj = new Hifact();
[Link]();
[Link]();
[Link]();
}
}