0% found this document useful (0 votes)
2 views

Computer Project_25 JAVA Programs

The document outlines a computer project by Harisankar Lal from Sarvodaya Vidyalaya, consisting of 25 Java programs categorized into number-based, array-based, and string-based questions. Each section includes examples, code implementations, and expected outputs for various mathematical concepts such as Fascinating Numbers, Circular Primes, Triangular Numbers, Bouncy Numbers, and Prime Palindromes. The project also acknowledges contributions from teachers, the principal, and family, emphasizing the educational support received during its completion.

Uploaded by

Harisankar Lal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Computer Project_25 JAVA Programs

The document outlines a computer project by Harisankar Lal from Sarvodaya Vidyalaya, consisting of 25 Java programs categorized into number-based, array-based, and string-based questions. Each section includes examples, code implementations, and expected outputs for various mathematical concepts such as Fascinating Numbers, Circular Primes, Triangular Numbers, Bouncy Numbers, and Prime Palindromes. The project also acknowledges contributions from teachers, the principal, and family, emphasizing the educational support received during its completion.

Uploaded by

Harisankar Lal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 130

SARVODAYA

VIDYALAYA
MAR IVANIOS VIDYA NAGAR, BETHANY HILLS

NALANCHIRA, TRIVANDRUM – 15

CERTIFIED BONAFIDE PROJECT


COMPUTER PROJECT
2024 – 2025

NAME : Harisankar Lal


CLASS : XII B
ROLL NO : 09
INDEX NO :
UNIQUE ID :

INTERNAL EXAMINER EXTERNAL EXAMINER

1|Page
Computer
Project

Submitted By:
Name : Harisankar Lal
Class : XII B
Roll no : 09

2|Page
Acknowledgement

I would like to express my special thanks of gratitude to my teacher Mrs. Sini


Mary Mathew, who gave me the golden opportunity to do this wonderful project
on 25 Java programs- 9 number-based, 8 array-based & 8 string-based.

I would also like to extend my gratitude to our Principal Prof. Dr. Shirley Stewart
for providing me with all the facility that was required.

Thirdly, I thank my parents and friends who treasured me for my hard work,
encouraged me, and helped me a lot in finalizing this project within the limited
time frame.

I am extremely grateful to all those who have contributed to all the information
that has come to use in this project. I would also like to thank all my supporters
who have motivated me to fulfil my project before the timeline.

Last, but not least, I would like to thank the almighty for making everything
possible for me till the end.

3|Page
Index
1. Number Based Questions
(i) Question 1 - Fascinating Number
(ii) Question 2 - Circular Prime
(iii) Question 3 - Triangular Number
(iv) Question 4 - Bouncy Number
(v) Question 5 - Prime Palindrome
(vi) Question 6 - Time
(vii) Question 7 - ISBN
(viii)Question 8 - Corresponding Day
(ix) Question 9 - Goldbach Number

2. Array Based Questions


(i) Question 10 - Octal Row
(ii) Question 11 - Fill Character
(iii) Question 12 - Circular Matrix
(iv) Question 13 - Saddle Point
(v) Question 14 - Quiz Key
(vi) Question 15 - Mirror Matrix
(vii) Question 16 - Non-Boundary & Diagonal
(viii)Question 17 - Shift Row

3. String Based Questions


(i) Question 18 - Alphabetical Order
(ii) Question 19 - Isogram
(iii) Question 20 - Palindrome String
(iv) Question 21 - Anagram
(v) Question 22 - Sorting Sentence
(vi) Question 23 - Pangram
(vii) Question 24 - Begin & End Vowel
(viii)Question 25 - Cipher Encryption

4|Page
Number
Based Questions

5|Page
Question 1 : Fascinating Number
Write a Program in Java to input a number and check whether it is a Fascinating
Number or not.

Fascinating Numbers: Some numbers of 3 digits or more exhibit a very interesting


property. The property is such that, when the number is multiplied by 2 and 3, and both
these products are concatenated with the original number, all digits from 1 to 9 are
present exactly once, regardless of the number of zeroes.

Let's understand the concept of Fascinating Number through the following example:
Consider the number 192
192 x 1 = 192
192 x 2 = 384
192 x 3 = 576
Concatenating the results: 192 384 576. It could be observed that '192384576' consists
of all digits from 1 to 9 exactly once. Hence, it could be concluded that 192 is a
Fascinating Number.

Some examples of fascinating Numbers are: 192, 219, 273, 327, 1902, 1920, 2019 etc.

6|Page
IPO Cycle

Input Process Output


• The number is multiplied by
1, 2 and 3.
• The products formed are
concatenated It is displayed whether the
A number is input • If the concatenated number entered number is
consists of all digits from 1 to fascinating or not
9 exactly once, the number is
Fascinating
• Else it is not.

7|Page
Code
import java.util.*;
public class Fascinating
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter a number to check: ");
int n=obj.nextInt();
int p1=n*1;
int p2=n*2;
int p3=n*3;
String s=Integer.toString(p1)+p2+p3;
System.out.println(s);
int k=0,l=s.length();
Outer:
for (int i=0; i<l; i++)
{
char ch=s.charAt(i);
int count=0;
for (int j=0; j<l; j++)
{
if (ch=='0')
continue;
else if (ch==s.charAt(j))
count++;
}
if (count>1)
{
k++;
break Outer;
}
}
if (k==0)
System.out.println(n+" is fascinating");
else
System.out.println(n+" is not fascinating");
}
}

8|Page
Output

Input:
Enter a number to check:
273
Output:
273546819
273 is fascinating

Input:
Enter a number to check:
341
Output:
3416821023
341 is not fascinating

9|Page
Question 2 : Circular Prime
Write a Program in Java to input a number and check whether it is a Circular Prime
or not.

Circular Prime: A Circular Prime is a prime number that remains prime under cyclic
shifts of its digits. When the leftmost digit is removed and replaced at the end of the
remaining string of digits, the generated number is still prime. The process is repeated
until the original number is reached again.
A number is said to be prime if it has only two factors I and itself.
Example:
131
311
113
Hence, 131 is a circular prime.

Accept a positive number N and check whether it is a circular prime or not. The new
numbers formed after the shifting of the digits should also be displayed.
Test your program with the following data and some random data:
Example 1
INPUT: N = 197
OUTPUT:
197 971 719
197 IS A CIRCULAR PRIME.

10 | P a g e
IPO Cycle

Input Process Output


• The leftmost digit is removed
and replaced at the end of the
remaining string of digits and
the resulting number is Displays a message if the
Enter a number to
checked if prime. entered number is a
check
Circular prime or not
• If all iterations result in prime
numbers, then the entered
number is a Circular Prime.

11 | P a g e
Code
import java.util.*;
public class Circular_prime
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the number to check: ");
int n=obj.nextInt();
int n1=n,l=0;
while (n1>0)//counting the no of digits
{
l+=1;
n1/=10;
}
int k=0;
String s=Integer.toString(n);
for (int i=0; i<l; i++)//rotating the digits and checking if each are prime
{
if (isPrime(Integer.valueOf(s)))
System.out.println(s);
else
{
k=1;
System.out.println(s);
}
s=s.substring(1,l)+s.charAt(0);
}
if (k==1)
System.out.println(n+" IS NOT A CIRCULAR PRIME");
else
System.out.println(n+" IS A CIRCULAR PRIME");
}
public static boolean isPrime(int n)//method to find if a number is prime or not
{
int k=0;
for (int i=1; i<=n/2; i++)
{
if (n%i==0)
k++;
}
if (k==1)
return true;
else
return false;
}
}

12 | P a g e
Output

Input:
Enter the number to check:
197
Output:
197
971
719
197 IS A CIRCULAR PRIME

Input:
Enter the number to check:
193
Output:
193
931
319
193 IS NOT A CIRCULAR PRIME

13 | P a g e
Question 3 : Triangular Number
Write a program in Java to display all the triangular numbers from 3 to n, taking the
value of n as an input.

A triangular number is formed by the addition of consecutive integers starting with 1.


For example,
1+2=3
1+2+3=6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 = 15

Thus, 3, 6, 10, 15, are triangular numbers.

14 | P a g e
IPO Cycle

Input Process Output


• A function ‘check’ is created
to check if a number is a
triangular number or not.
The value of n is Displays the triangular
entered • In the main method, a loop is numbers up to n
run up to n and finds the
triangular numbers up to n by
invoking the function ‘check’

15 | P a g e
Code
import java.util.*;
public class Triangular
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the value of n: ");
int n=obj.nextInt();//accepting n
if (n<=3)
{
System.out.println("n should be greater than 3");
System.exit(0);
}
String s="";
for (int i=3; i<=n; i++)
{
if(check(i))
s=s+Integer.toString(i)+",";//storing the triangular numbers
upto n
}
System.out.println("The triangular nos upto "+n+" are: "+s);
}
public static boolean check (int n)// method to check if a number is a
triangular number
{
int sum=0;
for (int i=1; i<n; i++)
{
sum+=i;
if (sum==n)
{
for (int j=1; j<=i; j++)
{
System.out.print(j+"+");
}
System.out.print("= "+sum+"\n");
return true;
}
}
return false;
}
}

