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

Class XII Assignment 5 (Prac)

The document contains 16 programming questions related to recursion. Each question provides sample code to solve a problem using recursion and sample input/output. Some examples of problems covered include: checking if a number is a palindrome, checking if a string is a palindrome, performing binary search recursively, checking if a number is an Armstrong number, and checking if a number is a Disarium number. The questions are intended as practice problems for students to write and test recursive functions.

Uploaded by

tojopaji298
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)
39 views9 pages

Class XII Assignment 5 (Prac)

The document contains 16 programming questions related to recursion. Each question provides sample code to solve a problem using recursion and sample input/output. Some examples of problems covered include: checking if a number is a palindrome, checking if a string is a palindrome, performing binary search recursively, checking if a number is an Armstrong number, and checking if a number is a Disarium number. The questions are intended as practice problems for students to write and test recursive functions.

Uploaded by

tojopaji298
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
You are on page 1/ 9

Assignment 5

Class XII A/B/C Computer Practical Questions

Write it in your practical notebook with algorithm and variable description

Question 11. Date 03/08/2023

//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

Enter 10 numbers in ascending order.


20
32
35
42
45
56
69
78
89
91
Enter a number to search.
25
Number Not Found.

Question 14. Date 24/08/2023

/*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:

Class name : R_Armstrong


Data member/Instance variables :
num : integer to store the number
sum : integer to store the sum of the cube of the digits
Member methods:
R_Armstrong(….) : to initialize data member num, and assign 0 to sum.
int power(int a,int b) : to calculate a^b using recursive function
void arm() : to verify and print whether the member data is an Armstrong number
or not.
Specify the class R_Armstrong,giving the details of the above data members and methods. Also define
the main() to create an object and call functions accordingly to enable the task*/

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

Enter the number: 245


Not an Armstrong number*/
Question 15. Date 31/08/2023

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”);
}
}

Question 16. Date 14/09/2023

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.

import java util.*;


class ArmNum
{
int n;
int 1;
public ArmNum(int num)
{
n = num;
i = 0;
for(int i = n; i! = 0; i/= 10)
i++;
}
public int sumPow(int i)
{ if(i < 10)
return (int)Math.pow(i, 1);
return (int)Math.pow(i % 10, 1) + sumPow(i/10);
}
public void isArmstrong()
{
if(n == sumPow(n))
System.out.println(n+" is an Armstrong number.");
else
System.out.println(n+ " is not an Armstrong number.");
}
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.print("N =");
int num = in.nextInt();
ArmNum obj = new ArmNum(num);
obj.isArmstrong();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy