Computer Project_25 JAVA Programs
Computer Project_25 JAVA Programs
VIDYALAYA
MAR IVANIOS VIDYA NAGAR, BETHANY HILLS
NALANCHIRA, TRIVANDRUM – 15
1|Page
Computer
Project
Submitted By:
Name : Harisankar Lal
Class : XII B
Roll no : 09
2|Page
Acknowledgement
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
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.
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
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
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.
14 | P a g e
IPO Cycle
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
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
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
Example 2:
INPUT:
M=100
N=5000
23 | P a g e
IPO Cycle
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
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:
34 | P a g e
Question 7 : ISBN
Write a Program in Java to check whether a number is an ISBN or not.
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
35 | P a g e
IPO Cycle
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
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;
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
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)
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
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];
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:
Example:
INPUT: N = 4
FIRST CHARACTER: @
SECOND CHARACTER: ?
THIRD CHARACTER: #
OUTPUT:
@ ? ? @
? # # ?
? # # ?
@ ? ? @
55 | P a g e
IPO Cycle
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
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
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);
}
}
}
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");
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
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);
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
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
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
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
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];
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
80 | P a g e
IPO Cycle
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();
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
85 | P a g e
IPO Cycle
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 (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
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:
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
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
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
100 | P a g e
IPO Cycle
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"
105 | P a g e
IPO Cycle
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.
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
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
112 | P a g e
Output
Sl No Input: Output:
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.
Example 4
INPUT: Alas! it failed #
OUTPUT: INVALID INPUT
114 | P a g e
IPO Cycle
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
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
124 | P a g e
IPO Cycle
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="";
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
Output:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
HELOABCDFGIJKMNPQRSTUVWXYZ
129 | P a g e
Thank you
130 | P a g e