16 | P a g e
Output

Input:
Enter the value of n:
15
Output:
1+2+= 3
1+2+3+= 6
1+2+3+4+= 10
1+2+3+4+5+= 15

The triangular nos upto 15 are: 3,6,10,15

Input:
Enter the value of n:
2
Output:
n should be greater than 3

17 | P a g e
Question 4 : Bouncy Number
Write a program in java to accept a number. Check and display whether it is a Bouncy
number or not.

Bouncy Numbers: A number is said to Bouncy number if the digits of the number are
unsorted.

For example,
22344 - It is not a Bouncy number because the digits are sorted in ascending order.
774410 - It is not a Bouncy number because the digits are sorted in descending order.
155349 - It is a Bouncy number because the digits are unsorted.
A number below 100 can never be a Bouncy number.

18 | P a g e
IPO Cycle

Input Process Output


• Two functions are defined to sort an
array into ascending and descending
orders respectively.
• Then store the resulting sorted digits
as a single number and returns it Displays whether
A number is • In the main method, the digits of the the number entered
entered number are extracted and stored in an is a Bouncy number
array or not
• The array is sorted and checked
whether equal to the entered number
• If equal, then it is not Bouncy number
• Else it is a Bouncy number

19 | P a g e
Code
import java.util.*;
public class Bouncy_no
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter a number to check: ");
int n=obj.nextInt();
int n1=n,n2=n, l=0;
if (n<100)//checking if number entered is less than 100
{
System.out.println("A number less than 100 can never be a Bouncy
number."
+"\n"+"TRY AGAIN");
System.exit(0);
}
while (n1>0)//counting the digits
{
l+=1;
n1/=10;
}
int a[]=new int[l];
for (int i=l-1; i>=0; i--)//storing the digits into an array
{
a[i]=n2%10;
n2/=10;
}
if (n== des_sort(a,l) || n==asc_sort(a,l))//checking if bouncy
number or not
{
System.out.println(n+" is not a Bouncy number");
}
else
System.out.println(n+" is a Bouncy number");
}
public static int des_sort(int a[], int l)//sorts the array in descending
order and stores the digits as a number
{
for (int i=0; i<l-1; i++)
{
for (int j=0; j<l-1-i; j++)
{
if (a[j]<a[j+1])

20 | P a g e
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
int m=0;
for (int i=0; i<l; i++)
m=m*10+a[i];
return m;
}
public static int asc_sort(int a[], int l)//sorts the array in ascending
order and stores the digits as a number
{
for (int i=0; i<l-1; i++)
{
for (int j=0; j<l-1-i; j++)
{
if (a[j]>a[j+1])
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}

int m=0;
for (int i=0; i<l; i++)
m=m*10+a[i];
return m;
}
}

21 | P a g e
Output

Input:
Enter a number to check:
22344
Output:
22344 is not a Bouncy number

Input:
Enter a number to check:
155349
Output:
155349 is a Bouncy number

Input:
Enter a number to check:
774410
Output:
774410 is not a Bouncy number

22 | P a g e
Question 5 : Prime 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>=100 and n<= 3000.
Display 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

Prime Palindrome: A prime palindrome integer is a positive integer (without leading


zeros) which is prime as well as a palindrome.
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

23 | P a g e
IPO Cycle

Input Process Output


• Two functions are defined to
check whether a number is a
palindrome or prime
Two positive
respectively
integers m and n Displays the prime
• Both these functions are
are entered where palindrome numbers from
m>=100 and n<= called in the main method m to n
where a loop is run from m to
3000
n and all prime-palindrome
numbers in that range are
found out

24 | P a g e
Code
import java.util.*;
public class Prime_palindrome
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter m and n: ");
int m=obj.nextInt();
int n=obj.nextInt();
if (m<=0 || n<=0)//checking conditions for m and n
{
System.out.println("m and n are to be positive.Try again");
System.exit(0);
}
else if (m>n)
{
System.out.println("m must be < or = n");
System.exit(0);
}
else if (m<100 || n>3000)
{
System.out.println("m must be >=100 & n must be <=3000");
System.exit(0);
}
System.out.println("The prime palindrome integers are: ");
int f=0;
for (int i=m; i<=n; i++)
{
if (isPalin(i) && isPrime(i))//finds the prime palindrome integers
between m and n
{
System.out.print(i+", ");
f++;
}
}
System.out.println("\n"+"The frequency of prime palindrome integers
is: "+f);
}
public static boolean isPalin(int n)//function to check whether a number
is palindrome or not
{
int n1,n2=0;
n1=n;

25 | P a g e
while (n1>0)
{
n2=n2*10+(n1%10);
n1/=10;
}
if (n==n2)
return true;
else
return false;
}
public static boolean isPrime(int n)//function to check whether a number
is prime or not
{
int k=0;
for (int i=1; i<=n/2; i++)
{
if (n%i==0)
k++;
}
if (k==1)
return true;
else
return false;
}
}

26 | P a g e
Output

Input:
Enter m and n:
100
1000
Output:
The prime palindrome integers are:
101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 797, 919, 929,
The frequency of prime palindrome integers is: 15

Input:
Enter m and n:
100
5000
Output:
m must be >=100 & n must be <=3000

27 | P a g e
Question 6 : Time
Give a time in numbers we can convert it into words.
For example, 5:00 five o' clock
5:10 ten minutes past five
5:15 QUARTER PAST FIVE
5:30 HALF PAST FIVE
5:40 twenty minutes to six
5:45 quarter to six
5:47 thirteen minutes to six

Write a program which first inputs two integers, the first between 1 and 12 (both
inclusive) and second between 0 and 59 (both inclusive) and then prints out the time
they represent, in words. Your program should follow the format of the examples
above.
SAMPLE DATA:
INPUT:
TIME: 3,0
OUTPUT: 3:00 three o' clock

INPUT:
TIME: 7,29
OUTPUT: 7:29 twenty-nine minutes past seven

INPUT:
TIME: 6,34
OUTPUT: 6:34 twenty-six minutes to seven

INPUT:
TIME: 12,1
OUTPUT: 12:01 one minute past twelve

INPUT:
TIME: 12,45
OUTPUT: 12:45 quarter to one

INPUT:
TIME: 10,59
OUTPUT: 10:59 one minute to eleven

28 | P a g e
INPUT:
TIME: 14:60
OUTPUT: incorrect input

Test your program for the data values given in the examples above and some random
data.

29 | P a g e
IPO Cycle

Input Process Output


• Two functions are defined.
• One to convert a given
number to its word form, and
the other to display the time
The hour and
in the format of hour:min.
minute to be Displays the time in the
displayed is • In the main method, the time format- hour:minutes
entered is entered and by checking
various conditions, the
accurate time is displayed by
calling the above defined
functions.

30 | P a g e
Code
import java.util.*;
public class time
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the time: (hour,min)");
int h=obj.nextInt();
int m=obj.nextInt();
if (h<1 || h>12)//checking conditions
{
System.out.println("Hour entered should be btw 1 and 12");
System.exit(0);
}
if (h<0 || h>59)
{
System.out.println("Minute entered should be btw 0 and 59");
System.exit(0);
}
System.out.println("The time entered: ");
disp(h,m);//displaying the time entered
if (m<=30)//displaying the word form by calling the method numwrd
{
if (m==15)
System.out.println("quarter past "+numwrd(h));
else if (m==30)
System.out.println("half past "+numwrd(h));
else if (m==0)
System.out.println(numwrd(h)+" o'clock");
else if (m==1)
System.out.println("one minute past "+numwrd(h));
else
System.out.println(numwrd(m)+" minutes past "+numwrd(h));
}
else
{
if (h==12)
{
if (m==45)
System.out.println("quarter to one");
else if (m==59)
System.out.println("one minute to one");
else

31 | P a g e
System.out.println(numwrd(60-m)+" minutes to one");
}
else
{
if (m==45)
System.out.println("quarter to "+numwrd(h+1));
else if (m==59)
System.out.println("one minute to "+numwrd(h+1));
else
System.out.println(numwrd(60-m)+" minutes to
"+numwrd(h+1));
}
}
}
public static void disp(int h, int m)//function to display the hours and
minutes in format
{
if (h<=9)
{
if (m<=9)
System.out.println("0"+h+":0"+m);
else
System.out.println("0"+h+":"+m);
}
else
{
if (m<=9)
System.out.println(h+":0"+m);
else
System.out.println(h+":"+m);
}
}
public static String numwrd(int n)//function to convert a number to its
word form
{
String
a[]={"one","two","three","four","five","six","seven","eight","nine","ten"
};
String b[]={"ten","twenty","thirty","fourty","fifty"};
String
c[]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventee
n","eighteen","nineteen"};
if (n>=1 && n<=10)
return a[n-1];

32 | P a g e
else if (n%10==0)
return b[n/10-1];
else if (n>=11 && n<=19)
return c[n%10-1];
else
return b[n/10-1]+" "+a[n%10-1];
}
}

33 | P a g e
Output

Sl No Input: Output:

