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

ISC 2025 (1) (2)

The document outlines several programming tasks in Java, including generating Goldbach number pairs, shifting rows in a matrix, checking for pangrams, calculating future dates, verifying symmetric matrices, and counting keystrokes on a cellphone keypad. Each task includes specific input requirements, expected outputs, and example scenarios for testing. The document also provides code snippets for each task to assist in implementation.

Uploaded by

Binit Karmakar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

ISC 2025 (1) (2)

The document outlines several programming tasks in Java, including generating Goldbach number pairs, shifting rows in a matrix, checking for pangrams, calculating future dates, verifying symmetric matrices, and counting keystrokes on a cellphone keypad. Each task includes specific input requirements, expected outputs, and example scenarios for testing. The document also provides code snippets for each task to assist in implementation.

Uploaded by

Binit Karmakar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

ISC 2025 (PAPER 2)

1. 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)
10 = 3, 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
import java.util.Scanner;
class Goldbach{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("N = ");
int n = in.nextInt();
if(n > 50 || n % 2 != 0){
System.out.println("INVALID INPUT");
return;
}
int i = 3;
while(i <= (n - i)){
if(isPrime(i) && isPrime(n - i))
System.out.println(i + ", " + (n - i));
i++;
}
}
public static boolean isPrime(int n){
int f = 0;
for(int i = 1; i <= n; i++){
if(n % i == 0)
f++;
}
return f == 2;
}
}
2. SHIFT ROW

Write a program to declare a matrix A[][] of order (M × 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 becomes the last row, 2 nd row will be the
1st 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)
Example 2
INPUT:
M=4
N=3
Enter elements in the matrix:
54 120 187
78 55 289
134 67 89
63 341 122
OUTPUT:
FORMED MATRIX AFTER ROTATING:
78 55 289
134 67 89
63 341 122
54 120 187
Highest element: 341 (Row: 2 and Column: 1)
Example 3
INPUT:
M=2
N=3
OUTPUT: SIZE IS OUT OF RANGE. INVALID ENTRY.

import java.util.Scanner;
class ShiftRows{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("M = ");
int m = in.nextInt();
System.out.print("N = ");
int n = in.nextInt();
if(m < 3 || m > 9 || n < 3 || n > 9){
System.out.println("SIZE IS OUT OF RANGE. INVALID ENTRY.");
return;
}
int mat[][] = new int[m][n];
int r = 0;
int c = 0;
System.out.println("ENTER ELEMENTS IN THE MATRIX:");
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
mat[i][j] = in.nextInt();
}
}
System.out.println("ORIGINAL MATRIX:");
display(mat);
int a[] = new int[n];
for(int i = 0; i < n; i++)
a[i] = mat[0][i];
for(int i = 1; i < m; i++){
for(int j = 0; j < n; j++){
mat[i - 1][j] = mat[i][j];
}
}
for(int i = 0; i < n; i++)
mat[m - 1][i] = a[i];
System.out.println("FORMED MATRIX AFTER ROTATING:");
display(mat);
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(mat[i][j] > mat[r][c]){
r = i;
c = j;
}
}
}
System.out.print("HIGHEST ELEMENT: " + mat[r][c]);
System.out.println(" (Row: " + r + " and Column: " + c + ")");
}
public static void display(int mat[][]){
for(int i = 0; i < mat.length; i++){
for(int j = 0; j < mat[0].length; j++){
System.out.print(mat[i][j] + "\t");
}
System.out.println();
}
}
}
3. 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-sensitive.
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

INPUT: Alas! it failed#


OUTPUT: INVALID INPUT

