Prac
Prac
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
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:
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:
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
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:
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