Enter the time: (hour,min) The time entered:


1.
5 05:00
0 five o'clock

Enter the time: (hour,min) The time entered:


2.
5 05:10
10 ten minutes past five

Enter the time: (hour,min) The time entered:


3.
5 05:15
15 quarter past five

Enter the time: (hour,min) Incorrect input


4.
13
61

Enter the time: (hour,min) The time entered:


5.
5 05:30
30 half past five

Enter the time: (hour,min) The time entered:


6.
5 05:45
45 quarter to six

Enter the time: (hour,min) The time entered:


7.
5 05:47
47 13 minutes to six

34 | P a g e
Question 7 : ISBN
Write a Program in Java to check whether a number is an ISBN or not.

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 to 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 digit and so on until we add 1 time the last digit. If the final number
leaves no remainder while divided by 11, the code is a valid ISBN
For example:
0201103311-10*0+9*2+8*0+7*1+6*1+5*0+4*3+3*3+2*1+1*1=55
This is a valid ISBN

007462542X=10*0+9*0+8*7+7*4+6*6+5*2+4*5+3*4+2*2+1*10=176
This is a valid ISBN
Similarly, 0112112425 is not a valid ISBN.

Test Data:
Input code: 0201530821
Output: Sum=99
Leaves no remainder - valid ISBN

Input code: 356680324


Output: Sum invalid input

Input code: 0231428031


Output: Sum=122
Leaves remainder - invalid ISBN

35 | P a g e
IPO Cycle

Input Process Output


• Length condition is checked.
• The value of X is assigned, in
case such a letter is entered at
the end. It is displayed whether the
A 10-digit code is
• The sum is calculated based entered code is a valid
entered
on the given condition. ISBN or not
• If it is divisible by 11, the
code is an ISBN.
• Else it is not

36 | P a g e
Code
import java.util.*;
public class ISBN
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the code: (10 digit) ");
String n=obj.next();
int l=n.length();
if (l!=10)//checking length condition
{
System.out.println("Invalid. must be 10 digits");
System.exit(0);
}
int a[]=new int[10];
if (n.charAt(9)=='X')//assigning value of X
{
a[9]=10;
for (int i=0; i<9; i++)
a[i]=(int)n.charAt(i)-48;
}
else
{
for (int i=0; i<10; i++)
a[i]=(int)n.charAt(i)-48;
}
int k=10,sum=0;
for (int i=0; i<10; i++)
{
sum+=a[i]*k;
k--;
}
if (sum%11==0)//condition for ISBN
System.out.println(n+" is a valid ISBN");
else
System.out.println(n+" is not a valid ISBN");
}
}

37 | P a g e
Output
Input:
Enter the code: (10 digit)
0201103311
Output:
Sum= 55
Leaves no remainder- 0201103311 is a valid ISBN

Input:
Enter the code: (10 digit)
007462542X
Output:
Sum= 176
Leaves no remainder- 007462542X is a valid ISBN

Input:
Enter the code: (10 digit)
0201530821
Output:
Sum= 99
Leaves no remainder- 0201530821 is a valid ISBN

Input:
Enter the code: (10 digit)
356680324
Output:
Invalid. must be 10 digits

Input:
Enter the code: (10 digit)
0231428031
Output:
Sum= 122
Leaves remainder- 0231428031 is not a valid ISBN

38 | P a g e
Question 8 : Corresponding Day
Write a program to calculate and print the corresponding day of the year (in the range
1 to 366).

Input consists of day number (DD), the month of the day (MM) and the year (YYYY).

Example:
Input: Day 03
Month 05
Year 1996
Output: CORRESPONDING DAY OF THE YEAR IS: 124
(31+29 +31 +30 +3=124)

39 | P a g e
IPO Cycle

Input Process Output


• Input conditions are checked.
• An array to storing the days
of all months is created.
• The methods are created, one
to find out whether a year is a
The day, month leap year or not, and the other The corresponding day of
and year are input to count the digits of a the year is displayed
number.
• Based on whether it is a leap
year or not, sum of all days up
to the given day is calculated.

40 | P a g e
Code
import java.util.*;
public class day_yr
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the day, month and year: (DD/MM/YYYY)");
int d=obj.nextInt();
int m=obj.nextInt();
int y=obj.nextInt();
if (count(d)>2||d>31||count(m)>2||m>12||count(y)>4)//checking
conditions
{
System.out.println("INVALID INPUT");
System.exit(0);
}
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};//array to store
the days of all months
if (isLeap(y)==false)
{
if(d==29 && m==2)
{
System.out.println(y+" is not a leap yr. February contains only
28 days");
System.exit(0);
}
}

if (isLeap(y)==true)
{
if ((m==2)&&d>29)
{
System.out.println("The day entered exceeds the given month");
System.exit(0);
}
}
else if (d>a[m-1])
{
System.out.println("The day entered exceeds the given month");
System.exit(0);
}

int day=0;

41 | P a g e
if (m>2)//calculating day number
{
for (int i=0; i<m-1; i++)
day+=a[i];
day+=d;
if (isLeap(y))
day=day+1;
}
else if (m==2)
day=31+d;
else if (m==1)
day=d;

System.out.println("The corresponding day of the year is: "+day);


}
public static int count(int n)//method to count the no of digits of a
number
{
int n1=n,c=0;
while(n1>0)
{
c++;
n1/=10;
}
return c;
}
public static boolean isLeap(int n)//method to check if a year is leap
year or not
{
if (n%100==0)
{
if (n%400==0)
return true;
else
return false;
}
if (n%4==0)
return true;
return false;
}
}

42 | P a g e
Output

Input:
Enter the day, month and year: (DD/MM/YYYY)
3
5
1996
Output:
The corresponding day of the year is: 124

Input:
Enter the day, month and year: (DD/MM/YYYY)
29
2
2023
Output:
2023 is not a leap yr. February contains only 28 days

Input:
Enter the day, month and year: (DD/MM/YYYY)
32
4
2024
Output:
INVALID INPUT

Input:
Enter the day, month and year: (DD/MM/YYYY)
31
6
2023
Output:
The day entered exceeds the given month

43 | P a g e
Question 9 : Goldbach Number
A number is said to be a Goldbach number, if the number can be expressed as the
addition of two odd prime number pairs. If we follow the above condition, then we
can find that every even number larger than 4 is a Goldbach number because it must
have any pair of odd prime number pairs.
Example: 6 3,3 (ONE PAIR OF ODD PRIME)
103,7 and 5,5 (TWO PAIRS OF ODD PRIME)
Write a program to enter any positive EVEN natural number 'N' where (1<=N<=50)
and generate odd prime twin of 'N'. Test your program for the following data and
some random data.

Example 1
INPUT: N=14
OUTPUT: ODD PRIME PAIRS ARE: 3,11
7,7

Example 2
INPUT:
N=20
OUTPUT: ODD PRIME PAIRS ARE: 17,3
13,7

Example 3
INPUT: N=44
OUTPUT: ODD PRIME PAIRS ARE: 41,3
37,7
31, 13

Example 4
INPUT:
N=25
OUTPUT: INVALID INPUT

44 | P a g e
IPO Cycle

Input Process Output


• Input conditions are checked.
• Two methods are created, one
to check if a number is odd or
A positive even not and the other to check if a
natural number number is prime or not. The odd prime pairs are
(greater than 4) is • Goldbach condition is displayed
input checked by finding out if the
number can be expressed as
the addition of two odd prime
number pairs

45 | P a g e
Code
import java.util.*;
public class GoldBach
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter a +ve even natural no: (greater than 4)");
int n=obj.nextInt();
if (n<1 || n>50 || n%2==1)//checking input conditions
{
System.out.println("Invalid Input");
System.exit(0);
}
System.out.println("Odd prime pairs are: ");
int sum=0;
for (int i=n; i>=2; i--)
{
for (int j=n; j>=2; j--)
{
if (isOdd(i)&&isPrime(i)&&isOdd(j)&&isPrime(j))//checking condition for
Goldbach
sum=i+j;
if (sum==n && i>=j)
{
System.out.println("("+i+","+j+")");
}
sum=0;
}
}
}
public static boolean isOdd(int n)//checking if a number is odd or not
{
if (n%2==0)
return false;
else
return true;
}
public static boolean isPrime(int n)//checking if a number is prime or not
{
int c=0;
for (int i=2; i<=n/2; i++)
{
if (n%i==0)
c++;
}
if (c==0)
return true;
else
return false;
}
}

46 | P a g e
Output

Input:
Enter a +ve even natural no: (greater than 4)
14
Output:
Odd prime pairs are:
(11,3)
(7,7)

Input:
Enter a +ve even natural no: (greater than 4)
20
Output:
Odd prime pairs are:
(17,3)
(13,7)

Input:
Enter a +ve even natural no: (greater than 4)
44
Output:
Odd prime pairs are:
(41,3)
(37,7)
(31,13)

Input:
Enter a +ve even natural no: (greater than 4)
4
Output: Invalid Input

47 | P a g e
Array
Based Questions

48 | P a g e
Question 10 : Octal Row
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.