import java.util.Scanner;
class Pangram{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the sentence: ");
String s = in.nextLine();
if(s.length() == 0){
System.out.println("INVALID INPUT");
return;
}
char last = s.charAt(s.length() - 1);
if(".?!".indexOf(last) == -1){
System.out.println("INVALID INPUT");
return;
}
String shortest = new String(s);
String longest = new String();
boolean isPangram = true;
for(char ch = 'a'; ch <= 'z'; ch++){
boolean match = false;
for(int i = 0; i < s.length(); i++){
if(ch == Character.toLowerCase(s.charAt(i))){
match = true;
break;
}
}
if(!match){
isPangram = false;
break;
}
}
if(isPangram)
System.out.println("IT IS A PANGRAM");
else
System.out.println("IT IS NOT A PANGRAM");
String word = new String();
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
if(Character.isLetterOrDigit(ch))
word += ch;
else{
if(word.length() > longest.length())
longest = new String(word);
if(word.length() < shortest.length())
shortest = new String(word);
word = new String();
}
}
System.out.println("LONGEST WORD: " + longest);
System.out.println("SHORTEST WORD: " + shortest);
}
}
4. FUTURE DATE

Write a program in Java to accept day number (between 1 and 366) and year (yyyy) from the
user and display the corresponding date. Also accept ‘N’ from the user where (1 ≤ N ≤ 100) to
compute and display the future date ‘N’ days after the given date. Display error message if
the value of the day number or ‘N’ are not within the limit. Day number is calculated taking
1st January of the given year as 1.
Test your program with given set of data and some random data.
Example 1
INPUT:
DAY NUMBER: 50
YEAR: 2024
N = 25
OUTPUT:
ENTERED DATE: FEBRUARY 19, 2024
25 DAYS LATER: MARCH 15, 2024
Example 2
INPUT:
DAY NUMBER: 321
YEAR: 2024
N: 77
OUTPUT:
ENTERED DATE: NOVEMBER 16, 2024
77 DAYS LATER: FEBRUARY 1, 2025
Example 3
INPUT:
DAY NUMBER: 400
YEAR: 2024
N: 125
OUTPUT:
INCORRECT DAY NUMBER
INCORRECT VALUE OF ‘N’
import java.util.Scanner;
class LaterDate{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
boolean validDayNum = true;
boolean validN = true;
System.out.print("DAY NUMBER: ");
int dayNum = Integer.parseInt(in.nextLine());
System.out.print("YEAR: ");
int year = Integer.parseInt(in.nextLine());
System.out.print("N: ");
int n = Integer.parseInt(in.nextLine());
if(dayNum < 1 || (dayNum > 365 && !isLeap(year))){
validDayNum = false;
System.out.println("INCORRECT DAY NUMBER");
}
else if(dayNum > 366 && isLeap(year)){
validDayNum = false;
System.out.println("INCORRECT DAY NUMBER");
}
if(n < 1 || n > 100){
validN = false;
System.out.println("INCORRECT VALUE OF 'N'");
}
if(!validDayNum || !validN)
return;
int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String month[] = {"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY",
"AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"};
if(isLeap(year))
day[1] = 29;
int index = 0;
while(dayNum > day[index]){
dayNum -= day[index++];
if(index == 12){
index = 0;
year++;
if(isLeap(year))
day[1] = 29;
else
day[1] = 28;
}
}
System.out.println("ENTERED DATE: " + month[index] + " " + dayNum + ", " + year);
System.out.print(n + " DAYS LATER: ");
while(n > 0){
dayNum++;
n--;
if(dayNum > day[index]){
dayNum = 1;
index++;
if(index == 12){
index = 0;
year++;
if(isLeap(year))
day[1] = 29;
else
day[1] = 28;
}
}
}
System.out.println(month[index] + " " + dayNum + ", " + year);
}
public static boolean isLeap(int y){
if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
return true;
return false;
}
}
5. SYMMETRIC MATRIX

