0% found this document useful (0 votes)
18 views19 pages

Prac

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)
18 views19 pages

Prac

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

Question 11:

A Prime-Adam integer is a positive integer (without leading


zeros) which is a prime as well as an Adam number.
Prime number: A number which has only two factors, i.e. 1 and
the number itself.
Example: 2, 3, 5, 7 … etc.
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' = 31, then,
(13)2 = 169
(31)2 = 961 which is reverse of 169
thus 13, is an Adam number.
Accept two positive integers m and n, where m is less than n as
user input. Display all Prime-Adam integers that are in the range
between m and n (both inclusive) and output them along with
the frequency, in the format given below:
Sample Input:
m=5
n = 100
Sample Output:
THE PRIME-ADAM INTEGERS ARE:
11 13 31
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3

Algorithm:
Step 1:Start.
Step 2:Definition of static void main.
Step 3:Take two ‘m’ and ‘n’ input from user using Scanner class.
Step 4:Check whether values of m and n are valid or not.
Step 5:Run a loop form m to n to check whether number is both
prime and adam or not and count the numbers.
Step 6:Print the frequency of Prime Adam number or print an
appropriate output if no number is present within the range.
Step 7:End of static void main.
Step 8:Definition of int reverse.
Step 9:Run a while loop to reverse the number and return the
reversed number.
Step 10:End of int reverse.
Step 11:Definition of boolean isAdam.
Step 12:Check whether the number is adam or not.
Step 13:End of boolean isAdam.
Step 14:Definition of boolean isPrime.
Step 12:Check whether the number is prime or not.
Step 13:End of boolean isPrime.
Step 14:End.

Program:
Output:

Ente
Ente
THE
11 1
FRE

Variable Description Table:


Variable Variabl Purpose Scope
Name e Type
m int Store lower limit of range. Main method
n int Store upper limit of range. Main method
adam boolean Check number is adam or Second for
not. loop
prime boolean Check number is prime or Second for
not. loop
i int Run for loop. Second for
loop
rev int Store reverse of number. Reverse
function
sqRevNu int Store square of reverse of Adam check
m number. function

Question 12:
Write a program in Java to accept a day number (between 1 and
366), year (in 4 digit) from the user to generate and display the
corresponding date. Also, accept 'N' (1 <= N <= 100) from the
user to compute and display the future date corresponding to 'N'
days after the generated date. Display an error message if the
value of the day number, year, and N are not within the limit or
not according to the condition specified.
Sample Input:
Enter Day Number: 255
Enter N Number of Days(within 1 and 100): 42
Enter Year: 2018
Sample Output:
Date is: 12 th September 2018
Date after N days is: 24 th October 2018

Algorithm:
Step 1:Start.
Step 2:Definition of static void main.
Step 3:Take day,value of n and year input from user using
Scanner class.
Step 4:Make array for date and month.
Step 5:Check condition for leap year and adjust the date in date
array.
Step 6:Calculate the date and check whether the input is in
range or not and print the date accordingly.
Step 7:If the year is not leap year then calculate the date and
print it accordingly.
Step 8:End of static void main.
Step 9:End.
Program:
Output:

Enter Day Number:


280
Enter N Number of Days(within 1 and 100):
90
Enter Year:
2023
Date is : 7 th October 2023
Date after N days is : 5 th January 2024

Variable Description Table:

Variabl Variabl Purpose Scope


e Name e Type
day int Store day input string. Main method
n int Store n input string. Main method
year int Store year input string. Main method
i int Run for loop. Main method
p int Store date after n days. Main method
k int Check whether input is Main method
within range or not.
ar int Store date in array. Main method
br String Store months in array. Main method

Question 13:
Write a program in Java to store the elements in two different
double dimensional arrays (in matrix form) A and B each of
order 4 x 4. Find the product of both the matrices and store the
result in matrix C. Display the elements of matrix C.
Note:
Two matrixes can be multiplied only if the number of columns of
the first matrix must be equal to the number of rows of the
second matrix.

