Class XII Assignment 5 (Prac)
Class XII Assignment 5 (Prac)
//Write a program to accept a number and check whether it is a palindrome number or not (use
recursive technique)
import java.util.*;
class R_Palindrome
{
static int rev(int n, int r)
{
if(n == 0)
return r;
r=(r * 10)+(n % 10);
return rev(n/10,r);
}
public static void main ()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a Number");
int n=in.nextInt();
int r= rev(n, 0);
if (r==n)
System.out.println("It is a palindrome Number");
else
System.out.println("It is not a palindrome Number" );
}
}
INPUT/OUTPUT
Enter a Number
141
It is a palindrome Number
Enter a Number
182
It is not a palindrome Number
Question 12. Date 10/08/2023
//Write a program to accept a string and check whether it is a palindrome string or not (use recursive
technique)
import java.util.*;
class R_S_Palindrome
{
public static boolean isPalin(String s)
{
if(s.length() == 0 || s.length() == 1)
return true;
if(s.charAt(0) == s.charAt(s.length()-1))
return isPalin(s.substring(1,s.length()-1));
return false;
}
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String str = in.nextLine();
if(isPalin(str))
System.out.println(str+" is a palindrome");
else
System.out.println(str+" is not a palindrome");
}
}
INPUT/OUTPUT
Enter a String
madam
madam is a palindrome
Enter a String
computer
computer is not a palindrome
Question 13. Date 17/08/2023
//Write a program to accept any 10 numbers and also a number to search. Perform binary search using
recursive technique.
import java.util.*;
class R_b_Search
{
static int b_Search(int arr[], int sch)
{
int beg,end;
beg = 0;
end = arr.length-1;
return b_Search(arr,sch,beg,end);
}
static int b_Search(int arr[], int sch, int beg, int end)
{
if(beg>=end)
{
if(arr[end]==sch)
{
return end;
}
else
{
return -1;
}
}
int mid = (beg+end)/2;
if(arr[mid]==sch)
{
return mid;
}
else if(arr[mid]>sch)
{
return b_Search(arr,sch,beg,mid-1);
}
else
{
return b_Search(arr,sch,mid+1,end);
}
}
public static void main()
{
int arr[] = new int[10];
System.out.println("Enter 10 numbers in ascending order.");
Scanner in = new Scanner(System.in);
for(int i =0;i<10;i++)
{
arr[i] = in.nextInt();
}
System.out.println("Enter a number to search.");
int num = in.nextInt();
int pos = b_Search(arr,num);
if(pos!= -1)
{
System.out.println("The number is at index: "+ pos);
}
else
{
System.out.println("Number Not Found.");
}
}
}
INPUT/OUTPUT
Enter 10 numbers in ascending order.
45
56
67
74
85
82
89
90
92
95
Enter a number to search.
85
The number is at index: 4
/*An Armstrong number is such that the sum of the cube of the digits of the number is the number
itself. Example 153= 13+53+33
Design a class R_Armstrong to perform the given task. Some of the members of the class are given
below:
import java.util.*;
class R_Armstrong
{
int num, sum;
R_Armstrong(int n)
{
num=n;
sum=0;
}
int power(int a,int b)
{
if(b==0)
return 1;
else
return a*power(a,b-1);
}
void arm()
{
int an=num,r;
int sum1=(int) sum;
while(an>0)
{
r=an%10;
an=an/10;
sum1=sum1+power(r,3);
}
if(num==sum1)
System.out.println("Armstrong number");
else
System.out.println("Not an Armstrong number");
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number:");
int num=sc.nextInt();
R_Armstrong obj=new R_Armstrong(num);
obj.arm();
}
}
/*Output:
Enter the number: 153
Armstrong number
A disarium number is a number in which the sum of the digits to the power of their respective position is
equal to the number itself.
Example: 135 = 11 + 32 + 53
Hence, 135 is a disarium number.
Design a class Disarium to check if a given number is a disarium number or not. Some of the members of
the class are given below:
Class name : Disarium
Data members/instance variables:
int num : stores the number
int size : stores the size of the number
Methods/Member functions:
Disarium (int nn) : parameterized constructor to initialize the data members n = nn and
size = 0
void countDigit() : counts the total number of digits and assigns it to size
int sumofDigits (int n, int p) : returns the sum of the digits of the number(n) to the power of their
respective positions (p) using recursive technique
void check() : checks whether the number is a disarium number and displays the
result with an appropriate message
Specify the class Disarium giving the details of the constructor! ), void countDigit(), int sumofDigits(int,
int) and void check(). Define the mainO function to create an object and call the functions accordingly to
enable the task.
class Disarium
{
public static void main ()
{
Scanner in = new Scanner (System.in));
System.out.println ("Enter a number");
int n = in.nextInt();
int copy = n, d = 0, sum = 0;
String s = Integer.toString (n);
int len = s.length();
while (copy >0)
{
d = copy % 10;
sum = sum + (int) Math.pow (d, len);
len--;
copy = copy/10;
}
if (sum == n)
System.outprintln (“Is a disarium no”);
else
System.out.prinln (“Is not a disarium no”);
}
}
Design a class ArmNum to check if a given number is an Armstrong number or not. [A number is said to
be Armstrong if sum of its digits raised to the power of length of the number is equal to the number]
Example:
371 = 33 + 73 + 13
1634 = 14 + 64 + 34 + 44
54748 = 55 + 45 + 75 + 45 + 85
Thus, 371, 1634 and 54748 are all examples of Armstrong numbers.
Some of the members of the class are given below:
Class name : ArmNum
Data members/instance variables:
n : to store the number
1 : to store the length of the number
Methods/Member functions:
ArmNum (int nn) : parameterized constructor to initialize the data member n = nn
int sum_pow(int i) : returns the sum of each digit raised to the power of the length of the number
using recursive technique eg., 34 will return 32 + 42 (as the length of the number is 2)
void isArmstrong() : checks whether the given number is an Armstrong number by invoking the
function sum_pow () and displays the result with an appropriate message.
Specify the class ArmNum giving details of the constructor( ), int sum_pow(int) and void isArmstrong( ).
Define a main() function to create an object and call the functions accordingly to enable the task.