Write a program to declare a square matrix A[][] of order M × M where ‘M’ is the number of
rows and the number of columns, such that M must be greater than 2 and less than 10.
Accept the value of M as user input. Display an appropriate message for an invalid input.
Allow the user to input integers into this matrix. Perform the following tasks:
(a) Display the original matrix.
(b) Check if the given matrix is said to be symmetric or not. A square matrix is said to be
symmetric, if the element of the i th row and jth column is equal to the element of the jth row
and ith column.
(c) Find the sum of the elements of left diagonal and the sum of the elements of right
diagonal of the matrix and display them.
Example 1
INPUT:
M=3
Enter elements of the matrix:
123
245
356
OUTPUT:
ORIGINAL MATRIX
123
245
356
THE GIVEN MATRIX IS SYMMETRIC.
THE SUM OF THE LEFT DIAGONAL = 11
THE SUM OF THE RIGHT DIAGONAL = 10

Example 2
INPUT:
M=4
Enter elements of the matrix:
7892
4563
8531
7642
OUTPUT:
ORIGINAL MATRIX
7892
4563
8531
7642
THE GIVEN MATRIX IS NOT SYMMETRIC.
THE SUM OF THE LEFT DIAGONAL = 17
THE SUM OF THE RIGHT DIAGONAL = 20
Example 3
INPUT: M = 12
OUTPUT: SIZE IS OUT OF RANGE.

import java.util.Scanner;
class Symmetric{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("M = ");
int m = Integer.parseInt(in.nextLine());
if(m < 3 || m > 9){
System.out.println("SIZE IS OUT OF RANGE");
return; }
int a[][] = new int[m][m];
System.out.println("Enter elements of the matrix:");
for(int i = 0; i < m; i++){
for(int j = 0; j < m; j++){
a[i][j] = Integer.parseInt(in.nextLine());
} }
int left = 0;
int right = 0;
boolean isSymmetric = true;
System.out.println("ORIGINAL MATRIX");
for(int i = 0; i < m; i++){
for(int j = 0; j < m; j++){
System.out.print(a[i][j] + "\t");
if(a[i][j] != a[j][i])
isSymmetric = false;
if(i == j)
left += a[i][j];
if(i + j == m - 1)
right += a[i][j];
}
System.out.println();
}
if(isSymmetric)
System.out.println("THE GIVEN MATRIX IS SYMMETRIC.");
else
System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC.");
System.out.println("THE SUM OF THE LEFT DIAGONAL = " + left);
System.out.println("THE SUM OF THE RIGHT DIAGONAL = " + right);
}}
6. CELLPHONE KEYPAD

Most (not all) cell phone keypads look like the following arrangement (the letters are above
the respective number):

For sending text/SMS, the common problem is the number of keystrokes to type a particular
text.
For example, in the word “STOP”, there are a total of 9 keystrokes needed to type the word.
You need to press the key 7 four times, the key 8 once, the key 6 three times and the key 7
once to get it.
Develop a program code to find the number of keystrokes needed to type the text.
For this problem, accept just one word without any punctuation marks, numbers or
whitespaces and the text message would consist of just 1 word.
Test your data with the sample data and some random data:
Example 1:
INPUT: DEAR
OUTPUT: Number of keystrokes = 7
Example 2:
INPUT: Thanks
OUTPUT: Number of keystrokes = 12
Example 3:
INPUT: Good-bye
OUTPUT: INVALID ENTRY

import java.util.Scanner;
class Keypad{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the word: ");
String word = in.next().toUpperCase();
int count = 0;
for(int i = 0; i < word.length(); i++){
char ch = word.charAt(i);
if(!Character.isLetter(ch)){
System.out.println("INVALID ENTRY");
return;
}
if("ADGJMPTW".indexOf(ch) >= 0)
count++;
else if("BEHKNQUX".indexOf(ch) >= 0)
count += 2;
else if("CFILORVY".indexOf(ch) >= 0)
count += 3;
else
count += 4;
}
System.out.println("Number of keystrokes = " + count);
}
}
7. UNIQUE DIGIT INTEGER