Example:
2 3 1 (decimal equivalent of 1st row = 153 i.e. 2x82 + 3x81 + 1x80)
4 0 5 (decimal equivalent of 2nd row = 261 i.e. 4x82 + 0x81 + 5x80)
1 5 6 (decimal equivalent of 3rd row = 110 i.e. 1x82 + 5x81 + 6x80)

Perform the following tasks on the matrix:


• Display the original matrix.
• Calculate the decimal equivalent for each row and display as per the format
given below.
• Test your program for the following data and some random data:

Example 1:
INPUT:
M=1
N=3
ENTER ELEMENTS FOR ROW 1: 1 4 4

OUTPUT:
FILLED MATRIX DECIMAL EQUIVALENT
1 4 4 100

49 | P a g e
IPO Cycle

Input Process Output


• A method is created to return
the decimal equivalent of the
octal number formed by
concatenating the digits of an
The no of rows and
array. The entered matrix and
columns of the
• Input conditions are checked. the decimal equivalent of
matrix is input.
Elements are • Using a nested for loop, the each row is are displayed
decimal equivalent of each side by side
accepted
row of the entered matrix is
found out by invoking the
above method and stored in a
separate 1D array

50 | P a g e
Code
import java.util.*;
public class Octal
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the value of M (btw 0 and 10) and N (btw 0
and 6): ");
int M=obj.nextInt();
int N=obj.nextInt();
if (M<=0 || M>=10 || N<=0 || N>=6)//checking input conditions
{
System.out.println("Out of Range");
System.exit(0);
}
int a[][]=new int[M][N];
int b[]=new int[N];
int c[]=new int[M];

for (int i=0; i<M; i++)//accepting elements


{
System.out.println("Enter the elements of row "+(i+1)+": ");
int p=0;
for (int j=0; j<N; j++)
{
a[i][j]=obj.nextInt();
b[p]=a[i][j];
p++;
}
c[i]=toDeci(b,N);//returns decimal equivalent of row i
}
System.out.println("Filled Matrix"+"\t"+"\t"+"Decimal Equivalent");
for (int i=0; i<M; i++)
{
for (int j=0; j<N; j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.print("\t"+"\t"+c[i]);
System.out.println();
}
}

51 | P a g e
public static int toDeci(int a[], int l)//method to return decimal
equivalent of corresponding row
{
int sum=0,k=0;
for (int i=l-1; i>=0; i--)
{
sum=sum+(a[i]*(int)Math.pow(8,k));
k++;
}
return sum;
}
}

52 | P a g e
Output

Input:
Enter the value of M (btw 0 and 10) and N (btw 0 and 6):
1
3
Enter the elements of row 1:
1
4
4
Output:
Filled Matrix Decimal Equivalent
1 4 4 100

Input:
Enter the value of M (btw 0 and 10) and N (btw 0 and 6):
4
4
Enter the elements of row 1:
1
2
3
4
Enter the elements of row 2:
5
6
7
8
Enter the elements of row 3:
9
10
11
12
Enter the elements of row 4:
13
14
15
16

53 | P a g e
Output:
Filled Matrix Decimal Equivalent
1 2 3 4 668
5 6 7 8 3008
9 10 11 12 5348
13 14 15 16 7688

Input:
Enter the value of M (btw 0 and 10) and N (btw 0 and 6):
11
3
Output:
Out of Range

Input:
Enter the value of M (btw 0 and 10) and N (btw 0 and 6):
3
7
Output: Out of Range

54 | P a g e
Question 11 : Fill Character
Write a program to declare a square matrix M[][] of order 'N' where 'N' must be
greater than 3 and less than 10. Allow the user to accept three different characters
from the keyboard and fill the array according to the instructions given below:

(i) Fill the four corners of the square matrix by character 1.


(ii) Fill the boundary elements of the matrix (except the four corners) by character 2.
(iii) Fill the non-boundary elements of the matrix by character 3.

Example:

INPUT: N = 4
FIRST CHARACTER: @
SECOND CHARACTER: ?
THIRD CHARACTER: #

OUTPUT:
@ ? ? @
? # # ?
? # # ?
@ ? ? @

55 | P a g e
IPO Cycle

Input Process Output


• Input condition is
checked.
The order of the square
• The matrix is filled
matrix is input. The
according to the The filled matrix is
first, second and third
conditions provided, displayed
characters to be filled
are entered. controlled using a nested
for loop

56 | P a g e
Code
import java.util.*;
public class mat_fillQ8
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the order of the square matrix: ");
int N=obj.nextInt();
if (N<=3 || N>=10)//checking input condition
{
System.out.println("it should be 3<N<10");
System.exit(0);
}
char M[][]=new char [N][N];
System.out.println("Enter 3 characters: ");//accepting characters
char c1=obj.next().charAt(0);
char c2=obj.next().charAt(0);
char c3=obj.next().charAt(0);
for (int i=0; i<N; i++)//filling the matrix
{
for (int j=0; j<N; j++)
{
if (i==0 || j==0 || i==N-1 || j==N-1)
M[i][j]=c2;
else
M[i][j]=c3;
}
}
M[0][0]=M[0][N-1]=M[N-1][0]=M[N-1][N-1]=c1;
System.out.println("The filled matrix is: ");//displaying the filled
matrix
for (int i=0; i<N; i++)
{
for (int j=0; j<N; j++)
{
System.out.print(M[i][j]+"\t");
}
System.out.println();
}
}
}

57 | P a g e
Output

Input:
Enter the order of the square matrix:
4
Enter 3 characters:
@
?
#
Output:
The filled matrix is:
@ ? ? @
? # # ?
? # # ?
@ ? ? @

Input:
Enter the order of the square matrix:
2
Output:
it should be 3<N<10

58 | P a g e
Question 12 : Circular Matrix
Write a program to fill in a two-dimensional array in a circular fashion with natural
numbers from 1 to N^2. given N as input.
Example:
If N=4, N^2=16
Then array will be:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7

59 | P a g e
IPO Cycle

Input Process Output


• Each row and column are
shifted by using 4 inner for
loops within an outer while
The order (N) of The filled circular matrix
loop.
the matrix is input is displayed
• The number up to (n2) are
generated and stored in a
circular order

60 | P a g e
Code
import java.util.*;
public class circular_Q9
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the value of N: ");//accepting the order
int N=obj.nextInt();
int a[][]=new int[N][N];
int x=0, y=N-1, p=0, q=N-1;
int w=1;
while (x<=y || w<=(N^2))//shifting and filling
{
for (int i=p; i<=q; i++)
a[x][i]=w++;
x++;
for (int i=x; i<=y; i++)
a[i][q]=w++;
q--;
for (int i=q; i>=p; i--)
a[y][i]=w++;
y--;
for (int i=y; i>=x; i--)
a[i][p]=w++;
p++;
}
for (int i=0; i<N; i++)//displaying the filled matrix
{
for (int j=0; j<N; j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}
}

61 | P a g e
Output

Input:
Enter the value of N:
4
Output:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7

Input:
Enter the value of N:
9
Output:
1 2 3 4 5 6 7 8 9
32 33 34 35 36 37 38 39 10
31 56 57 58 59 60 61 40 11
30 55 72 73 74 75 62 41 12
29 54 71 80 81 76 63 42 13
28 53 70 79 78 77 64 43 14
27 52 69 68 67 66 65 44 15
26 51 50 49 48 47 46 45 16
25 24 23 22 21 20 19 18 17

62 | P a g e
Question 13 : Saddle Point
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 the 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. If the matrix has no saddle point,
output the message "NO SADDLE POINT'.
(iii) Sort the elements along principal diagonal in ascending order using insertion sort
technique. All other elements should remain unchanged.
Test your program for the following data and some random data:

SAMPLE DATA:
INPUT: N = 4
MATRIX A[][] =
2 5 6 9
8 4 12 3
6 7 3 1
12 24 2 11

OUTPUT:
2 5 6 9
8 4 12 3
6 7 3 1
12 24 2 11
NO SADDLE POINT
MATRIX AFTER SORTING THE PRINCIPAL DIAGONAL
2 5 6 9
8 3 12 3
6 7 4 1
12 24 2 11

63 | P a g e
IPO Cycle

Input Process Output


• Input condition is checked.
• The saddle point is found out
by first finding out the
smallest element in each row
and finding its column
Entered matrix is
number.
displayed. It is then
The order of the • Then the corresponding
displayed whether the
matrix is input. column of the matrix is
matrix has got a saddle
Positive integers checked for the greatest
point or not. The matrix
are entered as element.
after sorting the main
elements • Now if the greatest element in diagonal elements is also
the column is equal to the displayed
smallest element found out,
then there is saddle point.
• The main diagonal elements
are then sorted using insertion
sort technique.

64 | P a g e
Code
import java.util.*;
public class SaddlePoint
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the order: ");
int N=obj.nextInt();
if (N>=20)//checking input condition
{
System.out.println("N should be less than 20");
System.exit(0);
}
int A[][]=new int[N][N];
System.out.println("Enter positive integers: ");
for (int i=0; i<N; i++)
{
for (int j=0; j<N; j++)
{
A[i][j]=obj.nextInt();
if (A[i][j]<0)//checking input condition
{
System.out.println("You should enter +ve integers only");
System.exit(0);
}
}
}

