Computer Project
Computer Project
well as an Adam-number.
[2020]
Adam number: The Square of a number and the square of its reverse are reverse to each
other.
Ex: 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 range between m and n (both inclusive) and output them
along with the frequency, in the following format below:
Ex: INPUT: m=5
n=100
OUTPUT: THE PRIME-ADAM INTEGERS ARE:
11 13 31
FREQUENCY IS: 3
Step 1: Start
Step 2: Getting the upper limit and lower limit to extract the PRIME-ADAM numbers.
Step 3: Checking if the lower limit is greater than the upper limit then displays Invalid Input.
Step 4: Running a loop from lower limit to upper limit to find the PRIME-ADAM numbers between them.
Step 5: Checking if the loop control variable satisfies the conditions of the PRIME-ADAM number.
Step 6: Checking if the reverse of the original number squared is equal to the reverse of the original number squared
and the original number is prime.
Step 7: make a reverse function to return the reverse of the number entered.
Step 8: make a prime function to check and return if the number is prime or not.
Step 9: Print the PRIME-ADAM integers and update the counter variable to keep the count of numbers found.
Step 10: Print the frequency of PRIME-ADAM integers between upper and lower limit.
Step 11: End
UID: 7925885
2
VARIABLE DESCRIPTION
UID: 7925885
3
Q2) Write a program to declare a 1 D array a[] and a square matrix b[][] of size N, where N
>2 and N< 10. Allow the user to input positive integers into the single dimensional array.
Perform the following tasks on the matrix:
[2019]
Sort the elements of the single dimensional array in ascending order using any standard
sorting technique and display the sorted elements.
Fill the square matrix b[][] in the following format.
If that array a[]= {5,2,1,8} the after sorting a[] ={1,2,5,8}
Then the matrix b[][] would fill as below:
1 2 5 8
1 2 5 1
1 2 1 2
1 1 2 5
Display the filled matrix in the above format.
Step 1: Start
Step 2: input the size of Array.
Step 3: check if the size of Array is valid or not.
Step 4: create an array of the required size.
Step 5: input the values from the user.
Step 6: sorting the array by using bubble sort technique.
Step 7: print the sorted array.
Step 8: Create a double dimension array.
Step 9: Filling the Array using nested loops and required variables.
Step 10: Displaying the new double dimension array.
Step 11: End
UID: 7925885
5
VARIABLES DESCRIPTION
UID: 7925885
6
Q3) A company manufactures packing cartons in four sizes i.e. cartons in accommodate 6
boxes, 12 boxes, 24 boxes and 48 boxes. Design a program to accept the number of boxes
to be packed (N) by user (maximum up to 1000 boxes) and display the break-up of the
cartons used in descending order of the capacity (i.e. preference should be given to
highest capacity available,
Ex1:
INPUT: N=726
OUTPUT: 48 * 15 =720
6 *1 = 6
Remaining boxes: =0
Total number of boxes: =726
Total number of cartons: =16
Ex 2:
INPUT: N=140
OUTPUT: 48 * 2 = 96
24 *1 = 24
12*1 = 12
6*1 =6
Remaining boxes 2 * 1 = 2
Total number of boxes =140
Total number of cartons = 6
Step 1: Start
Step 2: input the number of boxes.
Step 3: checking if the number is valid or not and printing relevant messages.
Step 4: create a variable to the total number of cartons required.
Step 5: create an array to keep the limit of each carton.
Step 6: run a loop four times.
Step 7: storing the number of cartons required.
Step 8: keep a count of the total number of cartons used.
Step 9: check if that carton is required or not and print the relevant details.
Step 10: print the number of remaining boxes, total boxes and total Cartons required.
Step 11: End
UID: 7925885
7
int s=0;int n=N; //for total cartons required and copy of no. of original boxes
int a[]={48,24,12,6}; //array to keep limit of boxes
for(int i=0;i<4;i++) //loop running for elements
{
int s1=N/a[i]; //storing the no. of cartons required
s+=s1; //total cartons
if(s1!=0) //checking if the carton is required or not
System.out.println(a[i]+"*"+s1+"="+(a[i]*s1)); //printing statement
N=N%a[i]; //updating the value of no. of boxes
}
System.out.println("Remaining Boxes : 2*"+ (N/2)+"="+(N)); //remaining boxes
System.out.println("Total Boxes : "+n); //total boxes
System.out.println("Total Cartons : "+(s+(N/2))); //total cartons
}
}
}
VARIABLE DESCRIPTION
UID: 7925885
8
Q4) A Goldbach number is a positive even integer that can be expressed as the sum of two
odd primes.(All even integers greater than 4 are Goldbach numbers.) [2018]
Ex: 6=3 +3
10=3 + 7
10= 5 + 5
Write a program to accept an even integer ‘N’ where N>9 and N<50. Find all the odd prime
pairs whose sum is equal to the number ‘N’.
Ex: INPUT: N=30
OUTPUT: PRIME PAIRS ARE: 7, 23
11, 19
13, 17
Step 1: Start
Step 2: input a number.
Step 3: checking if the number is valid or not and printing relevant details.
Step 4: running a loop from 2 to number divided by 2 to print the prime pairs.
Step 5: make a prime function.
Step 6: check if the number entered in the function is prime or not and return the value.
Step 7: in the loop of the main function check if the loop control variable and the difference between the number
and variable is prime or not.
Step 8: printing the number in a specific manner.
Step 9: End
VARIABLE DESCRIPTION
UID: 7925885
10
Q5) Give two positive numbers M and N, such that M is between 100 and 1000 and N is less
than 100. Find the smallest integer that is greater than M and whose digits add up to N. For
example if M=10 and N= 11, then the smallest integer greater than 100 whose digits add up
to 11 is 119.
Write a program to accept the numbers M and N from the user and print the smallest
required number whose sum of all its digits is equal to N. Also print the total number of
digits present in the required number. The program should check for validity of the inputs
and display an appropriate message for an invalid input.
[2015]
Input: M=1500
N=25
Output: The required number is= 1699
Total number of digits=4
Input: M=99
N=11
Output: Invalid Input
Step 1: Start
Step 2: input the lower limit.
Step 3: input the upper limit.
Step 4: checking if both the limits are valid or not.
Step 5: make a copy of the lower limit.
Step 6: running a while loop until the sum of digits is equal to the upper limit.
Step 7: make a function to find and return the sum of digits of the entered number.
Step 8: define the base case and the recursive case of the function.
Step 9: print the required number.
Step 10: print the total no. of digits of the number.
Step 11: End
UID: 7925885
11
Scanner sc = new Scanner(System.in); //object of scanner class
System.out.println("Enter the value of M and N");
int m = sc.nextInt(); //lower limit
int n = sc.nextInt(); //upper limit
if((m>10000||m<100)&&n>100) //invalid condition
System.out.println("invalid input");
else
{
int a=m; //copy of lower limit
while(sod(a)!=n) //while loop to check the condition
a++; //updating the value of a
System.out.println("Required number is= "+a); //printing required details
System.out.println("Total no. of digits= "+(a+"").length());
}
}
}
VARIABLE DESCRIPTION
UID: 7925885
12
Q6) Design a program to accept a day number (between 1 and 366), year (in 4 digits) 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
within the limit or not according to the condition specified.
[2019]
Ex: INPUT: DAY NUMBER: 360
YEAR: 2018
DATE AFTER (N DAYAS): 45
OUTPUT: 26 TH SEPTEMBER, 2018
DATE AFTER 45 DAYS: 9 TH FEBRUARY, 2019
Step 1: Start
Step 2: input the number of days.
Step 3: input the year and number of days ahead.
Step 4: checking the validation of the input values.
Step 5: create a static and void function.
Step 6: create an array to store the name of the months as well as the maximum days in a month.
Step 7: check if the number of days is more than a year to convert them into a year.
Step 8: running a loop to find the date and month of the entered day.
Step 9: printing the date, month and year in specific manner.
Step 10: calling the function first with the original date.
Step 11: calling the function with the number of days ahead.
Step 12: End
VARIABLE DESCRIPTION
UID: 7925885
15
Q7) Write a program to declare a square matrix A[][] of order (M×M) where M must be
greater than 3 and less than 10. Allow the user to input positive integers into this matrix.
Perform following tasks on the matrix.
[2016]
Sort the non boundary elements in ascending order using any standard sorting technique
and rearrange them in matrix.
Calculate the sum of both the diagonals.
Display the original matrix, rearranged matrix and only the diagonal elements of the
rearranged matrix with their sum.
Ex: M=4
Input:
9 2 1 2
8 13 8 4
15 6 3 11
7 12 23 8
Output:
Original matrix:
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
Rearranged matrix
9 2 1 5
8 3 6 4
15 8 13 11
7 12 23 8
Diagonal elements
9 5
3 6
8 13
7 8
Sum of the diagonal elements=59
Step 1: Start
Step 2: enter the number of rows and columns in the square matrix.
Step 3: check if the number of rows is valid or not.
Step 4: create a square matrix.
Step 5: input the elements to fill the matrix.
Step 6: create a temporary array to store required elements.
Step 7: sorting the temporary array using bubble sort technique.
Step 8: updating the rearranged matrix in the original matrix.
Step 9: print the new matrix.
Step 10: print the diagonal elements and evaluate their sum.
Step 11: print the relevant details.
Step 12: End.
VARIABLE DESCRIPTION
UID: 7925885
18
Int temp[] Array to store non boundary elements
UID: 7925885
19
Q8) Write a program to accept a sentence which may be terminated by either ‘.’ ,’?’, or ‘!’
only. The words are separated by a single blank space and are in UPPERCASE. Perform the
following task: [2019]
Check for the validity of the accepted sentence
Convert the non-palindrome words of the sentence into palindrome words by
concatenating the word by its reverse (excluding the last character).
Ex: The reverse of the word HELP would be LEH (omitting the last alphabet) and by
concatenating both, the new palindrome word is HELPLEH. Thus, the word HELP becomes
HELPLEH.
NOTE: The words which end with repeated alphabets, for ex. ABB would becomes ABBA
and not ABBBA and XAZZZ becomes XAZZZAX
Display the original sentence along with the converted sentence.
Step 1: Start
Step 2: accept a sentence from the user.
Step 3: check the validity of the sentence.
Step 4: splitting the words in an array.
Step 5: create an empty string.
Step 6: obtaining the last index where the last character was present in the original string.
Step 7: add the original word to the new string.
Step 8: start storing the reversed word from the obtained index.
Step 9: add a blank space in the new string.
Step 10: print the new string.
Step 11: End
UID: 7925885
21
Q9) The names of the teams participating in a competition should be displayed on a banner
vertically to accommodate as many teams as possible in a single banner. Design a program
to accept the names of N teams, where 2<N<9 and display them in a vertical order, side by
side with a horizontal tab (i.e. eight spaces).
Ex 1:
Input: N=3
Team 1: Emus
Team 2: Road Rols
Team 3: Coyote
Output:
E R C
m o o
u a y
s d o
t
R e
o
l
s
ex 2:
Input : N=10
Output: Invalid Input
Step 1: Start
Step 2: enter the size of the array.
Step 3: get an input for the first team using scanner.
Step 4: create an array of size entered by the user.
Step 5: input the names of the teams.
Step 6: obtain the length of the longest team’s name.
Step 7: run an outer loop for accessing the array.
Step 8: run an inner loop to access the team’s name.
Step 9: print if the length of the team’s name is smaller than the loop variable.
Step 10: print each character of the team’s name.
Step 11: change the line to maintain the format of printing.
Step 12: End
UID: 7925885
23
Q10) Write a program to declare a square matrix A[ ] [ ] of order (M x M) where ‘M’ is the
number of rows and the number of columns such that M must be greater than 2 and less
than 20. Allow the user to input integers into this matrix. Display appropriate error
message for an invalid input. Perform the following tasks:
(a) Display the input matrix.
(b) Create a mirror image matrix.
(c) Display the mirror image matrix.
Test your program with the sample data and some random data:
Example 1
INPUT : M = 3
4 16 12
8 2 14
413
OUTPUT :
ORIGINAL MATRIX
4 16 12
8 2 14
413
MIRROR IMAGE MATRIX
12 16 4
14 2 8
316
Example 2
INPUT : M = 22
OUTPUT : SIZE OUT OF RANGE
Step 1: Start
Step 2: enter the number of rows or columns for the square matrix.
Step 3: check the valid condition after that create an array.
Step 4: enter the elements of each array.
Step 5: print the original matrix.
Step 6: create an array for mirror image.
Step 7: fill the array.
Step 8: print the mirror image of the original array.
Step 9: End
UID: 7925885
25
Q11) Write a program to accept a sentence which may be terminated by either '.' or '?' only.
The words are to be separated by a single blank space. Print an error message if the input
does not terminate with '.' or '?'. You can assume that no word in the sentence exceeds 15
characters, so that you get a proper formatted output.
Perform the following tasks:
Step 1: Start
Step 2: enter a sentence.
Step 3: check the validation of the entered sentence.
Step 4: remove the punctuation at last.
Step 5: split the words of the sentence and store them into an array.
Step 6: make every first word of the sentence capital.
Step 7: print the new sentence.
Step 8: create an array for the count of vowels and consonants.
Step 9: find the number of vowels and consonants of each word.
Step 10: print each word along with the count of vowels and consonants.
Step 11: End
} VARIABLE DESCRIPTION
UID: 7925885
27
Q12) A circular prime is a prime number with the property that the number generated at
each intermediate Step when cyclically permuting its digits will be prime. For example, 1193
is a circular prime, since 1931, 9311 and 3119 all are also prime.
Step 1: Start
Step 2: enter a number.
Step 3: create a static and returning prime function.
Step 4: check if the entered number is prime or not.
Step 5: convert the number to the string.
Step 6: run a loop from 0 to the length of the number.
Step 7: check if the number formed by placing the first digit at last is prime or not.
Step 8: if the number is not prime print that number is not Circular prime and exit the program.
Step 9: as the loop ends print that the number is circular prime.
Step 10: End
VARIABLE DESCRIPTION
UID: 7925885
29
Q13) Write a program to declare a matrix A[][] of order (M x N) where 'M' is the number of
rows and 'N' is the number of columns such that the value of 'M' must be greater than 0 and
less than 10 and the value of 'N' must be greater than 2 and less than 6. Allow the user to
input digits (0 - 7) only at each location, such that each row represents an octal number.
INPUT:
M=3
N=4
ENTER ELEMENTS FOR ROW 1: 1 1 3 7
ENTER ELEMENTS FOR ROW 2: 2 1 0 6
ENTER ELEMENTS FOR ROW 3: 0 2 4 5
OUTPUT:
FILLED MATRIX DECIMAL EQUIVALENT
1 1 3 7 607
2 1 0 6 1094
0 2 4 5 165
Step 1: Start
Step 2: enter the number of rows and columns.
Step 3: check the validation of the number of rows and columns.
Step 4: create a double dimension array of the entered size by the user.
Step 5: create a new string array to keep the values of the entered number in string form to keep the leading 0’s.
Step 8: check the validation of the numbers entered.
Step 9: keep the count of leading 0’s so that matrix can be filled in proper manner.
Step 10: fill each matrix with the digits entered.
Step 11: print the matrix along with the decimal equivalent of each number.
Step 12: End
UID: 7925885
30
Scanner sc=new Scanner(System.in); //object of scanner class
System.out.println("Enter the values of M and N");
int M=sc.nextInt();
int N=sc.nextInt();
if(M>0&&M<10&&N>2&&N<6) //validating statements
{
int a[][]=new int[M][N];
for(int i=0;i<M;i++)
{
System.out.print("Enter elements for row "+(i+1)+" ");
for(int j=0;j<N;j++)
a[i][j]=sc.nextInt();
for(int j=0;j<N;j++)
if(a[i][j]>7)
{
System.out.println("Invalid input");
System.exit(0);
}
System.out.println();
}
System.out.println("FILLED MATRIX \t DECIMAL EQUIVALENT");
for(int i=0;i<M;i++) //printing the matrix in required form
{
String s="";
for(int j=0;j<N;j++)
{
System.out.print(a[i][j]);
s+=a[i][j];
}
System.out.print("\t\t"+convert(Integer.parseInt(s)));
System.out.println();
}
}
else
System.out.println("Out of range");
}
}
UID: 7925885
31
VARIABLE DESCRIPTION
String a1[] To store each number to prevent the loss of leading 0’s
UID: 7925885
32
Q14) Write a program to accept a sentence which may be terminated by either '.', '?' or '!'
only. The words are to be separated by a single blank space and are in UPPER CASE.
Perform the following tasks:
Check for the validity of the accepted sentence only for the terminating character.
Arrange the words in ascending order of their length. If two or more words have the same
length, then sort them alphabetically.
Display the original sentence along with the converted sentence.
Test your program for the following data and some random data:
Example 1:
INPUT:
AS YOU SOW SO SHALL YOU REAP.
OUTPUT:
AS YOU SOW SO SHALL YOU REAP.
AS SO SOW YOU YOU REAP SHALL
Step 1: Start
Step 2: accept a sentence from the user.
Step 3: check if the sentence ends with .?! only or not.
Step 4: remove the punctuation at last.
Step 5: split the sentence of each word by the spaces present between them.
Step 6: use bubble sort technique to re arrange the words.
Step 7: swap the two words on the basis of the length of each word.
Step 8: arrange the words of equal length alphabetically.
Step 9: print the rearranged array.
Step 10 : End
UID: 7925885
33
sa[j]=sa[j+1];
sa[j+1]=t;
}
if(sa[j].compareTo(sa[j+1])>0 && sa[j].length()==sa[j+1].length())
{
String t=sa[j];
sa[j]=sa[j+1];
sa[j+1]=t;
}
}
for(int i=0;i<sa.length;i++)
System.out.print(sa[i]+" "); //printing sorted array
}
else
System.out.println("Invalid input");
}
}
VARIABLE DESCRIPTION
UID: 7925885
34
Q15) Write a program to declare a square matrix A[][] of order MxM where ‘M’ is the number
of rows and the number of columns, such that M must be greater than 2 and less than 10.
Accept the value of M as the user input. Allow the user to input integers into this matrix.
Perform the following tasks:
Display the original matrix.
Rotate the matrix 90 degrees clockwise.
Find the sum of the elements of the four corners of the matrix. Test your program with
sample data and random data.
Step 1: Start
Step 2: input the number of rows and columns of the square matrix.
Step 3: check the validation of the input by the user.
Step 4: create an array of size entered by the user.
Step 5: input the values for the matrix.
Step 6: print the original array.
Step 7: print the matrix such that the array is rotated clockwise 90 degrees.
Step 8: print the sum of the corner element of the matrix.
Step 9: End
UID: 7925885
36
Q16) A prime palindrome integer is a positive integer (without leading zeros) which is prime
as well as a palindrome. Given two positive integers m and n, where m < n, write a program
to determine how many prime-palindrome integers are there in the range between m and n
(both inclusive) and output them.
The input contains two positive integers m and n where m < 3000 and n < 3000. Display the
number of prime palindrome integers in the specified range along with their values in the
format specified below:
Test your program with the sample data and some random data:
Example 1
INPUT: M = 100
N = 1000
OUTPUT: The prime palindrome integers are:
101,131,151,181,191,313,351,373,383,727,757,787,797,919,929
Frequency of prime palindrome integers: 15
Example 2
INPUT: M = 100
N = 5000
OUTPUT: Out of range
Step 1: Start
Step 2: Import the util package for Scanner class
Step 3: Make a static boolean prime function to check if the number is prime or not
Step 4: Return false if the number is 1.
Step 5: If any number between 2 and num/2 divides then the number is not prime
Step 6: Return appropriate outputs
Step 7: Make a static boolean palindrome function.
Step 8: Return appropriate outputs according to the conditions
Step 9: Make an object of Scanner class and enter the range
Step 10: Check the validity of the range
Step 11: Run a loop to traverse the range and print the prime palindrome numbers.
Step 12: Print the frequency of the occurrence of prime palindrome numbers.
Step 13: End
import java.util.*;
public class que16
{
static boolean prime(int n) //A function to check prime
{
if(n==1)
return false;
for(int i = 2;i<=n/2;i++)
if(n%i==0)
return false; //returning statements
return true;
}
static boolean palindrome(int n) //function to check palindrome number
{
int rev=0,c=n; //storing the reverse and copy of original number
UID: 7925885
37
while(c!=0) //reverse the number
{
rev = rev*10+(c%10);
c/=10;
}
if(n==rev) //returning statements
return true;
else
return false;
}
public static void main() //main function to call other functions
{
Scanner sc= new Scanner(System.in); //Scanner’s object
System.out.println("Enter M and N:"); //input the range
int M =sc.nextInt();
int N =sc.nextInt();
if(M>3000 || N>3000) //invalidating conditions
System.out.println("OUT OF RANGE");
else
{
System.out.println("the prime palindrome integers are: ");
int c=0; //counting variable
for(int i = M;i<=N;i++) //finding prime palindrome numbers between the range
if(prime(i) && palindrome(i))
{
System.out.print(i+",");
c++;
}
System.out.println("\nFrequency of prime palindrome integers are: "+c); //printing
frequency
}
}
} VARIABLE DESCRIPTION
UID: 7925885
38
Q17) Write a program to accept a sentence as input. The words in the string are to be
separated by a blank. Each word must be in upper case. The sentence is terminated by
either “.”,”!” or “?”. Perform the following tasks:
(i) Obtain the length of the sentence. (measured in words)
(ii) Arrange the sentence in alphabetical order of the words.
Test your program with the sample data and some random data:
Example 1:
INPUT: NECESSITY IS THE MOTHER OF INVENTION.
OUTPUT:
Length: 6
Rearranged Sentence:
INVENTION IS MOTHER NECESSITY OF THE
Example 2:
INPUT: BE GOOD TO OTHERS.
OUTPUT:
Length: 4
Rearranged Sentence: BE GOOD OTHERS TO
Step 1: Start
Step 2: Importing the util package
Step 3: Creating a class and main function
Step 4: Creating an object of scanner class
Step 5: Input the sentence with appropriate message.
Step 6: checking the valid condition.
Step 7: remove the punctuation from the sentence.
Step 8: create an array of words separated by space.
Step 9: sort the array using Bubble sort technique.
Step 10: print the new sentence.
Step 11:End
VARIABLE DESCRIPTION
UID: 7925885
40
Q18) 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 remaining ‘d’ or
‘d-1’ digits. If the sum of the two pieces is equal to the number, then ‘n’ is a Kaprekar
number. The first few Kaprekar numbers are: 9, 45, 297 ……..
Example 1:
9
92 = 81, right-hand piece of 81 = 1 and left hand piece of 81 = 8
Sum = 1 + 8 = 9, i.e. equal to the number.
Example 2:
45
452 = 2025, right-hand piece of 2025 = 25 and left hand piece of 2025 = 20
Sum = 25 + 20 = 45, i.e. equal to the number.
Given the two positive integers p and q, where p < q, write a program to determine how
many Kaprekar numbers are there in the range between p and q (both inclusive) and output
them.
The input contains two positive integers p and q. Assume p < 5000 and q < 5000. You are to
output the number of Kaprekar numbers in the specified range along with their values in
the format specified below:
SAMPLE DATA:
INPUT:
p=1
q = 1000
OUTPUT:
THE KAPREKAR NUMBERS ARE:-
1, 9, 45, 55, 99, 297, 703, 999
FREQUENCY OF KAPREKAR NUMBERS IS: 8
Step 1: Start
Step 2: make a static boolean function to check if the number is kaprekar or not
Step 3: find out the square of the number
Step 4: extract the parts of the squared number
Step 5: check if the sum of the parts are equal to the number or not
Step 6: Obtain the range from the user
Step 7: check for the validation
Step 8: count the frequency of the numbers
Step 9: print the numbers in order
Step 10: End
UID: 7925885
42
Q19) A composite Magic number is a positive integer which is composite as well as a
magic number.
Composite number: A composite number is a number which has more than two factors. For
example: Factors of 10 are: 1,2,5,10
Magic number: A Magic number is a number in which the eventual sum of the digit d is
equal to 1. For example: 28=2+8=10==>1+0=1
Accept two positive integers m and n, where m is less than n as user input. Display the
number of composite magic integers that are in the range between m and n (both inclusive)
and output them along with frequency of magic numbers in that range.
Step 1: Start
Step 2: make a boolean static composite function to check if the number is composite
Step 3: make a function to calculate the sum of the number
Step 4: run a while loop till number becomes 0
Step 5: get the value of sum of digits
Step 6: make a isMagic() function to check if the number is magic or not
Step 7: run a while while the number is one digit
Step 8: if the one digit number is one return true
Step 9: enter the range from the user in main function
Step 10: run a loop between the ranger and check teacher number
Step 11: along with that count the frequency also
Step 12: print the relevant details
Step 13: End
UID: 7925885
44
Q20) An ISBN ( International Standard Book Number) is a ten digit code which uniquely
identifies a book. The first nine digits represent the Group, Publisher and Title of the book
and the last digit is used to check whether ISBN is correct or not.
Each of the first nine digits of the code can take a value between 0 and 9. Sometimes it is
necessary to make the last digit equal to ten; this is done by writing the last digit of the
code as X. To verify an ISBN, calculate 10 times the first digit, plus 9 times the second digit,
plus 8 times the third and so on until we add 1 time the last digit. If the final number leaves
no remainder when divided by 11, the code is a valid ISBN.Design a program to accept a
ten digit code from the user. For an invalid input, display an appropriate message. Verify
the code for its validity in the format specified below. Test your program with sample data
and some random data.
Example 1
INPUT CODE : 0201530821
OUTPUT : SUM = 99
LEAVES NO REMAINDER - VALID ISBN CODE
Example 2
INPUT CODE : 035680324
OUTPUT : INVALID INPUT
Example 3
INPUT CODE : 0231428031
OUTPUT : SUM = 122
LEAVES REMAINDER - INVALID ISBN CODE
Step 1: Start
Step 2: make a static int function to calculate the sum according to ISBN code
Step 3: calculate the sum according to index of ISBN number
Step 4: check for the presence of ‘X’ in the ISBN code
Step 5: add respective digits
Step 6: return the value obtained
Step 7: take input from the user
Step 8: check for valid length
Step 9: calculate the sum of ISBN number entered
Step 10: print relevant details
Step 11: End
import java.util.*; //import the package
public class que20
{
static int sum(String n) //evaluating the sum of ISBN
{
int s=0;
for(int i=0;i<n.length()-1;i++) //calculation of sum of all digits
s+= Integer.parseInt(n.charAt(i)+"")*(10-i); //storing the sum
if(n.charAt(n.length()-1)=='x'||n.charAt(n.length()-1)=='X') //checking for the
presence if 'X'
UID: 7925885
45
s+= 10;
else
s+= Integer.parseInt(n.charAt(n.length()-1)+"");
return s; //returning statements
}
public static void main()
{
Scanner sc=new Scanner(System.in); //object of scanner class
System.out.println("Enter an ISBN code: "); //input from user
String n =sc.next();
if(n.length()!=10) //not of valid length
System.out.println("Invalid Input");
else
{
System.out.println("Sum: "+sum(n)); //sum of all digits according to ISBN
if(sum(n)%11==0) //checking for valid ISBN code and printing relevant details
System.out.println("valid ISBN code");
else
System.out.println("invalid ISBN code");
}}}
VARIABLE DESCRIPTION
UID: 7925885
46
Q21) Write a program to input a natural number less than 1000 and display it in words. Test
your program on the sample data and some random data.
Step 1: Start
Step 2: import the util package for Scanner class
Step 3: create the main function
Step 4: Create the object of Scanner class
Step 5: check the validity of the number
Step 6: print “zero” if the number is zero
Step 7: create two arrays which consist of the number names from 1- 19 and multiples of ten from 20
Step 8: check for the number of digits of the number then concatenate the number names to string
Step 9: print the final string
Step 10: End
import java.util.*; //importing the util class for Scanner
public class que21
{
public static void main() //main function for the execution of program
{
Scanner sc= new Scanner(System.in); //object of Scanner class
System.out.println("Enter a number less than"); //asking for input from user
int n=sc.nextInt(); //user input
if(n>=1000) //validating condition
System.out.println("Invalid Input");
else
{
if(n==0) //if the number is zero
System.out.println("Zero");
else //arrays for storing number names according to use
{
String
s1[]={"","one","two","three","four","five","six","seven","eight","nine","ten","eleven",
"twelve","thirteen", "fourteen","fifteen" ,"sixteen", "seventeen"
,"eighteen","nineteen"};
String
s2[]={"","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
String num=n+"";String s="";
if(num.length()==3) //if the number is 3 digit
{
s+= s1[Integer.parseInt(num.charAt(0)+"")]+" hundred "; //storing for the value
of 100's
num =num.substring(1); //making the number shorter
if(Integer.parseInt(num)<=19) //for 10's number name
s+=s1[Integer.parseInt(num)];
else
UID: 7925885
47
s+= s2[Integer.parseInt(num.charAt(0)+"")]+"
"+s1[Integer.parseInt(num.charAt(1)+"")];
}
else if(num.length()==2) //if the number is 2 digit
{
if(Integer.parseInt(num)<=19) //extracting the word for less than 19 from
array
s+=s1[Integer.parseInt(num)];
else //otherwise from other array for names
s+= s2[Integer.parseInt(num.charAt(0)+"")]+"
"+s1[Integer.parseInt(num.charAt(1)+"")];
}
else
s+= s1[Integer.parseInt(num.charAt(0)+"")];
System.out.println(s); //printing the final number name
}
}
}
} VARIABLE DESCRIPTION
UID: 7925885
48
Q22) Encryption is a technique of coding messages to maintain their secrecy. A String
array of size 'n' where 'n' is greater than 1 and less than 10,stores single sentences (each
sentence ends with a full stop) in each row of the array. Write a program to accept the size
of the array. Display an appropriate message if the size is not satisfying the given
condition. Define a string array of the inputted size and fill it with sentences row-wise.
Change the sentence of the odd rows with an encryption of two characters ahead of the
original character. Also change the sentence of the even rows by storing the sentence in
reverse order. Display the encrypted sentences as per the sample data given below.
Example :
INPUT: n = 4
IT IS CLOUDY.
IT MAY RAIN.
THE WEATHER IS FINE.
IT IS COOL.
OUTPUT:
KV KU ENQWFA.
RAIN MAY IT.
VJG YGCVJGT KU HKPG.
COOL IS IT.
Step 1: Start
Step 2: import the util package for Scanner class
Step 3: make a static returning function to rotate the string passed as parameter
Step 4: run a for loop throughout the length of the string
Step 5: check for the special condition which arises when two is added to ‘y’ or ‘z’
Step 6: return the new string obtained
Step 7: make a static returning function to return the reversed string
Step 8: split the sentence and run the loop in reverse
Step 9: create the object of scanner class
Step 10: check for the validation of the user input
Step 11: take input from the user check its validation and remove the punctuation if present
Step 12: obtain the encoded string by passing it to the required function
Step 13: print the new array
Step 14: End
UID: 7925885
49
if(Character.isLetter((char)(s.charAt(i)+2))) //if the character ahead is letter
s1+=(char)(s.charAt(i)+2);
else //if the character at +2 is not letter then rotating the string
s1+=(char)(s.charAt(i)-24);
}
return s1; //returning the final string
}
static String rev(String s) //reversing the string
{
String s1=""; //storing new string
String sa[]=s.split(" "); //array of words
for(int i=sa.length-1;i>=0;i--) //reversing the words
s1+=" "+sa[i];
return s1.trim(); //returning the string with no leading or trial spaces
}
public static void main()
{
Scanner sc = new Scanner(System.in); //object of scanner class
System.out.println("Enter array size"); //asking for input form user
int x=sc.nextInt(); //user input
if(x<=1||x>=10) //for invalid condition
System.out.println("INVALID INPUT");
else
{
String s[]=new String[x]; //array to store sentences
System.out.println("Enter sentences");
String e=sc.nextLine().toUpperCase(); //user input due to use of scanner
for(int i=0;i<x;i++)
{
s[i]=sc.nextLine().toUpperCase(); //converting to uppercase
if("?!.".indexOf(s[i].charAt(s[i].length()-1))==-1) //validating condition
{
System.out.println("Invalid Input");
System.exit(0); //terminating the program
}
else
s[i]=s[i].substring(0,s[i].length()-1);
}
for(int i=0;i<s.length;i++) //for alternate encoding of strings
UID: 7925885
50
if(i%2==1)
s[i]=rev(s[i]);
else
s[i]=rotate(s[i]);
System.out.println("Encoded sentences:"); //printing the sentences
for(int i=0;i<s.length;i++)
System.out.println(s[i]);
}
}
}
VARIABLE DESCRIPTION
UID: 7925885
51
Q23) A bank intends to design a program to display the denomination of an input amount
upto 5 digits. The available denominations with the bank are of rupees
1000,500,100,50,20,10,5,2 and 1. Design a program to accept the amount from the user and
display the break-up in descending order of denominations. (i,e preference should be given
to the highest denomination available) along with the total number of notes. [Note: only the
denomination used should be displayed].
Also print the amount in words according to the digits.
Example 1
INPUT: 14836
OUTPUT: ONE FOUR EIGHT THREE SIX
DENOMINATION:
1000 X 14 =14000
500 X 1 =500
100 X 3 =300
50 X 1 =50
5 X 1 =5
1 X 1 =1
Example 2
EXAMPLE 2:
INPUT: 235001
OUTPUT: INVALID AMOUNT
Step 1: Start
Step 2: import the util package for Scanner class
Step 3: Create the object of Scanner class
Step 4: Enter the amount from the user
Step 5: Check its validity
Step 6: Create an array for number names and a variable to store number name of each digit
Step 7: run a loop across the length of the array consist of notes
Step 8: calculate the number of notes required
Step 9: print the information in the required format
Step 10: End
UID: 7925885
53
Q24) A smith number is a composite number, the sum of whose digits is the sum of the
digits of its prime factors obtained as a result of prime factorization (excluding 1). The first
few such numbers are 4, 22, 27, 58, 85, 94, 121.....
Example : 666 Prime factors are 2, 3, 3 and 37
Sum of the digits are (6+6+6) = 18
Sum of the digits of the factors 2+3+3+(3+7) = 18
Sample data:
Input 94
Output SMITH Number
Input 102
Output NOT SMITH Number
Step 1: Start
Step 2: import the util package for Scanner class
Step 3: make a static boolean prime function to check if the number is prime or not
Step 4: create a function to calculate the sum of digits
Step 5: run a loop while the number does not becomes 0 and store each digit in separate variable
Step 6: return the sum of digits
Step 7: create the object of Scanner class and take input from user
Step 8: run a loop from 2 to the number
Step 9: store the sum of digit of every prime factor
Step 10: check if the sum of digit of the number is equal to the sum of factors or not
Step 11: print relevant details
Step 12: End
VARIABLE DESCRIPTION
UID: 7925885
55
Q25) A unique-digit integer is a positive integer (without leading zeros) with no duplicate
digits. For example 7, 135, 214 are all unique-digit integers whereas 33, 3121, 300 are not.
Given two positive integers m and n, where m<30000 and n<30000. You are to output the
number of unique-digit integers in the specified range along with their values in the format
specified below:
SAMPLE DATA:
Step 1: Start
Step 2: import the util package for Scanner class
Step 3: make a static returning function to check if the number in parameter is unique or not
Step 4: convert the number to string
Step 5: rum a loop to check every character
Step 6: check if the first index and the last index of the character is same or not
Step 7: Create an object of Scanner class
Step 8: enter the range from user and check for validation
Step 9: run a loop through the range and check if the number is unique or not
Step 10: increase the counter by one every time we come across unique number
Step 11: print the relevant details
Step 12: End
VARIABLE DESCRIPTION
UID: 7925885
57
Q26) 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.
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.
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 ner lbh?
Example 2
INPUT:
Encryption helps to secure data.
OUTPUT:
The cipher text is:
Rapelcgvba urycf gb frpher qngn.
Example 3
INPUT:
You
OUTPUT:
INVALID LENGTH
Step 1: Start
Step 2: import the util package for Scanner class
Step 3: Create the object of Scanner class
Step 4: Enter the sentence from the user and check the validity
Step 5: run a loop for each character of the string
Step 6: stores the letter according to the cases and rotation
Step 7: at last add the special characters if present
Step 8: print the relevant details
Step 9: End
UID: 7925885
59
Q27. Write a program that inputs the names of people into two different arrays, A and B.
Array A has N number of names while Array B has M number of names, with no duplicates
in either of them. Merge array A and B into a single array C, such that the resulting array is
sorted alphabetically. Display all the three arrays, A, B and C, sorted alphabetically. Test
your program for the given data and some random data.
SAMPLE DATA:
INPUT
Enter number of names in Array A, N=2
Enter number of names in Array B, M=3
First array:(A) Suman Anil
Second array:(B) Usha Sachin John
OUTPUT
Sorted Merged array:(C)
Anil John Sachin Suman Usha
Sorted first array:(A)
Anil Suman
Sorted Second array:(B)
John Sachin Usha
Step 1: Start
Step 2: import the util package for Scanner class
Step 3: Create the object of Scanner class
Step 4: Enter the number of names and then create the array
Step 5: sort the names alphabetically in the first array
Step 6: Sort the names alphabetically in the second array also
Step 7: print all the arrays according to the syntax given in the question
Step 8: End
VARIABLE DESCRIPTION
UID: 7925885
62
Q28. A new advanced Operating System, incorporating the latest hi-tech features has been
designed by Opera Computer Systems. The task of generating copy protection codes to
prevent software piracy has been entrusted to the Security Department. The Security
Department has decided to have codes containing a jumbled combination of alternate
uppercase letters of the alphabet starting from A upto K (namely among A,C,E,G,I,K). The
code may or may not be in the consecutive series of alphabets. Each code should not
exceed 6 characters and there should be no repetition of characters. If it exceeds 6
characters, display an appropriate error message. Write a program to input a code and its
length. At the first instance of an error display "Invalid" stating the appropriate reason. In
case of no error, display the message "Valid".
Test your program for the following data and some random data.
SAMPLE DATA
INPUT N=4 ABCE
OUTPUT Invalid! Only alternate letters permitted!
INPUT N=7
OUTPUT ERROR! Length of string should not exceed 6 characters!
Step 1: Start
Step 2: import the util package for Scanner class
Step 3: Create the object of Scanner class
Step 4: ask for the length of the word from the user
Step 5: take a flag variable for conditions
Step 6: Enter the code from the user and match the length with entered length
Step 7: check all the valid conditions if it comes out to be false break the construct and loop
Step 8: if all the conditions are satisfied then initialise the variable to true
Step 9: print the relevant details
Step 10: End
VARIABLE DESCRIPTION
UID: 7925885
65
Q29) Write a program to accept a sentence which may be terminated by either '.', '?', or '!'
only. Any other character may be ignored. The words may be separated by more than one
blank space and are in UPPER CASE.
Perform the following tasks.
Accept the sentence and reduce all the extra blank space between two words to a single
blank space.
Accept a word from the user which is part of the sentence along with its position number
and delete the word and display the sentence.
Test your program with the sample data and some random data.
Example 1
INPUT: A MORNING WALK IS A IS BLESSING FOR THE WHOLE DAY.
WORD TO BE DELETED: IS
WORD POSITION IN THE SENTENCE: 6
Example 2
INPUT: AS YOU SOW, SO SO YOU REAP.
WORD TO BE DELETED: SO
WORD POSITION IN THE SENTENCE: 4
Example 3
INPUT: STUDY WELL ##.
OUTPUT: INVALID OUTPUT.
Step 1: Start
Step 2: import the util package for Scanner class
Step 3: Create the object of Scanner class
Step 4: input a sentence from the user
Step 5: check the validation of the sentence
Step 6: remove extra spaces by comparing the next character
Step 7: input the word and the index of the word to be deleted
Step 8: run a loop and skip the index entered by the user
Step 9: Print the relevant details
Step 10: End
} VARIABLE DESCRIPTION
UID: 7925885
67
Q30) A positive
natural number,
(for e.g.27) can be
represented as follows:
2+3+4+5+6+7
8+9+10
13+14
where every row represents a combination of consecutive natural numbers, which add up
to 27.
Write a program which inputs a positive natural number N and prints the possible
consecutive number combinations, which when added give N.
Test your program for the following data and some random data
SAMPLE DATA
INPUT N = 15
OUTPUT 7 8
12345
456
Step 1: Start
Step 2: import the util package for Scanner class
Step 3: Create the object of Scanner class
Step 4: input a number from the user
Step 5: check the validation of the number
Step 6: run a loop from one to the number to get numbers which can give the sum
Step 7: run a nested loop to obtain consecutive numbers
Step 8: if the sum of the number exceeds the original number break the loop
Step 9: if the sum is equal to the number print the string of numbers
Step 10: End
{
int s=0; //stores sum of numbers
String sp=""; //stores string of numbers adding up to n
for(int i=1;i<n;i++)
{
sp=""; //for adding integers to string
s=0; //consecutive numbers
for(int j=i;j<n;j++)
{
s+=j; //adding consecutive integers
sp=sp+j+" "; //adding integers in string
if(s>=n) //if sum exceeds of becomes same as number
break;
}
if(s==n) //if sum is equal to number
System.out.println(sp);
}
}
}
}
VARIABLE DESCRIPTION
UID: 7925885
69
ACKNOWLEDGEMENT
My success in this Java project wouldn't be possible without a
chorus of support. First, a heartfelt thanks to Pooja Ma'am for
her clear explanations, challenging assignments, and
insightful feedback. It's thanks to her that I navigated the
intricacies of Java with growing confidence. To my friends, I
owe their valuable input and fresh perspectives that helped
me tackle coding hurdles from different angles, honing my
problem-solving skills. And lastly, to my parents, whose
unwavering support and belief in me fuelled my motivation
throughout. This achievement is as much theirs as it is mine.
UID: 7925885
70