A unique digit integer is a positive integer (without leading zeros) with no duplicate digits.
For example, 7, 135, 214 are all unique-digit integers whereas 33, 3121, 300 are not.
Given two positive integers m and n, where m < n, write a program to determine how many
unique-digit integers are there in the range between m and n (both inclusive) and output
them.
The input contains two positive integers m and n. Assume m < 30000 and n < 30000. You are
to output the number of unique-digit integers in the specified range along with their values in
the format specified below:
Example 1
INPUT:
m = 100
n = 120
OUTPUT:
THE UNIQUE-DIGIT INTEGERS ARE:
102, 103, 104, 105, 106, 107, 108, 109, 120
FREQUENCY OF UNIQUE-DIGIT INTEGERS IS: 9
Example 2
INPUT:
m = 2505
n = 2525
OUTPUT:
THE UNIQUE-DIGIT INTEGERS ARE:
2506, 2507, 2508, 2509, 2510, 2513, 2514, 2516, 2517, 2518, 2519
FREQUENCY OF UNIQUE-DIGIT INTEGERS IS: 11
Example 3
INPUT:
m = 2520
n = 2529
OUTPUT:
THE UNIQUE-DIGIT INTEGERS ARE: NIL
FREQUENCY OF UNIQUE-DIGIT INTEGERS IS: 0

import java.util.Scanner;
class UniqueDigit{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("m = ");
int m = Integer.parseInt(in.nextLine());
System.out.print("n = ");
int n = Integer.parseInt(in.nextLine());
if(m > n || m >= 30000 || n >= 30000){
System.out.println("INVALID RANGE");
return;
}
int count = 0;
System.out.print("THE UNIQUE-DIGIT INTEGERS ARE: ");
for(int i = m; i <= n; i++){
if(isUnique(i)){
if(count == 0)
System.out.print("\n" + i);
else
System.out.print(", " + i);
count++;
}
}
if(count == 0)
System.out.print("NIL");
System.out.println("\nFREQUENCY OF UNIQUE-DIGIT INTEGERS IS: " + count);
}
public static boolean isUnique(int n){
String s = String.valueOf(n);
for(int i = 0; i < s.length() - 1; i++){
char ch = s.charAt(i);
String sub = s.substring(i + 1);
if(sub.indexOf(ch) >= 0)
return false;
}
return true;
}
}
8. MAXIMUM MINIMUM SORT