System.out.println("ENTERED MATRIX");//displaying entered matrix


for (int i=0; i<N; i++)
{
for (int j=0; j<N; j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}

int min=0,col=0,max=0;
boolean flag=false;
for (int i=0; i<N; i++)//finding saddle point
{
min=A[i][0];

65 | P a g e
for(int j=0;j<N;j++)
{
if(A[i][j]<min)
{
min=A[i][j];
col = j; // marking the coloumn of minimum element in ith
row
}
} // end of for loop-j
for(int k=0;k<N;k++)
{
if(A[k][col]>max)
max=A[k][col]; // finding maximum element in the
respective coloumn
} // end of for loop-k
if(max==min)
{ //checking for saddle point
System.out.println("Saddle Point="+max);
flag=true;
}
}//end of for loop-i

if(flag==false)
System.out.println("NO SADDLE POINT");

int b[]=new int[N];


for (int i=0; i<N; i++)
{
for (int j=0; j<N; j++)
{
if (i==j)
b[i]=A[i][j];
}
}
int key=0;//sorting main diagonal elements using insertion sort
for (int i=1; i<N; i++)
{
key=b[i];
int j=i-1;
while (j>=0 && b[j]>key)
{
b[j+1]=b[j];
j--;
}

66 | P a g e
b[j+1]=key;
}
for (int i=0; i<N; i++)
{
for (int j=0; j<N; j++)
{
if (i==j)
A[i][j]=b[i];
}
}
System.out.println("MATRIX AFTER SORTING THE PRINCIPAL DIAGONAL”);
for (int i=0; i<N; i++)
{
for (int j=0; j<N; j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
}

67 | P a g e
Output

Input:
Enter the order:
4
Enter positive integers:
2
5
6
9
8
4
12
3
6
7
3
1
12
24
2
11

Output:
ENTERED MATRIX
2 5 6 9
8 4 12 3
6 7 3 1
12 24 2 11
NO SADDLE POINT
MATRIX AFTER SORTING THE PRINCIPAL DIAGONAL
2 5 6 9
8 3 12 3
6 7 4 1
12 24 2 11

68 | P a g e
Input:
Enter the order:
4
Enter positive integers:
1
2
-5

Output:
You should enter +ve integers only

69 | P a g e
Question 14 : Quiz Key
The result of a quiz competition is to be prepared as follows:
The quiz has five questions with four multiple choices (A,B,C,D), with each question
carrying 1 mark for the correct answer. Design a program to accept the number of
participants n such that n must be greater than 3 and less than11. Create a double-
dimensional array of size (nx5) to store the answers of each participant row-wise.
Calculate the marks for each participant by matching the correct answer stored in a
single-dimensional array of size 5. Display the scores for each participant and also
the participant(s) having the highest score.

Example:
If the value of n=4, then the array would be:
Q1 Q2 Q3 Q4 Q5
Participant 1 A B B C A
Participant 2 D A D C B
Participant 3 A A B A C
Participant 4 D C C A B
Key to the question: D C C B A

Note: Array entries are line fed (i.e. one entry per line). Test your program for the
following data and some random data.
Example 1
INPUT: N = 5 OUTPUT:
Participant I D A B C C Scores:
Participant 2 A A D C B Participant 1 = 0
Participant 3 B A C D B Participant 2 = 1
Participant 4 D A D C B Participant 3 = 1
Participant 5 B C A D D Participant 4 = 1
Key:BCDAA Participant 5 = 2
Highest Score: Participant 5

70 | P a g e
IPO Cycle

Input Process Output


• The choices entered for
each participant is
displayed.
• The answer key is also
The no of participants • Input condition is checked. displayed.
is input. The choices • The scores of each participant • The participants along
of each participant are are calculated by comparing with their respective
input. The key is also their choices with the key scores are displayed.
input entered. • The highest scoring
participant is also
displayed

71 | P a g e
Code
import java.util.*;
public class QuizQ7
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the no of participants: ");
int n=obj.nextInt();
if (n<=3 || n>=11)//checking input condition
{
System.out.println("no of participants must be greater than 3 and
less than 11");
System.exit(0);
}
char a[][]=new char[n][5];
System.out.println("Enter the choices for: ");//accepting the
answers of each participant
for (int i=0; i<n; i++)
{
System.out.println("Participant "+(i+1)+"\t");
for (int j=0; j<5; j++)
{
a[i][j]=obj.next().charAt(0);
}
}
char key[]=new char[5];
System.out.println("Enter the answer key: ");//accepting answer key
for (int i=0; i<5; i++)
key[i]=obj.next().charAt(0);

for (int i=0; i<n; i++)//displaying the choices entered


{
System.out.print("Participant "+(i+1)+"\t");
for (int j=0; j<5; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.print("Key: "+"\t");
for (int i=0; i<5; i++)//displaying key
System.out.print(key[i]+" ");

72 | P a g e
int scores[]=new int[n];int m=0;
for (int i=0; i<n; i++)//calculating scores
{
for (int j=0; j<5; j++)
{
if (a[i][j]==key[j])
m++;
}
scores[i]=m;
m=0;
}
System.out.println("\n"+"Scores: ");
int pos=0,max=0;
for (int i=0; i<n; i++)//print participants along with their
respective scores
{
System.out.println("Participant "+(i+1)+"\t"+scores[i]);
if (scores[i]>max)
{
max=scores[i];
pos=i;
}
}
System.out.println("The highest score: "+"\n"+"Participant
"+(pos+1));//displaying participant with highest score
}
}

73 | P a g e
Output

Input:
Enter the no of participants:
5

Enter the choices for:


Participant 1
D
A
B
C
C

Participant 2
A
A
D
C
B

Participant 3
B
A
C
D
B

Participant 4
D
A
D
C
B

Participant 5
B
C
A
D

74 | P a g e
D

Enter the answer key:


B
C
D
A
A

Output:
Participant 1 DABC C
Participant 2 AA D C B
Participant 3 BACD B
Participant 4 DAD CB
Participant 5 BCAD D

Key: B C D AA

Scores:

Participant 1 0
Participant 2 1
Participant 3 1
Participant 4 1
Participant 5 2

The highest score:


Participant 5

75 | P a g e
Question 15 : Mirror Matrix
Write a program to declare a square matrix AfIlj of order (MXN) where `M' is the
number of rows and the number of columns. 'NI' should be greater than 2 and less
than 20. Allow user to enter integers into this matrix. Display appropriate error
message for an invalid input.
Perform the following tasks.
1. Display the input matrix
2. Create a mirror image of the inputted matrix.
3. Display the mirror image matrix

Test Data:
Input: M=3
4 16 12
8 2 14
6 1 3
Output:
Original matrix
4 16 12
8 2 14
6 1 3
Mirror image matrix:
12 16 4
14 2 8
3 1 6

76 | P a g e
IPO Cycle

Input Process Output


Order of square matrix
• A loop is run to find the The original and mirrored
is input. The elements
mirror matrix. matrices are displayed
are entered

77 | P a g e
Code
import java.util.*;
public class Mirror_img
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the order: ");
int m=obj.nextInt();
if (m<=2 || m>=20)//checking input condition
{
System.out.println("size OUT OF RANGE");
System.exit(0);
}
int a[][]=new int[m][m];
System.out.println("Enter the elements: ");//accepting elements
for (int i=0; i<m; i++)
{
for (int j=0; j<m; j++)
a[i][j]=obj.nextInt();
}
System.out.println("Original matrix: ");//displaying original matrix
for (int i=0; i<m; i++)
{
for (int j=0; j<m; j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
int b[][]=new int[m][m];

for (int i=0; i<m; i++)//finding mirror matrix


{
for (int j=0; j<m; j++)
b[i][j]=a[i][m-1-j];
}
System.out.println("Mirror image matrix: ");//displaying mirror matrix
for (int i=0; i<m; i++)
{
for (int j=0; j<m; j++)
System.out.print(b[i][j]+"\t");
System.out.println();
}
}
}

78 | P a g e
Output

Input:
Enter the order:
3
Enter the elements:
4
16
12
8
2
14
6
1
3
Output:
Original matrix:
4 16 12
8 2 14
6 1 3
Mirror image matrix:
12 16 4
14 2 8
3 1 6

Input:
Enter the order:
2
Output:
size OUT OF RANGE

79 | P a g e
Question 16 : Non-Boundary & Diagonal
Write a program to declare a square matrix A[][] of order (MM) where 'M' must be
greater than 3 and less than 10. Allow the user to input positive integers into this
matrix. Perform the following tasks on the matrix:
1. Sort the non-boundary elements in ascending order using any standard sorting
technique and rearrange them in the matrix.
2. Calculate the sum of both the diagonals.
3. Display the original matrix, rearranged matrix and only the diagonal elements of
the rearranged matrix with their sum.
Test your program for the following data and some random data:

Example 1
INPUT: M=4 DIAGONAL ELEMENTS
9 2 1 5
9 5
8 13 8 4
3 6
15 6 3 11
8 13
7 12 23 8
7 8
SUM OF THE DIAGONAL
OUTPUT: ELEMENTS = 59
ORIGINAL MATRIX
9 2 1 5
8 13 8 4
15 6 3 11 Example 2
7 12 23 8 INPUT:
M=3

REARRANGED MATRIX 9215


OUTPUT:
9 2 1 5
THE MATRIX SIZE IS OUT OF
8 3 6 4 RANGE.
15 8 13 11
7 12 23 8

80 | P a g e
IPO Cycle

Input Process Output


• Input condition is checked.
The original matrix is
• The non boundary elements
displayed. The rearranged
are stored in an array.
matrix is displayed
• The array is sorted in
Order of the matrix followed by the diagonal
is input. Elements ascending order and then elements alone, leaving
filled back into the original
are entered blank spaces for the other
matrix.
elements. Finally, the sum
• Sum of diagonal elements is of diagonal elements is
also calculated. displayed

81 | P a g e
Code
import java.util.*;
public class nb_diagQ6
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the order: ");
int m=obj.nextInt();

if (m<=3 || m>=10)//checking input condition


{
System.out.println("size OUT OF RANGE");
System.exit(0);
}
int a[][]=new int[m][m];
System.out.println("Enter the elements: ");//accepting elements
for (int i=0; i<m; i++)
{
for (int j=0; j<m; j++)
a[i][j]=obj.nextInt();
}
System.out.println("ORIGINAL MATRIX: ");
display(a,m);

int b[]=new int[(m-1)*(m-1)];


int k=0;
for (int i=1; i<m-1; i++)
{
for (int j=1; j<m-1; j++)
{
b[k]=a[i][j];
k++;
}
}
int key=0;//sorting non boundary elements in ascending order using
insertion sort
for (int i=1; i<m; i++)
{
key=b[i];
int j=i-1;
while (j>=0 && b[j]>key)
{
b[j+1]=b[j];

82 | P a g e
j--;
}
b[j+1]=key;
}
k=0;
for (int i=1; i<m-1; i++)
{
for (int j=1; j<m-1; j++)
{
a[i][j]=b[k];;
k++;
}
}
System.out.println("REARRANGED MATRIX: ");
display(a,m);
System.out.println("\n"+"DIAGONAL ELEMENTS: ");
int sum=0;
for (int i=0; i<m; i++)//printing diagonal elements and finding their sum
{
for (int j=0; j<m; j++)
{
if ((i==j)||(i+j==m-1))
{
System.out.print(a[i][j]+"\t");
sum+=a[i][j];
}
else
System.out.print(" "+"\t");
}
System.out.println();
}
System.out.println("SUM OF DIAGONAL ELEMENTS: "+sum);
}
public static void display(int a[][], int m)//function to display matrix
{
for (int i=0; i<m; i++)
{
for (int j=0; j<m; j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
}
}

83 | P a g e
Output

Input: Output:
Enter the order: ORIGINAL MATRIX:
4
9 2 1 5
Enter the elements: 8 13 8 4
9
2 15 6 3 11
1 7 12 23 8
5
8
13 REARRANGED MATRIX:
8
4 9 2 1 5
15 8 3 6 4
6
3 15 8 13 11
11 7 12 23 8
7
12
23 DIAGONAL ELEMENTS:
8
9 5
3 6
8 13
7 8
SUM OF DIAGONAL ELEMENTS: 59

84 | P a g e
Question 17 : Shift Row
Write a program to declare a matrix A[] 1 of order (M X N) where 'M' is the number
of rows and 'N' is the number of columns such that both M and N must be greater
than 2 and less than 10. Allow the user to input integers into this matrix. Display
appropriate error message for an invalid input. Perform the following tasks on the
matrix.
a) Display the input matrix
b) Shift each row one step upwards so the first row will become the last row, the
second row will be the first row and so on
c) Display the rotated matrix along with the highest element and its location in the
matrix
Test your program for the following data and some random data:

Example 1
INPUT: M =3 N = 4
Enter elements in the matrix:
100 90 87 76
200 500 167 998
77 567 89 254

OUTPUT: FORMED MATRIX AFTER ROTATING:


200 500 167 998
77 567 89 254
100 90 87 76

Highest element: 998 (Row: 0 and Column: 3)

85 | P a g e
IPO Cycle

Input Process Output


• Input condition is checked.
• Each row is shifted one step
The no of rows and The original and shifted
upwards so that the first row
columns of the matrices are displayed.
will become last row, the
matrix are entered. The highest element and
second row will be the first
Elements are its position are also
row and so on.
accepted displayed
• The highest element is found
out along with its position.

86 | P a g e
Code
import java.util.*;
public class shiftmat_Q2
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the no of rows and columns: ");
int m=obj.nextInt();
int n=obj.nextInt();

if ((m<=2 || m>=10)||(n<=2 || n>=10))//checking input condition


{
System.out.println("size OUT OF RANGE");
System.exit(0);
}
int a[][]=new int[m][n];
System.out.println("Enter the elements: ");//accepting the elements
for (int i=0; i<m; i++)
{
for (int j=0; j<n; j++)
a[i][j]=obj.nextInt();
}
System.out.println("Original matrix: ");//displaying original matrix
for (int i=0; i<m; i++)
{
for (int j=0; j<n; j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
int b[][]=new int[m][n];
int h=0,r=0,c=0;
for (int i=0; i<m; i++)//rotating the matrix
{
for (int j=0; j<n; j++)
{
if (i==m-1)
b[i][j]=a[0][j];
else
b[i][j]=a[i+1][j];

if (a[i][j]>h)
{
h=a[i][j];

87 | P a g e
r=i;
c=j;
}
}
}
System.out.println("The Rotated matrix: ");//displaying the rotated
matrix
for (int i=0; i<m; i++)
{
for (int j=0; j<n; j++)
System.out.print(b[i][j]+"\t");
System.out.println();
}
System.out.println("Highest element: "+h+" (Row: "+r+" and
Column: "+c+")");//displaying the highest element along with its position
}
}

88 | P a g e
Output
Input:
Enter the no of rows and columns:
3
4
Enter the elements:
100
90
87
76
200
500
167
998
77
567
89
254
Output:
Original matrix:
100 90 87 76
200 500 167 998
77 567 89 254

The Rotated matrix:


200 500 167 998
77 567 89 254
100 90 87 76

Highest element: 998 (Row: 1 and Column: 3)

Input:
Enter the no of rows and columns:
3
11
Output:
size OUT OF RANGE
SUM OF DIAGONAL ELEMENTS: 59

89 | P a g e
String
Based Questions

90 | P a g e
Question 18 : Alphabetical Order
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

91 | P a g e
IPO Cycle

Input Process Output


• Terminating character is
checked.
• The no of words is found out.
The no of words(length)
• Words are extracted and
A sentence is input and the sorted sentence
stored in an array. are displayed
• A method is defined to sort
the array based on the
alphabetical order of words.

92 | P a g e
Code
import java.util.*;
public class q3_wordalphaorder
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter a sentence: ");
String s=obj.nextLine();
int l=s.length();
char ch=s.charAt(l-1);
if (ch=='.'|| ch=='!'|| ch=='?')//checking terminating character
System.out.println("");
else
{
System.out.println("The sentence should be terminated by '.','!'
or '?'");
System.exit(0);
}
s=s.toUpperCase();
s=" "+s;int k=0;
for (int i=0; i<l; i++)//finding no of words
{
if (s.charAt(i)==' ')
k++;
}
String a[]=new String[k];
s=s.trim();
System.out.println("Length: "+k);
System.out.println("Rearranged sentence: ");
k=0; String wrd="";
for (int i=0; i<l; i++)//extracting words
{
char ch1=s.charAt(i);
if (ch1==' '||ch1=='.'||ch1=='!'||ch1=='?')
{
a[k]=wrd;
k++;
wrd="";
}
else
wrd+=ch1;
}
sort(a,k);

93 | P a g e
}
public static void sort(String a[], int l)//method to sort a string array
{
for (int i=0; i<l-1; i++)
{
for (int j=0; j<l-1-i; j++)
{
if (a[j].compareTo(a[j+1])>0)
{
String t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for (int i=0; i<l; i++)
{
System.out.print(a[i]+" ");
}
}
}

94 | P a g e
Output

Input:
Enter a sentence:
NECESSITY IS THE MOTHER OF INVENTION.

Output:
Length: 6
Rearranged sentence:
INVENTION IS MOTHER NECESSITY OF THE

Input:
Enter a sentence:
OM NAMAH SHIVAYA

Output:
The sentence should be terminated by ‘.’, ‘!’ or '?'

95 | P a g e
Question 19 : Isogram
Given a word or phrase, check if it is an isogram or not. An isogram is a word in
which no letter occurs more than once.

Examples:
Input: Machine
Output: True

Input: Geek
Output: False

96 | P a g e
IPO Cycle

Input Process Output


• A nested loop is run to check
if any letter is repeating or not
True or False is displayed
in the word.
A word is entered based on whether the
• If repeating, then it is not an
word is an isogram or not
isogram.
• Else it is an isogram.

97 | P a g e
Code
import java.util.*;
public class Isogram
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter a word to check: ");
String w=obj.next();//accepting word
int l=w.length();
for (int i=0; i<l-1; i++)//checking if any letter is repeating
{
for (int j=i+1; j<l; j++)
{
if (w.charAt(i)==w.charAt(j))
{
System.out.println("False");
System.exit(0);
}
}
}
System.out.println("True");
}
}

98 | P a g e
Output

Input:
Enter a word to check:
Machine
Output:
True

Input:
Enter a word to check:
Geek
Output:
False

99 | P a g e
Question 20 : Palindrome String

A palindrome is a word that may be read the same in either direction. Accept a sentence
in upper case which is terminated by either '.', "?', '!'. Each word in the sentence is
separated by a blank space.
Perform the following tasks:
Test Data:
Input: MOM AND DAD ARE COMING AT NOON.
Output: MOM DAD NOON
Number of palindromic words: 3

Input: HOW ARE YOU?


Output: No palindromic words

100 | P a g e
IPO Cycle

Input Process Output


• Terminating character is
checked.
• A method is defined to check
if a word is palindrome or not
The number of palindrome
• Each word is extracted and is
A sentence is input words in the entered
checked if palindrome or not
sentence is displayed
by invoking the above
method.
• The no of palindrome words
is counted.

101 | P a g e
Code
import java.util.*;
public class palindrome_wrd
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter a sentence: ");
String s=obj.nextLine();//accepting sentence
int l=s.length();
char ch=s.charAt(l-1);
if (ch=='.'|| ch=='!'|| ch=='?'|| ch==',')//checking terminating
condition
System.out.println("");
else
{
System.out.println("The sentence should be terminated by
'.','!',',' or '?'");
System.exit(0);
}
s=s.toUpperCase();
String wrd="";
int k=0;
for (int i=0; i<l; i++)//extracting each word and checking for
palindrome words
{
char ch1=s.charAt(i);
if (ch1==' '||ch1=='.'||ch1=='!'||ch1=='?')
{
if (isPalin(wrd))
k++;
wrd="";
}
else
{
wrd+=ch1;
}
}
if (k==0)
System.out.println("No palindrome words");
else
System.out.println("\n"+"Number of palindrome words: "+k);

102 | P a g e
public static boolean isPalin(String s)//method to check if a word is
palindrome or not
{
int l=s.length();
String s1="";
for (int i=l-1; i>=0; i--)
s1+=s.charAt(i);
if (s1.equals(s))
{
System.out.print(s+" ");
return true;
}
else
return false;
}
}

103 | P a g e
Output

Input:
Enter a sentence:
MOM AND DAD ARE COMING AT NOON.

Output:
MOM DAD NOON
Number of palindrome words: 3

Input:
Enter a sentence:
HOW ARE YOU?

Output:
No palindrome words

Input:
Enter a sentence:
Are you a Sanju fan

Output:
The sentence should be terminated by ‘.’, ‘!’,',' or '?'

104 | P a g e
Question 21 : Anagram
Given two strings str1 and str2 consisting of lowercase characters, the task is to check
whether the two given strings are anagrams of each other or not.

An anagram of a string is another string that contains the same characters, only the order
of characters can be different. For example, "act and "tac" are anagrams of each other.

Examples:
Input: strl = "listen" str2="silent"
Output: "Anagram"

Explanation: All characters of "listen" and "silent" are the same.

Input: str1 = "gram" str2 = "arm"


Output: "Not Anagram"

105 | P a g e
IPO Cycle

Input Process Output


• A method is created to sort
the characters of a string in
ascending order.
• Both entered strings are
It is displayed whether
Two words are sorted.
both strings are anagrams
provided as input • They are then checked for
of each other or not
equality.
• If equal, then they are
anagrams of each other.
• Else they are not.

106 | P a g e
Code
import java.util.*;
public class AnagramQ6
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter two strings to check: ");
String str1=obj.next();
String str2=obj.next();
String s1=Sortstr(str1);
String s2=Sortstr(str2);
if(s1.equals(s2))//checking if both sorted strings are equal
System.out.println("Anagram");
else
System.out.println("Not Anagram");
}
public static String Sortstr(String s)//method to sort the characters of a
string in ascending order
{
char a[]=new char[26];
for (int i=65; i<=90; i++)
a[i-65]=(char)i;

s=s.toUpperCase();
String sn="";
int l=s.length();
for (int i=0; i<26; i++)
{
for (int j=0; j<l; j++)
{
char c=s.charAt(j);
if (a[i]==c)
sn=sn+c;
}
}
return sn;
}
}

107 | P a g e
Output

Input:
Enter two strings to check:
listen
silent

Output:
Anagram

Input:
Enter two strings to check:
gram
arm

Output:
Not Anagram

108 | P a g e
Question 22 : Sorting Sentence
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 with the sample 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
Example 2:
INPUT:
SELF HELP IS THE BEST HELP.

OUTPUT:
SELF HELP IS THE BEST HELP.
IS THE BEST HELP HELP SELF
Example 3:
INPUT:
BE KIND TO OTHERS.

OUTPUT:
BE KIND TO OTHERS.
BE TO KIND OTHERS
Example 4:
INPUT:
NOTHING IS IMPOSSIBLE#

OUTPUT:
INVALID INPUT

109 | P a g e
IPO Cycle

Input Process Output


• Terminating character is
checked.
• The number of words is
counted.
• Words are extracted and
stored in an array.
A sentence is given • A method is defined to sort a The original sentence and
as input. string array based on length of the sorted sentence are
words, in which, if two words displayed
have same length, they are
sorted alphabetically.
• Another array is created
having the same length, into
which the sorted words are
stored based on length.

110 | P a g e
Code
import java.util.*;
public class sort_sent
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter a sentence: ");
String sen=obj.nextLine();
int l=sen.length();
char c=sen.charAt(l-1);
if (c=='.'||c=='?'||c=='!')//checking terminating character
System.out.println();
else
{
System.out.println("The sentence must be terminated by either
'.', '?' or '!' only");
System.exit(0);
}
sen=sen.toUpperCase();
sen=" "+sen;
int count=0,g=0;
String wrd="";
for (int i=0; i<l+1; i++)//counting the no of words
{
char ch=sen.charAt(i);
if (ch==' ')
count++;
}
String q[]=new String[count];
sen=sen.trim();
for (int i=0; i<l; i++)//extracting words and storing them
{
char ch=sen.charAt(i);
if (ch==' '||ch=='.'||ch=='?'||ch=='!')
{
q[g]=wrd;
wrd="";
g++;
}

else
wrd+=ch;
}

111 | P a g e
System.out.println("\n"+"The original sentence is: ");
for (int i=0; i<count; i++)
{
System.out.print(q[i]+" ");
}
String b[]=sort(q,count);//storing each word of entered sentence
into an array b

System.out.println("\n"+"The sorted sentence is: ");


for (int i=0; i<count; i++)
{
System.out.print(b[i]+" ");
}
}
public static String[] sort(String s[], int c)//method to sort a string
array based on length of words
{
int l=0;
for (int i=0; i<c-1; i++)
{
for (int j=0; j<c-1-i; j++)
{
if (s[j].length()>s[j+1].length())
{
String temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
else if (s[j].length()==s[j+1].length())//if two words have
same length, they are sorted alphabetically
{
if (s[j].compareTo(s[j+1])>0)
{
String temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
}
return s;
}
}

112 | P a g e
Output

Sl No Input: Output:

Enter a sentence: The original sentence is:


1. AS YOU SOW SO SHALL AS YOU SOW SO SHALL YOU REAP
YOU REAP. The sorted sentence is:
AS SO SOW YOU YOU REAP SHALL

Enter a sentence: The original sentence is:


2. SELF HELP IS THE BEST SELF HELP IS THE BEST HELP
HELP The sorted sentence is:
IS THE BEST HELP HELP SELF

Enter a sentence: The original sentence is:


BE KIND TO OTHERS. BE KIND TO OTHERS
3.
The sorted sentence is:
BE TO KIND OTHERS

Enter a sentence: The sentence must be terminated by either


4.
NOTHING IS IMPOSSIBLE# '.', '?' or '!' only

113 | P a g e
Question 23 : Pangram
Write a program to accept a sentence which may be terminated by either ‘.’, ‘?’ or
‘!’ only. The words may be separated by a single blank space and should be case-
insensitive.
Perform the following tasks:
(a) Determine if the accepted sentence is a Pangram or not.
[A Pangram is a sentence that contains every letter of the alphabet at least once.]
Example: "The quick brown fox jumps over the lazy dog"

(b) Display the first occurring longest and shortest word in the accepted sentence.
Test your program for the following data and some random data:

Example 1
INPUT: Pack my box with five dozen liquor jugs.

OUTPUT: IT IS A PANGRAM
LONGEST WORD: liquor
SHORTEST WORD: my

Example 2
INPUT: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.

OUTPUT: IT IS A PANGRAM
LONGEST WORD: QUICK
SHORTEST WORD: THE

Example 3
INPUT:
Hello my World.

OUTPUT: IT IS NOT A PANGRAM


LONGEST WORD: Hello
SHORTEST WORD: my

Example 4
INPUT: Alas! it failed #
OUTPUT: INVALID INPUT

114 | P a g e
IPO Cycle

Input Process Output


• Terminating character is
checked.
It is displayed whether the
• Longest and shortest words of
entered sentence is a
the sentence are found.
pangram or not. The
A sentence is input • It is checked whether the longest and shortest words
sentence is a pangram or not in the sentence are also
by running a loop and finding displayed
out if all alphabets are present
or not.

115 | P a g e
Code
import java.util.*;
public class Pangram
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter a sentence: ");//accepting sentence
String s=obj.nextLine();
int l=s.length();
char c=s.charAt(l-1);
if (c=='.'||c=='?'||c=='!')//checking terminating character
System.out.println();
else
{
System.out.println("INVALID INPUT");
System.exit(0);
}
String wrd="",lon="",sho=s;
for (int i=0; i<l; i++)//finding the longest and shortest word
{
char ch=s.charAt(i);
if (ch==' '||ch=='.'||ch=='?'||ch=='!')
{
if (wrd.length()>lon.length())
lon=wrd;
if (wrd.length()<sho.length())
sho=wrd;
wrd="";
}
else
wrd+=ch;
}
int k=0;
Outer:
for (int i=65; i<=90; i++)//finding out if sentence is Pangram or not
{
k=0;
char ch=(char)i;
for (int j=0; j<l; j++)
{
char cs=s.charAt(j);
if (ch==Character.toUpperCase(cs))
k++;

116 | P a g e
}
if (k==0)
{
System.out.println("IT IS NOT A PANGRAM");
break Outer;
}
}
if (k>0)
System.out.println("IT IS A PANGRAM");
System.out.println("LONGEST WORD: "+lon);
System.out.println("SHORTEST WORD: "+sho);
}
}

117 | P a g e
Output

Input:
Enter a sentence:
Pack my box with five dozen liquor jugs.
Output:
IT IS A PANGRAM
LONGEST WORD: liquor
SHORTEST WORD: my

Input:
Enter a sentence:
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
Output:
IT IS A PANGRAM
LONGEST WORD: QUICK
SHORTEST WORD: THE

Input:
Enter a sentence:
Hello my World.
Output:
IT IS NOT A PANGRAM
LONGEST WORD: Hello
SHORTEST WORD: my

Input:
Enter a sentence:
Alas! it failed #
Output:
INVALID INPUT

118 | P a g e
Question 24 : Begin & End Vowel
Write a program to accept a sentence which may be terminated by either '.', '?' or '!'
only. The words may be separated by more than one blank space and are in UPPER
CASE.
Perform the following tasks:
1. Fing the number of words beginning and ending with a vowel.
2. Place the words which begin and end with a vowel at the beginning, followed
by the remaining words as they occur in the sentence.

Test your program with the sample data and some random data:

Example 1
INPUT:
ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.

OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL

119 | P a g e
IPO Cycle

Input Process Output


• Terminating character is
checked.
• Number of words are counted.
Words are extracted and
stored in an array.
• Two methods are created, one
No of words beginning
to check whether a character
and ending with a vowel
A sentence is input is a vowel or not, and the
is displayed. The replaced
other to check whether a word sentence is also displayed
begins and ends with a vowel.
• A new array is declared, into
which the words beginning
and ending with vowels are
stored first, followed by the
rest of the words.

120 | P a g e
Code
import java.util.*;
public class Question5
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter a sentence: ");
String s=obj.nextLine();
int l=s.length();
char ch=s.charAt(l-1);
if (ch=='?'||ch=='.'||ch=='!')//checking terminating character
System.out.println();
else
{
System.out.println("The sentence must end with '?','.'or'!'");
System.exit(0);
}
s=" "+s;int c=0;
for (int i=0; i<=l; i++)
{
if (s.charAt(i)==' ')
c++;
}
s=s.trim();
String a[]=new String[c];
String wrd=""; int k=0;
for (int i=0; i<l; i++)//extracting words and storing them in an array
{
char c1=s.charAt(i);
if (c1==' '||c1=='?'||c1=='.'||c1=='!')
{
a[k]=wrd;
wrd="";
k++;
}
else
wrd+=c1;
}
String b[]=new String[c];
int count=0;k=0;
for (int i=0; i<c; i++)//counting the no of words beginning and
ending with a vowel
{

121 | P a g e
if (isBegEndvow(a[i]))
count++;
}
int q=count;
for (int i=0; i<c; i++)//replacing the sentence
{
if (isBegEndvow(a[i]))
{
b[k]=a[i];
k++;
}
else
{
b[q]=a[i];
q++;
}
}
System.out.println("The number of words beginning and ending with a
vowel are: "+count);
System.out.println("The replaced sentence is: ");
for (int i=0; i<c; i++)
System.out.print(b[i]+" ");
}
public static boolean isBegEndvow(String s)//method to check if a word
is beginning and ending with a vowel
{
int l=s.length();
char c=s.charAt(0);
char d=s.charAt(l-1);
if(isVow(c) && isVow(d))
return true;
else
return false;
}
public static boolean isVow(char ch)//method to check if a character is a
vowel or not
{
char c=Character.toUpperCase(ch);
if (c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
return true;
else
return false;
}
}

122 | P a g e
Output

Input:
Enter a sentence:
ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.

Output:
The number of words beginning and ending with a vowel are: 3
The replaced sentence is:
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL

123 | P a g e
Question 25 : Cipher Encryption
The computer department of the Agency of International Espionage is trying to
decode intercepted messages. The agency's spies have determined that the enemy
encodes messages by accepting a keyword and then arranging the alphabets starting
from A till Z after the keyword till the end. The repeated letters of the keyword are
removed, and the rest of the alphabets are filled without including those in the
keyword.
The alphabets from A to Z are compared with the new string for encoding the
message. The reverse is done to decode the encoded message.

Example:
INPUT
Keyword: hello
Message to encode: Keep trying.

OUTPUT:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
HELOABCDFGIJKMNPQRSTUVWXYZ

The encoded message:


IAAP TRYFMC
The decoded message:
KEEP TRYING

124 | P a g e
IPO Cycle

Input Process Output


• Arranging the new set of
alphabets according to the
keyword.
The keyword in • The created string is
input. Next, the compared to the string of Printing the encoded and
message to be alphabets from A-Z for reverse decoded message
encoded is input. encoding.
• The reverse process is done to
decode back the encoded
message.

125 | P a g e
Code
import java.util.*;
public class Cipher_Encryption
{
public static void main(String[]args)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the keyword: ");//accepting keyword
String key=obj.nextLine();

String or ="";
for (int i=65; i<=90; i++)//creating string of alphabets
{
or+=(char)i;
}
System.out.println(or);
String en="";
boolean flag;
String decode="";
for (int i=0; i<key.length(); i++)//removing repeated characters
{
flag=true;
char ch=key.charAt(i);
if (Character.isLetter(ch))
{
for (int j=0; j<en.length(); j++)
{
if (ch==en.charAt(j))
{
flag=false;
break;
}
}
if (flag)
{
en+=ch;
}
}
}
en=en.toUpperCase();
decode+=en;
en=en+or;

126 | P a g e
for (int i=0; i<or.length(); i++)//creating string starting with key
{
flag=true;
char ch=or.charAt(i);
if (Character.isLetter(ch))
{
for (int j=0; j<decode.length(); j++)
{
if (ch==en.charAt(j))
{
flag=false;
break;
}
}
if (flag)
{
decode+=ch;
}
}
}
System.out.println(decode);//displaying created string
String encode="";

System.out.println("Enter the original message to encode: ");


String or_msg=obj.nextLine();
or_msg=or_msg.toUpperCase();
int l1=or_msg.length();
for (int i=0; i<l1; i++)
{
if (Character.isLetter(or_msg.charAt(i)))//checking condition
{
for (int j=0; j<or.length(); j++)
{
if (or_msg.charAt(i)==or.charAt(j))
{
encode+=decode.charAt(j);
break;
}
}
}
else
{
encode+=or_msg.charAt(i);
}

127 | P a g e
}
System.out.println("\n"+"The encoded message: "+"\n"+encode);
encode=encode.toUpperCase();
String newDecode="";
int l=encode.length();
for (int i=0; i<l; i++)//to decode back the entered message
{
if (Character.isLetter(encode.charAt(i)))
{
for (int j=0; j<decode.length(); j++)
{
if (encode.charAt(i)==decode.charAt(j))
{
newDecode+=or.charAt(j);
break;
}
}
}
else
{
newDecode+=encode.charAt(i);
}
}
System.out.println("\n"+"The decoded message: "+"\n"+newDecode);
}
}

128 | P a g e
Output

Input:
Enter the keyword:
Hello

Enter the original message to encode:


Keep trying

Output:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
HELOABCDFGIJKMNPQRSTUVWXYZ

The encoded message:


IAAP TRYFMC

The decoded message:


KEEP TRYING

129 | P a g e
Thank you

130 | P a g e

You might also like