Xii Practical Question21
Xii Practical Question21
Write a program in java to input a number and check whether it is a kaprekar number or not.
Note: A positive whole number ‘n’ that has ‘d’ number of digits is squared and split into two
pieces, a right hand piece that has d digits and a left hand piece that has d or d-1 digits.
If the sum of the two equal pieces is equal to the number,then’n’ is a kaprekar number.
The first few kaprekar numbers are : 9,45,297…
Example 1: 9
9^2 =81,right hand piece of 81 is 1 and left hand piece of 81 is 8
Sum=1+8=9
i.e equal to the number. hence 9 is a kaprekar number.
Question no 2:
Consider a sequence of natural numbers
1,2,3,4,5,6,7,8,9,10…..
Removing every second number we get
1,3,5,7,9,11,13,15
Removing every third number in the process we get
1,3,7,9,13,15,…
And this process continues and we observe that some numbers remain indefinitely in the
sequence,these numbers are called lucky numbers.
Write a program in java to generate and display all lucky numbers upto a given limit
Example 1:
Input n=10
Output
Lucky numbers within 10 are 1,3,7
Question no 3:
Write a program in java to accept a decimal number and convert that to a hexadecimal
number.
Display both the hexadecimal and decimal forms of the number.
Question no 4 :
The Mobius function M(N) for a natural number N is defined as follows:
M(N)=0 if any prime factor of n is contained in n more than once
M(N)=-1p if n is a product of p distinct prime factors.
Examples:
M(78)=-1(for 78 = 2*3*13 and M(78)=-13=-1)
M(34)=1(for 34 =2*17 and M(34)=-12=1).
Adam number : the square of a number and the square of its reverse are reverse to each
Other .
Example : if n=13 and reverse of ‘n’=31then(13^2 =169 and 31^2=961 then reverse of
169=961)
Accept two positive integers m and n. where m is less than n as user input.
Display all prime adam numbers that are in the range between n and m(both inclusive)
And output them along with the frequency in the format given below:
Test your program with the following data and some random data:
Example 1:
INPUT:
M=5 n=100
Output :
The prime – adam inmtegers aree :
11 13 31
Frequency of Prime adam Integers is:3
Example 2:
Input
M=100 n=200
Output:
The prime adam integers are:101 103 113
Frequency of prime adam integers is :3
Question no 6:
Write a program to accept the year,month and the weekday name of the 1 st day of that month
and generate its calendar.
Example :
Input:
Year:
2016
Month:
February
1 st day of February: Monday
Output:
February 2016
Sun Mon Tue Wed Thu Fri Sat
0 1 2 3 4 5 6
7 8 9 10 11 12 13
….
Question no 7:
Write a program to accept 2 dates in the string format dd/mm/yyyy
And find the difference in days between the 2 dates
Example :
Input:
Date 1:
20/12/2012
Date 2 :
11/02/2013
The program should include the part for validating the inputs namely the date and the day on
the first January of that year
Question no 8:
Caesar cipher is an encryption technique which is implemented as rot13 (‘rotate by 13
places’0
It is a simple letter substitution cipher that replaces a letter with the letter 13 places after it
in the alphabets, with the other characters remaining unchanged.
Write a program to accept a plain text of length L, where L must be greater than 3 and less
than 100
Encrypt the text if valid as per the Caesar cipher.
Test your program with the sample data and some random data.
Example 1 :
Input:
Hello! How are you?
Output:
The cipher text is :
Uryyb!ubj ne rib
Question no 9:
Accept a paragraph pf text consisting of sentences that are terminated by either a “.”(full
stop),”!”(excalamation mark) or a “?”(question mark).
Assume that there can be maximum 10 sentences in a paragraph.
Write a program in java to arrange the sentences in increasing order of their number of words
Example:
Input:
Hello! how are you? Please come and attend the party
Output:
Hello!=1
Question no 10:
Write a program in java to accept a sentence which may be terminated by either full stop
question mark or exclamation mark only.
The words are to be separated by a single blank space and are in upper case
Perform the following tasks :
a)check for the validity of the accepted sentence using the terminating character.
b)arrange the words in ascending order of their length. If two or more words have the
same length ,then sort them alphabetically.
Question no 11:
A square matrix is said to be a magic square, if the sum of each row each column and each
diagonal is same .
Write a program to enter an integer number ‘n’. create a magic square of size ‘n*n’. Finally,
print the elements of the matrix as Magic Square.
Note: n<=5
Sample Output:
Question: 12
Write a program to declare a square matrix A[ ] [ ] of order N(N < 20). Allow the user to input
positive integers into this matrix. Perform the following tasks on the matrix.
i. Output the original matrix.
ii. Find the SADDLE POINT for the matrix.
A saddle point is an element of the matrix such that it is minimum element for
the row to which it belongs and the maximum element for the column to which it
belongs. Saddle point for a given matrix is always unique.
Example: In the Matrix 4 5 6
789
513
Saddle point = 7 because it is the minimum element of row 2 and maximum element
of column 1.
Question: 13
Write a Program in Java to fill a square matrix of size ‘n*n” in circular fashion ( clockwise)
With natural numbers from 1 to n*n, taking ‘n’ as input.
For example: if n = 4, then n*n = 16, hence the arrary will be filled as given below.
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
Question: 14
A class Rearrange has been defined to insert an element and to delete an element and to
delete an element from an array. Some specify the class Rearrange giving details of the
functions void enter(), void insert(), void disp10, void disp20 and void remov(). The main
function need not be written.
Question no 15:
A class ader has been defined to add any two accepted time. Example :
time a - 6 hours 35 minutes
Time b- 7 hours 45 minutes
Their sum is 14 hours 20 minutes (where 60 minutes = 1 hour)
The details of the members are given below :
Class name : adder
Data members / instance variables : a[ ]:
Integer array to hold two elements(hours and minutes)
Question no 16:
Class collection contains an array of 100 integers. Using the following class descriptions
create an array with some common elements from two integer arrays. Some of the members
of the class are given below :
Question no 17:
Write a java program to accept n elements from the user and sort the elements using insertion
sort method
Question no 18:
Write a java program to accept n elements from the user and sort the elements using
selection sort method
Question no 19:
Methods
Extractphoneno(): receives the names whose phone numbers is to be displayed and the
displays the extracted phone number along with the names and last names .
Assume that the fields are tab separated and the last names width is 7 letter also write
method main()
That obtain the names of the person whose number is to be displayed and invokes the method
accordingly
Question no 20:
Write a java program to accept 10 names and store it in text file named name.txt
Sort names in the file and store it in another text file in the name of order.txt
Question no 21:
A binary file named “ABC.DAT” contains the product code unit price and quantity for the
number of items
Write a method to accept the product code ‘p’ and check the availability of the product and
display with an appropriate message.
The method declaration is as follows:
Void findpro(int p)
Question no 22:
A superclass Perimeter has been defined to calculate the perimeter of a parallelogram.
Define a subclass area to compute the area of the parallelogram by using the required data
members of the super class. The details are given below:
Class name: perimeter
Data members/instance variables:
Question no 23:
A super class detail has been defined to store the details of the customer . Define a subclass
bill to compute the monthly telephone charge of the customer as per the chart given below:
Number of calls
1-100
101-200
201-300
Above 300
Rate
Only rental charge
60 paisa per call + rental charge 80 paisa per call + rental charge 1 rupee per call+ rental
charge
The details of both the classes are given below:
Class Name : Detail
Member functions:
Member functions:
Bill(…) : parameterized constructor to assign values to data members of both classes and to
initialize amt = 0.0.
void cal() : calculates the monthly telephone charge as per the charge given above.
void show() : to display the detail of the customer and amount to be paid.
Specify the class Detail giving details of the constructor( ) and void show(). Using the concept
of inheritance, specify the class Bill giving details of the constructor( ), void cal() and void
show().
Specify the class Detail giving details of the constructor( ) and void show(). Using the concept
of inheritance, specify the class Bill giving details of the constructor( ), void cal() and void
show().
Question no 24:
Data members:
Member methods:
Matrix1()-constructor to initialize arrays a and b and the other data members with 0. Void
readlimit()-to input values of m,n,r,c as number of rows and columns of a[] and b[].
Void input_a()-to input matrix a[] of order m*n Void input_b()-to input matrix b[] of order m*n
Void shoowmats()-to print matrix a[][]and matrix b[][].
Class name: matrix2(derived class of matrix 1)
Data member:
Member methods:
C[][] with 0
Void findproduct()-to find the product of matrix c[][],b[][] and store resultant matrix in c[][].The
matrix can be multiplied if n=r.
Void showmatrix()-to print matrix c[][] if multiplication is done ,otherwise output the message
“Matrix a and b cannot be multiplied.
Question no 25:
data member : int number [6][2] to contain marks for six subjects and subject code (numeric)
member function:
int point(): to return the point in each subject according to the following:
marks>=90 1 point
80-89- 2 point
70-79- 3 point
60-69- 4 point
50-59- 5 point
40-49- 6 point
- - - - - - - - - - - - accordingly
Member functions/methods :
Void bestsubjects(): to display the total points and best four subject codes using the concept
of inheritance
Specify the details of both the classes using the concept of inheritance.
Specify the details of the constructor Iscscores(), and functions int point(), void
bestsubjects().
Other functions are written for you and you need not write the main function.
Question no 26:
Question:27
Java program to explain the importance of exception handling in java.create user defined
exception.
Question no 28:
Write a java program to exhibit the importance of exception handling in java(explicit
handling).
Question no 29:
Register is an entity which can hold a maximum of 100 names. The register enables the user
to add and remove names from the topmost end only. Define a class Register with the
following details:
Class name: Register
cap: stores the maximum capacity of the array. top: to point the index of the top end.
Member functions:
Register(int max): constructor to initialize the data members cap = max, top = -1 and create
the string array.
void push(String n): to add names in the register at the top location if possible, otherwise
display the message “OVERFLOW”.
String pop(): removes and returns the names from the topmost location of the register if any,
else returns “$$”
Question no 30:
Write a java program to accept an expression from the user and the validity of the given
expression using stack data structure
Question no 31:
Queue is a linear data structure which enables the user to add elements from the rear end and
remove elements from the front end only, using the concept of FIFO.
Note:
Since insertion and deletion both takes place from different ends, We have to keep record of
the index of both the front as well as the rear end. Consider a q[] is implemented using an
array q[].index of the front element is stored in the variable ’front ’and index of the last
element is store in the variable rear.
Question:32
Write a program to create a binary tree and perform the inorder traversal
Question:33
Question no 34:
Write a program to create a binary tree and perform the postorder traversal
Question no 35:
A class called Evenseries has been definedto find the smallest value of the integer ‘n’ such
that , 2 + 4/2! + 8/3 + 6/4! + ……. + 2n/n!>=s where 2.0<s<7.0
Some of the members of the class evenseries are as follows
Member Functions/methods:
EvenSeries(): constructor to initialize data members to 0.
Specify the class EvenSeries giving the details of the constructor and the functions void
accept(),long fact(long x), and void disp().
The main function need not to be written.
Question no 36:
The lcm of two or more non zero integers is the least positive integer divisible by all of them.
Denoted as lcm(a,b)
The gcd of two integers is the largest integer that divides them both . This is usually denoted
as gcd(a,b). There is a relationship between lcm and gcd of two numbers say a and b that
lcm(a,b)*gcd(a,b)=ab
That is the product of the lcm and the gcd is equal to the product itself . write a class
numbers that contains these methods :
i).lcm that receives two integers and returns the lcm of the two integers
ii).gcd that receives two integers and returns the gcd of the two integers
iii). Relation that receives two integers and displays the relationship between lcm and gcd of
these two numbers
iv).main() that reads two numbers from the user and analyzes relationship of these two
numbers.