Write a program to declare a matrix A[][] of order (M × 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 20. Allow the user to input integers into this matrix.
Perform the following tasks on the matrix:
(a) Display the input matrix.
(b) Find the maximum and minimum value in the matrix and display them along with their
position.
(c) Sort the elements of the matrix in descending order using standard sorting technique and
rearrange them in the matrix.
(d) Output the rearranged matrix.
Test your program for the following data and some random data:
Example 1
INPUT:
M=3
N=4

ENTER ELEMENTS OF THE MATRIX:


879 3
-2 0 4 5
1 3 6 -4
OUTPUT:
ORIGINAL MATRIX
879 3
-2 0 4 5
1 3 6 -4
LARGEST NUMBER: 9
ROW = 0
COLUMN = 2
SMALLEST NUMBER = -4
ROW = 2
COLUMN = 3
REARRANGED MATRIX
-4 -2 0 1
3 345
6 789
Example 2
INPUT:
M=3
N=3
ENTER ELEMENTS OF THE MATRIX:
7 93
-2 4 5
1 16 4
OUTPUT:
ORIGINAL MATRIX
7 93
-2 4 5
1 16 4
LARGEST NUMBER: 16
ROW = 2
COLUMN = 1
SMALLEST NUMBER: -2
ROW = 1
COLUMN = 0
REARRANGED MATRIX
-2 1 3
44 5
7 9 16
Example 3
INPUT:
M=3
N = 22
OUTPUT: SIZE OUT OF RANGE

import java.util.Scanner;
class MaxMin{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("M = ");
int m = Integer.parseInt(in.nextLine());
System.out.print("N = ");
int n = Integer.parseInt(in.nextLine());
if(m < 3 || n < 3 || m > 19 || n > 19){
System.out.println("SIZE OUT OF RANGE");
return;
}
int a[][] = new int[m][n];
int b[] = new int[m * n];
int index = 0;
System.out.println("ENTER ELEMENTS OF THE MATRIX:");
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
a[i][j] = Integer.parseInt(in.nextLine());
b[index++] = a[i][j];
}
}
System.out.println("ORIGINAL MATRIX");
display(a);
int max = a[0][0];
int i1 = 0;
int j1 = 0;
int min = a[0][0];
int i2 = 0;
int j2 = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(a[i][j] > max){
max = a[i][j];
i1 = i;
j1 = j;
}
if(a[i][j] < min){
min = a[i][j];
i2 = i;
j2 = j;
}
}
}
System.out.println("LARGEST NUMBER: " + max);
System.out.println("ROW = " + i1);
System.out.println("COLUMN = " + j1);
System.out.println("SMALLEST NUMBER: " + min);
System.out.println("ROW = " + i2);
System.out.println("COLUMN = " + j2);
for(int i = 0; i < b.length; i++){
for(int j = 0; j < b.length - 1 - i; j++){
if(b[j] > b[j + 1]){
int temp = b[j];
b[j] = b[j + 1];
b[j + 1] = temp;
}
}
}
index = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
a[i][j] = b[index++];
}
}
System.out.println("REARRANGED MATRIX");
display(a);
}
public static void display(int mat[][]){
for(int i = 0; i < mat.length; i++){
for(int j = 0; j < mat[0].length; j++){
System.out.print(mat[i][j] + "\t");
}
System.out.println();
}
}
}
9. ANAGRAM

Write a program to check if a given string is an Anagram of another string. Two strings are
anagrams if they can be rearranged to form the same thing. For example, “listen” and “silent”
are anagrams. Accept two strings from the user and check if they are anagrams of each other.
Ensure that the comparison is case-sensitive and ignores spaces. Display an appropriate
message based on whether they are anagrams or not. If any of the strings contain invalid
characters (e.g., numbers or special characters), generate an error message.
Test your program with the following data and some random data:
Example 1
INPUT:
Enter first string: Listen
Enter second string: Silent
OUTPUT: STRINGS ARE ANAGRAMS

Example 2
INPUT:
Enter first string: Dormitory
Enter second string: Dirty room
OUTPUT: STRINGS ARE ANAGRAMS
Example 3
INPUT:
Enter first string: Hello
Enter second string: World
OUTPUT: STRINGS ARE NOT ANAGRAMS
Example 4
INPUT:
Enter first string: Test123
Enter second string: 321tset
OUTPUT: INVALID CHARACTERS IN STRING. INVALID INPUT

import java.util.Scanner;
class Anagrams{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter first string: ");
String a = in.nextLine().toUpperCase();
System.out.print("Enter second string: ");
String b = in.nextLine().toUpperCase();
if(!valid(a) || !valid(b)){
System.out.println("INVALID CHARACTERS IN STRING. INVALID INPUT");
return;
}
a = removeSpaces(a);
b = removeSpaces(b);
boolean status = true;
while(a.length() > 0){
String x = String.valueOf(a.charAt(0));
a = a.replace(x, "");
b = b.replace(x, "");
if(a.length() != b.length()){
status = false;
break;
}
}
if(status)
System.out.println("THEY ARE ANAGRAMS!");
else
System.out.println("THEY ARE NOT ANAGRAMS.");
}
public static boolean valid(String s){
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
if(!Character.isWhitespace(ch) && !Character.isLetter(ch))
return false;
}
return true;
}
public static String removeSpaces(String s){
String str = new String();
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
if(Character.isWhitespace(ch))
continue;
str += ch;
}
return str;
}
}

You might also like