Algorithm:
Step 1:Start.
Step 2:Definition of static void main.
Step 3:Take both arrays input from user using for loop using
Scanner class.
Step 4:Print both the matrices.
Step 5:Perform matrix multiplication using for loop and store
value in matrix.
Step 6:Print the resultant matrix using for loop.
Step 7:End of static void main.
Step 8:End.
Program:

Output:

Enter elements of Matrix A Enter


Row 1 :
1
2
3
4
Enter Row 2 :
-9
8
4
2
Enter Row 3 :
0
-3
10
5
Enter Row 4 :
1
4
3
6
Enter elements of Matrix B Enter
Row 1 :
3
4
32
5
Enter Row 2 :
6
4
23
5
Enter Row 3 :
6
22
64
3
Enter Row 4 :
23
34
5
6

Input Matrix A:
1 2 3
-9 8 4 2
0 -3 10
5
1 4 3
Input Matrix B:
3 4 32 5
6 4 23
5 6 22
64 3
23 34 5 6
Output Matrix C:
125 214 290 48
91 152 162 19
157 378 596 45
183 290 346 70

Variable Description Table:

Variabl Variabl Purpose Scope


e Name e Type
a int Store first array input. Main method
b int Store second array input. Main method
c int Store resultant array. Main method
i int Run for loop. All for loops
j int Run for loop. All for loops
k int Run for loop. All for loops

Question 14:
Caesar Cipher is an encryption technique which is implemented
as ROT13 ('rotate by 13 places'). 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.
ROT13
A/a B/b C/c D/d E/e F/f G/g H/h I/i J/j K/k L/l M/m
↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕
N/n O/o P/p Q/q R/r S/s T/t U/u V/v W/w X/x Y/y Z/z
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.
Sample Input:
Hello! How are you?
Sample Output:
The cipher text is:
Uryyb! Ubj ner lbh?

Algorithm:
Step 1:Start.
Step 2:Definition of static void main.
Step 3:Take string input from user using Scanner class.
Step 4:Check whether the string is within range or not.
Step 5: Run a for loop from 0 to length-1.
Step 6: Extract characters string accordingly.
Step 7:If the character is in the first half of alphabetical series,
add 13 to its ascii else do the reverse.
Step 8:Add the characters to the object.
Step 9:Print the new string outside the loop.
Step 10:End of static void main.
Step 11:End.
Program:

Output:

Enter Plain Text:


Java is Fun.
The Cipher Text is:
Wnin vf Sha.
Variable Description Table:

Variabl Variabl Purpose Scope


e Name e Type
str String Store input string. Main method
len int Store length of string. Main method
ch char Extract characters from For loop
string.
cipher String Store the output. Main method
i int Run for loop. For loop

Question 15:
An Emirp number is a number which 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:
Classname: Emirp
Data members / instance variables:
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

Algorithm:
Step 1:Start.
Step 2:Definition of static void main.
Step 3:Take number input from user using Scanner class.
Step 4:Create object and call isEmirp method.
Step 5:End of static void main.
Step 6:Definition of Emirp.
Step 7:Initialise the global variables.
Step 8:End of Emirp.
Step 9:Definition of isprime.
Step 10:Check number is prime or not using recursion.
Step 11:End of isprime.
Step 12:Definition of isEmirp.
Step 13:Reverse the number and check both the numbers are
prime.
Step 14:End of isEmirp.
Step 15:End.

Program:
Output:
Enter a number
13
13 is an Emirp number
Enter a number
10
10 is not an Emirp number

Variable Description Table:

Variabl Variabl Purpose Scope


e Name e Type
n int Store input number. Emirp
rev int Store reverse of input Emirp
number.
f int Prime checking. Emirp
ans1 int Condition checking for isEmirp method
original number.
ans2 int Condition checking for isEmirp method
reverse number.
x int Take input from user. Main method

You might also like