//2020
1.
import java.util.Scanner;
public class PrimeAdam
public static int reverse(int num)
int rev = 0;
while (num != 0)
int d = num % 10;
rev = rev * 10 + d;
num /= 10;
return rev;
public static boolean isAdam(int num)
int sqNum = num * num;
int revNum = reverse(num);
int sqRevNum = revNum * revNum;
int rev = reverse(sqNum);
return rev == sqRevNum;
public static boolean isPrime(int num)
int c = 0;
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
c++;
return c == 2;
public static void main(String args[])
Scanner in = new Scanner(System.in);
System.out.print("Enter the value of m: ");
int m = in.nextInt();
System.out.print("Enter the value of n: ");
int n = in.nextInt();
int count = 0;
if (m >= n)
System.out.println("INVALID INPUT");
return;
System.out.println("THE PRIME-ADAM INTEGERS ARE:");
for (int i = m; i <= n; i++)
boolean adam = isAdam(i);
if (adam)
boolean prime = isPrime(i);
if (prime)
System.out.print(i + " ");
count++;
}
if (count == 0)
System.out.print("NIL");
System.out.println();
System.out.println("FREQUENCY OF PRIME-ADAM INTEGERS IS: " + count);
2.
import java.util.Scanner;
public class OctalMatrix
public static void main(String args[])
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of rows (M): ");
int m = in.nextInt();
System.out.print("Enter the number of columns (N): ");
int n = in.nextInt();
if (m <= 0 || m >= 10 || n <= 2 || n >= 6)
System.out.println("OUT OF RANGE");
return;
int a[][] = new int[m][n];
for (int i = 0; i < m; i++)
{
System.out.println("ENTER ELEMENTS FOR ROW " + (i + 1) + ": ");
for (int j = 0; j < n; j++)
a[i][j] = in.nextInt();
if (a[i][j] < 0 || a[i][j] > 7)
System.out.println("INVALID INPUT");
return;
System.out.println("FILLED MATRIX\tDECIMAL EQUIVALENT");
for (int i = 0; i < m; i++)
int decNum = 0;
for (int j = 0; j < n; j++)
decNum += a[i][j] * Math.pow(8, n - j - 1 );
System.out.print(a[i][j] + " ");
System.out.print("\t\t" + decNum);
System.out.println();
3.
import java.util.*;
public class StringCheck
public static String sortString(String ipStr)
{
StringTokenizer st = new StringTokenizer(ipStr);
int wordCount = st.countTokens();
String strArr[] = new String[wordCount];
for (int i = 0; i < wordCount; i++)
strArr[i] = st.nextToken();
for (int i = 0; i < wordCount - 1; i++)
for (int j = 0; j < wordCount - i - 1; j++)
if (strArr[j].length() > strArr[j + 1].length())
String t = strArr[j];
strArr[j] = strArr[j+1];
strArr[j+1] = t;
if (strArr[j].length() == strArr[j + 1].length()
&&(strArr[j].compareTo(strArr[j+1]) > 0))
String t = strArr[j];
strArr[j] = strArr[j+1];
strArr[j+1] = t;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < wordCount; i++)
sb.append(strArr[i]);
sb.append(" ");
}
return sb.toString().trim();
public static void main(String args[])
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
int len = str.length();
System.out.println();
if (str.charAt(len - 1) != '.' && str.charAt(len - 1) != '?'
&& str.charAt(len - 1) != '!')
System.out.println("INVALID INPUT");
return;
String sortedStr = sortString(str.substring(0, len - 1));
System.out.println(str);
System.out.println(sortedStr);
//2019
1.
import java.util.Scanner;
public class DateCalculator
public static boolean isLeapYear(int y) {
boolean ret = false;
if (y % 400 == 0) {
ret = true;
else if (y % 100 == 0) {
ret = false;
else if (y % 4 == 0) {
ret = true;
else {
ret = false;
return ret;
public static String computeDate(int day, int year) {
int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String monthNames[] = {"JANUARY", "FEBRUARY", "MARCH",
"APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER",
"OCTOBER", "NOVEMBER", "DECEMBER"};
boolean leap = isLeapYear(year);
if (leap) {
monthDays[1] = 29;
int i = 0;
int daySum = 0;
for (i = 0; i < monthDays.length; i++) {
daySum += monthDays[i];
if (daySum >= day) {
break;
int date = day + monthDays[i] - daySum;
StringBuffer sb = new StringBuffer();
sb.append(date);
sb.append("TH ");
sb.append(monthNames[i]);
sb.append(", ");
sb.append(year);
return sb.toString();
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("DAY NUMBER: ");
int dayNum = in.nextInt();
System.out.print("YEAR: ");
int year = in.nextInt();
System.out.print("DATE AFTER (N DAYS): ");
int n = in.nextInt();
if (dayNum < 1 || dayNum > 366) {
System.out.println("DAY NUMBER OUT OF RANGE");
return;
}
if (n < 1 || n > 100) {
System.out.println("DATE AFTER (N DAYS) OUT OF RANGE");
return;
String dateStr = computeDate(dayNum, year);
int nDays = dayNum + n;
int nYear = year;
boolean leap = isLeapYear(year);
if (leap && nDays > 366) {
nYear = nYear + 1;
nDays = nDays - 366;
else if (nDays > 365) {
nYear = nYear + 1;
nDays = nDays - 365;
String nDateStr = computeDate(nDays, nYear);
System.out.println();
System.out.println("DATE: " + dateStr);
System.out.println("DATE AFTER " + n
+ " DAYS: " + nDateStr);
2.
import java.util.Scanner;
public class Array
public static void sortArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("ENTER VALUE OF N: ");
int n = in.nextInt();
if (n <= 2 || n >= 10) {
System.out.println("MATRIX SIZE OUT OF RANGE");
return;
int a[] = new int[n];
int b[][] = new int[n][n];
System.out.println("ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY:");
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
sortArray(a);
System.out.println("SORTED ARRAY:");
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
for (int i = n - 1, r = 0; i >= 0; i--, r++) {
for (int j = 0; j <= i; j++) {
b[r][j] = a[j];
for (int k = n - 1; k > i; k--) {
b[r][k] = a[k - i - 1];
System.out.println();
System.out.println("FILLED MATRIX:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(b[i][j] + " ");
System.out.println();
3.
import java.util.*;
public class Palindrome
public static boolean isPalindrome(String word) {
boolean palin = true;
int len = word.length();
for (int i = 0; i <= len / 2; i++) {
if (word.charAt(i) != word.charAt(len - 1 - i)) {
palin = false;
break;
return palin;
public static String makePalindrome(String word) {
int len = word.length();
char lastChar = word.charAt(len - 1);
int i = len - 1;
while (word.charAt(i) == lastChar) {
i--;
StringBuffer sb = new StringBuffer(word);
for (int j = i; j >= 0; j--) {
sb.append(word.charAt(j));
}
return sb.toString();
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("ENTER THE SENTENCE:");
String ipStr = in.nextLine().trim().toUpperCase();
int len = ipStr.length();
char lastChar = ipStr.charAt(len - 1);
if (lastChar != '.'
&& lastChar != '?'
&& lastChar != '!') {
System.out.println("INVALID INPUT");
return;
String str = ipStr.substring(0, len - 1);
StringTokenizer st = new StringTokenizer(str);
StringBuffer sb = new StringBuffer();
while (st.hasMoreTokens()) {
String word = st.nextToken();
boolean isPalinWord = isPalindrome(word);
if (isPalinWord) {
sb.append(word);
else {
String palinWord = makePalindrome(word);
sb.append(palinWord);
}
sb.append(" ");
String convertedStr = sb.toString().trim();
System.out.println();
System.out.println(ipStr);
System.out.println(convertedStr);
//2018
1.
import java.util.Scanner;
public class GoldbachNumber
public static boolean isPrime(int num) {
int c = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
c++;
return c == 2;
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("ENTER THE VALUE OF N: ");
int n = in.nextInt();
if (n <= 9 || n >= 50) {
System.out.println("INVALID INPUT. NUMBER OUT OF RANGE.");
return;
if (n % 2 != 0) {
System.out.println("INVALID INPUT. NUMBER IS ODD.");
return;
System.out.println("PRIME PAIRS ARE:");
int a = 3;
int b = 0;
while (a <= n / 2) {
b = n - a;
if (isPrime(a) && isPrime(b)) {
System.out.println(a + ", " + b);
a += 2;
2.
import java.util.Scanner;
public class ArraySort
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("ENTER THE VALUE OF M: ");
int m = in.nextInt();
System.out.print("ENTER THE VALUE OF N: ");
int n = in.nextInt();
if (m <= 2|| m >= 10 || n <= 2 || n >= 10)
System.out.println("MATRIX SIZE OUT OF RANGE.");
return;
int a[][] = new int[m][n];
System.out.println("ENTER ELEMENTS OF MATRIX:");
for (int i = 0; i < m; i++) {
System.out.println("ENTER ELEMENTS OF ROW " + (i+1) + ":");
for (int j = 0; j < n; j++) {
a[i][j] = in.nextInt();
System.out.println("ORIGINAL MATRIX");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
System.out.println();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n - 1; j++) {
for (int k = 0; k < n - j - 1; k++) {
if (a[i][k] > a[i][k + 1]) {
int t = a[i][k];
a[i][k] = a[i][k+1];
a[i][k+1] = t;
System.out.println("MATRIX AFTER SORTING ROWS");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
System.out.println();
3.
import java.util.Scanner;
public class Banner
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("ENTER THE VALUE OF N: ");
int n = in.nextInt();
in.nextLine();
if (n <= 2 || n >= 9) {
System.out.println("INVALID INPUT");
return;
String teams[] = new String[n];
int highLen = 0;
for (int i = 0; i < n; i++) {
System.out.print("Team " + (i+1) + ": ");
teams[i] = in.nextLine();
if (teams[i].length() > highLen) {
highLen = teams[i].length();
for (int i = 0; i < highLen; i++) {
for (int j = 0; j < n; j++) {
int len = teams[j].length();
if (i >= len) {
System.out.print(" \t");
else {
System.out.print(teams[j].charAt(i) + "\t");
System.out.println();
}
//2017
1.
import java.util.Scanner;
public class CartonBoxes
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of boxes (N): ");
int n = in.nextInt();
if (n < 1 || n > 1000) {
System.out.println("INVALID INPUT");
return;
int cartonSizes[] = {48, 24, 12, 6};
int total = 0;
int t = n;
for (int i = 0; i < cartonSizes.length; i++) {
int cartonCount = t / cartonSizes[i];
t = t % cartonSizes[i];
total += cartonCount;
if (cartonCount != 0) {
System.out.println(cartonSizes[i] + " * " + cartonCount +
" = " + (cartonSizes[i] * cartonCount));
if (t != 0) {
System.out.println("Remaining boxes = " + t
+ " * 1 = " + t);
total++;
else {
System.out.println("Remaining boxes = 0");
System.out.println("Total number of boxes = " + n);
System.out.println("Total number of cartons = " + total);
2.
import java.util.Scanner;
public class QuizCompetition
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the Number of Participants (N): ");
int n = in.nextInt();
if (n <= 3 || n >= 11) {
System.out.println("INPUT SIZE OUT OF RANGE.");
return;
char answers[][] = new char[n][5];
char key[] = new char[5];
System.out.println("Enter answers of participants");
for (int i = 0; i < n; i++) {
System.out.println("Participant " + (i+1));
for (int j = 0; j < 5; j++) {
answers[i][j] = in.next().charAt(0);
System.out.println("Enter Answer Key:");
for (int i = 0; i < 5; i++) {
key[i] = in.next().charAt(0);
int hScore = 0;
int score[] = new int[n];
System.out.println("Scores:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < 5; j++) {
if (answers[i][j] == key[j]) {
score[i]++;
if (score[i] > hScore) {
hScore = score[i];
System.out.println("Participant " + (i+1) + " = " + score[i]);
System.out.println("Highest Score:");
for (int i = 0; i < n; i++) {
if (score[i] == hScore) {
System.out.println("Participant " + (i+1));
3.
import java.util.Scanner;
public class CaesarCipher
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter plain text:");
String str = in.nextLine();
int len = str.length();
if (len <= 3 || len >= 100) {
System.out.println("INVALID LENGTH");
return;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if ((ch >= 'A' && ch <= 'M') || (ch >= 'a' && ch <= 'm')) {
sb.append((char)(ch + 13));
else if ((ch >= 'N' && ch <= 'Z') || (ch >= 'n' && ch <= 'z')) {
sb.append((char)(ch - 13));
}
else {
sb.append(ch);
String cipher = sb.toString();
System.out.println("The cipher text is:");
System.out.println(cipher);
//2016
1.
import java.util.Scanner;
public class CircularPrime
public static boolean isPrime(int num) {
int c = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
c++;
return c == 2;
public static int getDigitCount(int num) {
int c = 0;
while (num != 0) {
c++;
num /= 10;
return c;
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("ENTER INTEGER TO CHECK (N): ");
int n = in.nextInt();
if (n <= 0) {
System.out.println("INVALID INPUT");
return;
boolean isCircularPrime = true;
if (isPrime(n)) {
System.out.println(n);
int digitCount = getDigitCount(n);
int divisor = (int)(Math.pow(10, digitCount - 1));
int n2 = n;
for (int i = 1; i < digitCount; i++) {
int t1 = n2 / divisor;
int t2 = n2 % divisor;
n2 = t2 * 10 + t1;
System.out.println(n2);
if (!isPrime(n2)) {
isCircularPrime = false;
break;
else {
isCircularPrime = false;
if (isCircularPrime) {
System.out.println(n + " IS A CIRCULAR PRIME.");
else {
System.out.println(n + " IS NOT A CIRCULAR PRIME.");
2.
import java.util.Scanner;
public class MatrixSort
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("ENTER MATRIX SIZE (M): ");
int m = in.nextInt();
if (m <= 3 || m >= 10) {
System.out.println("THE MATRIX SIZE IS OUT OF RANGE.");
return;
int a[][] = new int[m][m];
System.out.println("ENTER ELEMENTS OF MATRIX");
for (int i = 0; i < m; i++) {
System.out.println("ENTER ROW " + (i+1) + ":");
for (int j = 0; j < m; j++) {
a[i][j] = in.nextInt();
if (a[i][j] < 0) {
System.out.println("INVALID INPUT");
return;
System.out.println("ORIGINAL MATRIX");
printMatrix(a, m);
sortNonBoundaryMatrix(a, m);
System.out.println("REARRANGED MATRIX");
printMatrix(a, m);
computePrintDiagonalSum(a, m);
public static void sortNonBoundaryMatrix(int a[][], int m) {
int b[] = new int[(m - 2) * (m - 2)];
int k = 0;
for (int i = 1; i < m - 1; i++) {
for (int j = 1; j < m - 1; j++) {
b[k++] = a[i][j];
}
for (int i = 0; i < k - 1; i++) {
for (int j = 0; j < k - i - 1; j++) {
if (b[j] > b[j + 1]) {
int t = b[j];
b[j] = b[j+1];
b[j+1] = t;
k = 0;
for (int i = 1; i < m - 1; i++) {
for (int j = 1; j < m - 1; j++) {
a[i][j] = b[k++];
public static void computePrintDiagonalSum(int a[][], int m) {
int sum = 0;
System.out.println("DIAGONAL ELEMENTS");
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
if (i == j || i + j == m - 1) {
sum += a[i][j];
System.out.print(a[i][j] + "\t");
else {
System.out.print("\t");
}
}
System.out.println();
System.out.println("SUM OF THE DIAGONAL ELEMENTS = " + sum);
public static void printMatrix(int a[][], int m) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
System.out.print(a[i][j] + "\t");
System.out.println();
3.
import java.util.*;
public class VowelWord
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("ENTER THE SENTENCE:");
String ipStr = in.nextLine().trim().toUpperCase();
int len = ipStr.length();
char lastChar = ipStr.charAt(len - 1);
if (lastChar != '.'
&& lastChar != '?'
&& lastChar != '!') {
System.out.println("INVALID INPUT");
return;
String str = ipStr.substring(0, len - 1);
StringTokenizer st = new StringTokenizer(str);
StringBuffer sbVowel = new StringBuffer();
StringBuffer sb = new StringBuffer();
int c = 0;
while (st.hasMoreTokens()) {
String word = st.nextToken();
int wordLen = word.length();
if (isVowel(word.charAt(0))
&& isVowel(word.charAt(wordLen - 1))) {
c++;
sbVowel.append(word);
sbVowel.append(" ");
else {
sb.append(word);
sb.append(" ");
String newStr = sbVowel.toString() + sb.toString();
System.out.println("NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = " + c);
System.out.println(newStr);
}
public static boolean isVowel(char ch) {
ch = Character.toUpperCase(ch);
boolean ret = false;
if (ch == 'A'
|| ch == 'E'
|| ch == 'I'
|| ch == 'O'
|| ch == 'U')
ret = true;
return ret;
//2015
1.
import java.util.*;
class Number
private int M, N;
String output;
private boolean status;
public Number(int M, int N)
this.M = M;
this.N = N;
output = "";
status = false;
public void findSolution()
{
int M = this.M;
int sum = 0;
do {
sum = sumOfDigits(M);
if (sum == N)
output = "OUTPUT:\tThe required number = " + M + "\n\tTotal number of digits = " +
countDigits(M);
status = true;
M++;
if (M == Integer.MAX_VALUE)
status = false;
break;
while (sum != N);
public boolean hasSolution()
return status;
private int sumOfDigits(int n)
int sum = 0;
for (int temp = n; temp > 0; temp /= 10)
sum += temp % 10;
return sum;
}
private int countDigits(int n)
int count = 0;
for (int temp = n; temp > 0; temp /= 10)
count++;
return count;
public String toString()
return output;
class ISC2015Q1
public static void main(String[] args)
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\tM = ");
int M = in.nextInt();
System.out.print("\tN = ");
int N = in.nextInt();
if (M < 100 || M > 10000 || N < 0 || N >= 100)
System.out.println("OUTPUT:\tINVALID INPUT");
} else
Number num = new Number(M, N);
num.findSolution();
if (num.hasSolution())
System.out.println(num);
} else
System.out.println("OUTPUT:\tNo solution exists");
in.close();
2.
import java.util.*;
class Matrix
private int A[][], M;
public Matrix(int M)
A = new int[M][M];
this.M = M;
public void getInput()
Scanner in = new Scanner(System.in);
for (int row = 0; row < M; row++)
for (int col = 0; col < M; col++)
A[row][col] = in.nextInt();
}
in.close();
public String toString()
String output = "";
for (int row = 0; row < M; row++)
output += "\t";
for (int col = 0; col < M; col++)
output += A[row][col];
if (col < M)
output += "\t";
output += "\n";
return output;
public void rotate()
int half = M / 2, temp;
for (int i = 0; i < half; i++)
for (int x =i; x<M-i-1;x++)
temp = A[M-x-1][i];
A[M-x-1][i]=A[M-i-1][M-x-1];
A[M-i-1][M-x-1]=A[x][M-i-1];
A[x][M-i-1]=A[i][x];
A[i][x]=temp;
}
}
public int getCornerSum()
return A[0][0]+A[0][M-1]+A[M-1][M-1]+A[M-1][0];
public class ISC2015Q2
public static void main(String[] args)
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\tM = ");
int M = in.nextInt();
if (M > 2 && M < 10)
Matrix objMatrix = new Matrix(M);
objMatrix.getInput();
System.out.println("OUTPUT:");
System.out.println("ORIGINAL MATRIX");
System.out.println(objMatrix);
objMatrix.rotate();
System.out.println("MATRIX AFTER ROTATION");
System.out.println(objMatrix);
System.out.println("Sum of the corner elements = "+objMatrix.getCornerSum());
else
System.out.println("OUTPUT: SIZE OUT OF RANGE");
in.close();
}
3.
import java.util.*;
class Sentence
private String sentence;
private static String vowels = "aeiouAEIOU";
public Sentence(String sentence)
this.sentence = sentence;
public boolean isValid()
return (new String(".?")).indexOf(sentence.charAt(sentence.length() - 1)) >= 0;
public String toString()
String output = "";
String source = sentence.substring(0, sentence.length() - 1);
String wordList[] = source.split(" ");
for (String word : wordList)
output += Character.toUpperCase(word.charAt(0)) + (word.substring(1)).toLowerCase() + " ";
return output.trim();
private static boolean isVowel(char ch)
return vowels.indexOf(ch) >= 0;
}
private static boolean isConsonant(char ch)
return Character.isLetter(ch) && !isVowel(ch);
public void displayStats()
System.out.printf("%-15s %-12s %-12s\n", "Word", "Vowels", "Consonants");
String source = sentence.substring(0, sentence.length() - 1);
String wordList[] = source.split(" ");
String titleCase = "";
int vowels = 0, consonants = 0;
char ch;
for (String word : wordList)
titleCase = Character.toUpperCase(word.charAt(0)) + (word.substring(1)).toLowerCase() + " ";
vowels = 0; consonants = 0;
for (int position = 0; position < titleCase.length(); position++)
ch = titleCase.charAt(position);
if (isVowel(ch))
vowels++;
else if (isConsonant(ch))
consonants++;
System.out.printf("%-15s %-12d %-12d\n", titleCase, vowels, consonants);
class ISC2015Q3
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\t");
String input = in.nextLine();
System.out.print("OUTPUT:\t");
Sentence objSentence = new Sentence(input);
if (objSentence.isValid())
System.out.println(objSentence);
objSentence.displayStats();
} else
System.out.println("INVALID INPUT");
in.close();
//2014
1.
import java.util.*;
class CompositeMagicNumber
private int m, n, frequency;
String output;
public CompositeMagicNumber(int m, int n)
this.m = m;
this.n = n;
public void process()
{
frequency = 0;
output = "THE COMPOSITE INTEGERS ARE:\n\t";
for (int i = m; i <= n; i++)
if (isMagicNumber(i) && isCompositeNumber(i))
frequency++;
output+=i+", ";
output=output.substring(0,output.length()-2);
public String toString()
return output;
public int getFrequency()
return frequency;
private static boolean isPrimeNumber(int n)
if (n < 2)
return false;
else if (n == 2)
return true;
else if (n % 2 == 0)
return false;
else
int limit = (int) Math.sqrt(n);
for (int divisor = 3; divisor <= limit; divisor += 2)
if (n % divisor == 0)
return false;
return true;
private static boolean isCompositeNumber(int n)
return !isPrimeNumber(n);
private static boolean isMagicNumber(int n)
return n % 9 == 1;
class ISC2014Q1
public static void main(String[] args)
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\tm = ");
int m = in.nextInt();
System.out.print("\tn = ");
int n = in.nextInt();
System.out.print("OUTPUT:\t");
if (m < n)
CompositeMagicNumber obj = new CompositeMagicNumber(m, n);
obj.process();
System.out.println(obj);
System.out.println("FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: " + obj.getFrequency());
} else
System.out.println("INVALID INPUT");
in.close();
2.
import java.util.*;
class Matrix
private int A[][], M;
public Matrix(int M)
this.M = M;
A = new int[M][M];
public String toString()
String output = "";
for (int row = 0; row < M; row++)
output += "\t";
for (int col = 0; col < M; col++)
output += A[row][col];
if (col < M - 1)
output += "\t";
}
output += "\n";
return output;
public void getInput()
Scanner in = new Scanner( System.in);
for(int row=0; row<M; row++)
for( int col=0; col<M; col++)
A[row][col]=in.nextInt();
in.close();
public boolean isSymmetric()
for(int row=0; row<M; row++)
for( int col=0; col<row; col++)
if( A[row][col]!=A[col][row]) return false;
return true;
public void computeAndDisplaySumOfDiagonals()
int sumOfLeftDiagonal=0, sumOfRightDiagonal=0;
for( int i=0; i<M;i++)
{
sumOfLeftDiagonal+=A[i][i];
sumOfRightDiagonal+=A[i][M-1-i];
System.out.println("The sum of the left diagonal = "+sumOfLeftDiagonal);
System.out.println("The sum of the right diagonal = "+sumOfRightDiagonal);
class ISC2014Q2
public static void main(String[] args)
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\tM = ");
int M = in.nextInt();
if (M > 2 && M < 10)
Matrix objMatrix = new Matrix(M);
objMatrix.getInput();
System.out.println("OUTPUT:\tORIGINAL MATRIX");
System.out.println(objMatrix);
if(objMatrix.isSymmetric())
System.out.println("THE GIVEN MATRIX IS SYMMETRIC");
}else
System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC");
objMatrix.computeAndDisplaySumOfDiagonals();
} else
{
System.out.println("OUTPUT:\tTHE MATRIX SIZE IS OUT OF RANGE");
in.close();
3.
import java.util.*;
class Sentence
private String source,wordList[], answer;
private char terminator;
public Sentence(String source)
this.source = source;
public boolean isValid()
terminator = source.charAt(source.length()-1);
return (new String(".?!")).indexOf(terminator)>-1;
public void delete(String wordToBeDeleted, int position)
answer="";
wordList = source.split(" ");
System.out.println(Arrays.toString(wordList));
int wordCount=0;
for(String word: wordList)
if(word.length()==0) continue;
wordCount++;
if(wordCount==position && word.equals(wordToBeDeleted)) continue;
answer+=word+" ";
public String toString()
return answer.trim();
class ISC2014Q3
public static void main(String[] args)
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\t");
String source = in.nextLine();
Sentence objSentence = new Sentence(source);
if(objSentence.isValid())
System.out.print("WORD TO BE DELETED: ");
String wordToBeDeleted = in.nextLine();
System.out.print("WORD POSITION IN THE SENTENCE: ");
int position = in.nextInt();
objSentence.delete(wordToBeDeleted,position);
System.out.println("OUTPUT:\t"+objSentence);
else
System.out.println("OUTPUT:\tINVALID INPUT.");
in.close();
}
}
//2013
1.
import java.util.*;
class ISBN
private String isbn;
private int sum;
public ISBN(String isbn)
this.isbn = isbn;
if (isbn.length() != 10)
throw new IllegalArgumentException();
for (int position = 0; position < 9; position++)
if (!Character.isDigit(isbn.charAt(position)))
throw new IllegalArgumentException();
char lastCharacter = isbn.charAt(9);
if (!(Character.isDigit(lastCharacter) || (lastCharacter == 'x' || lastCharacter == 'X')))
throw new IllegalArgumentException();
computeSum();
public void computeSum() {
int digit;
for (int position = 0; position < isbn.length(); position++)
{
digit = isbn.charAt(position) - '0';
digit = (digit == 40 || digit == 72) ? 10 : digit;
sum += digit * (10 - position);
public int getSum()
return sum;
class ISC2013Q1
public static void main(String[] args)
Scanner in = new Scanner(System.in);
System.out.print("INPUT CODE:\t");
String isbn = in.nextLine();
try
ISBN objISBN = new ISBN(isbn);
System.out.println("OUTPUT:\tSUM = " + objISBN.getSum());
if (objISBN.getSum() % 11 == 0)
System.out.println("\tLEAVES NO REMAINDER – VALID ISBN CODE");
} else
System.out.println("\tLEAVES REMAINDER – INVALID ISBN CODE");
catch (IllegalArgumentException e)
{
System.out.println("OUTPUT : INVALID INPUT");
in.close();
2.
import java.util.*;
class Matrix
private int A[][];
private int M;
public Matrix(int M)
this.M = M;
A=new int[M][M];
public void getInput()
Scanner in = new Scanner( System.in);
for( int row=0; row<M;row++)
for( int col=0; col<M;col++)
A[row][col]=in.nextInt();
in.close();
public String toString()
String output="";
for( int row=0; row<M;row++)
output+="\t";
for( int col=0; col<M;col++)
output+=A[row][col];
if(col<M-1)
output+="\t";
output+="\n";
return output;
public void mirror()
int temp;
for( int row=0; row<M;row++)
for(int col=0; col<M/2; col++)
temp=A[row][col];
A[row][col]=A[row][M-1-col];
A[row][M-1-col]=temp;
public class ISC2013Q2
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\tM = ");
int M = in.nextInt();
if (M > 2 && M < 20)
Matrix objMatrix = new Matrix(M);
objMatrix.getInput();
System.out.println("OUTPUT:\tORIGINAL MATRIX");
System.out.println(objMatrix);
objMatrix.mirror();
System.out.println("\tMIRROR IMAGE MATRIX");
System.out.println(objMatrix);
else
System.out.println("OUTPUT:\tSIZE OUT OF RANGE");
in.close();
3.
import java.util.*;
class Palindrome
private String sentence, answer;
private int frequency;
public Palindrome(String sentence)
this.sentence = sentence;
frequency = 0;
answer = "";
process();
private boolean isPalindrome(String word)
int half = word.length() / 2;
for (int position = 0; position < half; position++)
if (word.charAt(position) != word.charAt(word.length() - 1 - position))
return false;
return true;
private void process()
char ch;
String delimters=".?! ",word="";
for( int position=0; position<sentence.length();position++)
ch = sentence.charAt(position);
if(delimters.indexOf(ch)>=0)
if(isPalindrome(word))
answer+=word+" ";
frequency++;
word="";
else
word+=ch;
}
public int getFrequency()
return frequency;
public String toString()
return answer.trim();
public class ISC2013Q3
public static void main(String[] args)
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\t");
String sentence = in.nextLine();
Palindrome objPalindrome = new Palindrome(sentence);
System.out.print("OUTPUT:\t");
if(objPalindrome.getFrequency()>0)
System.out.println(objPalindrome);
System.out.println("\tNUMBER OF PALINDROMIC WORDS: "+objPalindrome.getFrequency());
else
System.out.println("NO PALINDROMIC WORDS");
in.close();
}
//2012
1.
import java.util.*;
class ISBN
private String isbn;
private int sum;
public ISBN(String isbn)
this.isbn = isbn;
if (isbn.length() != 10)
throw new IllegalArgumentException();
for (int position = 0; position < 9; position++)
if (!Character.isDigit(isbn.charAt(position)))
throw new IllegalArgumentException();
char lastCharacter = isbn.charAt(9);
if (!(Character.isDigit(lastCharacter) || (lastCharacter == 'x' || lastCharacter == 'X')))
throw new IllegalArgumentException();
computeSum();
public void computeSum()
int digit;
for (int position = 0; position < isbn.length(); position++)
digit = isbn.charAt(position) - '0';
digit = (digit == 40 || digit == 72) ? 10 : digit;
sum += digit * (10 - position);
public int getSum()
return sum;
class ISC2013Q1
public static void main(String[] args)
Scanner in = new Scanner(System.in);
System.out.print("INPUT CODE:\t");
String isbn = in.nextLine();
try
ISBN objISBN = new ISBN(isbn);
System.out.println("OUTPUT:\tSUM = " + objISBN.getSum());
if (objISBN.getSum() % 11 == 0)
System.out.println("\tLEAVES NO REMAINDER – VALID ISBN CODE");
else
System.out.println("\tLEAVES REMAINDER – INVALID ISBN CODE");
}
}
catch (IllegalArgumentException e)
System.out.println("OUTPUT : INVALID INPUT");
in.close();
2.
import java.util.*;
class Matrix
private int A[][];
private int M;
public Matrix(int M)
this.M = M;
A=new int[M][M];
public void getInput()
Scanner in = new Scanner( System.in);
for( int row=0; row<M;row++)
for( int col=0; col<M;col++)
A[row][col]=in.nextInt();
in.close();
}
public String toString()
String output="";
for( int row=0; row<M;row++)
output+="\t";
for( int col=0; col<M;col++)
output+=A[row][col];
if(col<M-1) output+="\t";
output+="\n";
return output;
public void mirror()
int temp;
for( int row=0; row<M;row++)
for(int col=0; col<M/2; col++)
temp=A[row][col];
A[row][col]=A[row][M-1-col];
A[row][M-1-col]=temp;
public class ISC2013Q2
{
public static void main(String[] args)
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\tM = ");
int M = in.nextInt();
if (M > 2 && M < 20)
Matrix objMatrix = new Matrix(M);
objMatrix.getInput();
System.out.println("OUTPUT:\tORIGINAL MATRIX");
System.out.println(objMatrix);
objMatrix.mirror();
System.out.println("\tMIRROR IMAGE MATRIX");
System.out.println(objMatrix);
else
System.out.println("OUTPUT:\tSIZE OUT OF RANGE");
in.close();
3.
import java.util.*;
class Palindrome
private String sentence, answer;
private int frequency;
public Palindrome(String sentence)
this.sentence = sentence;
frequency = 0;
answer = "";
process();
private boolean isPalindrome(String word)
int half = word.length() / 2;
for (int position = 0; position < half; position++)
if (word.charAt(position) != word.charAt(word.length() - 1 - position))
return false;
return true;
private void process()
char ch;
String delimters=".?! ",word="";
for( int position=0; position<sentence.length();position++)
ch = sentence.charAt(position);
if(delimters.indexOf(ch)>=0)
if(isPalindrome(word))
answer+=word+" ";
frequency++;
word="";
else
{
word+=ch;
public int getFrequency()
return frequency;
public String toString()
return answer.trim();
public class ISC2013Q3
public static void main(String[] args)
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\t");
String sentence = in.nextLine();
Palindrome objPalindrome = new Palindrome(sentence);
System.out.print("OUTPUT:\t");
if(objPalindrome.getFrequency()>0)
System.out.println(objPalindrome);
System.out.println("\tNUMBER OF PALINDROMIC WORDS: "+objPalindrome.getFrequency());
else
System.out.println("NO PALINDROMIC WORDS");
}
in.close();
//2012
1.
import java.util.*;
class PrimePalindrome
private int m,n,frequency;
private String output;
public PrimePalindrome(int m,int n)
this.m=m;
this.n=n;
process();
private static boolean isPrime(int n)
if(n<2)
return false;
else if(n==2)
return true;
else if(n%2==0)
return false;
else
int limit = (int)Math.sqrt(n);
for(int divisor=3; divisor<=limit; divisor+=2)
if(n%divisor==0)
return false;
return true;
private static boolean isPalindrome( int n)
int reverse=0;
for( int temp=n;temp>0;temp/=10)
reverse=reverse*10+(temp%10);
return n==reverse;
public void process()
output="";
frequency=0;
for( int number = m%2==0?m+1:m; number<=n; number+=2)
if(isPalindrome(number) && isPrime(number))
frequency++;
output+=number+", ";
output=output.substring(0,output.length()-2);
public String toString()
return output;
}
public int getFrequency()
return frequency;
class ISC2012Q1
public static void main(String[] args)
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\tm = ");
int m = in.nextInt();
System.out.print("\tn = ");
int n = in.nextInt();
if( m < 3000 && n<3000)
PrimePalindrome obj= new PrimePalindrome(m, n);
System.out.print("OUTPUT:\tTHE PRIME PALINDROME INTEGERS ARE:\n\t");
System.out.println(obj);
System.out.println("\tFREQUENCY OF PRIME PALINDROME INTEGERS: "+obj.getFrequency());
else
System.out.println("OUTPUT:\tOUT OF RANGE");
in.close();
2.
import java.util.*;
class Sentence
private String sentence,output;
private int length;
public Sentence(String sentence)
this.sentence=sentence.substring(0, sentence.length()-1);
process();
private void process()
String word[] = sentence.split(" ");
length=word.length;
sort(word);
output="";
for( String w:word)
output+=w+" ";
output=output.trim();
private void sort(String word[])
String temp;
boolean swapped;
for( int pass=0; pass<word.length-1;pass++)
swapped=false;
for( int pos=0; pos<word.length-1-pass;pos++)
if(word[pos+1].compareTo(word[pos])<0)
{
temp=word[pos+1];
word[pos+1]=word[pos];
word[pos]=temp;
swapped=true;
if(!swapped)
break;
public int getLength()
return length;
public String toString()
return output;
public class ISC2012Q2
public static void main(String[] args)
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\n\t");
String sentence = in.nextLine();
Sentence obj = new Sentence(sentence);
System.out.println("OUTPUT:\n\tLENGTH IS: "+obj.getLength());
System.out.println("\tREARRANGED SENTENCE");
System.out.println("\t"+obj);
in.close();
3.
import java.util.*;
class Matrix
private int A[][], M, N, minRow, minCol, maxRow, maxCol, min, max;
public Matrix(int M, int N)
A = new int[M][N];
this.M = M;
this.N = N;
public void getInput()
Scanner in = new Scanner(System.in);
for (int row = 0; row < M; row++)
for (int col = 0; col < N; col++)
A[row][col] = in.nextInt();
in.close();
public void computeMinimumPosition(int startRow, int startCol)
min = A[startRow][startCol];
minRow = startRow;
minCol = startCol;
for (int row = startRow; row < M; row++)
for (int col = startCol; col < N; col++)
startCol = 0;
if (A[row][col] < min)
min = A[row][col];
minRow = row;
minCol = col;
public void computeMaxiumPosition(int startRow, int startCol)
max = A[startRow][startCol];
maxRow = startRow;
maxCol = startCol;
for (int row = startRow; row < M; row++)
for (int col = startCol; col < N; col++)
startCol=0;
if (A[row][col] > max)
max = A[row][col];
maxRow = row;
maxCol = col;
}
}
public String toString()
String output = "";
for (int row=0;row<M;row++)
output += "\t";
for (int col=0;col<N;col++)
output += A[row][col];
if (col<N-1)
output += "\t";
output += "\n";
return output;
public void showMaxDetails()
computeMaxiumPosition(0,0);
System.out.println("LARGEST NUMBER: " + max);
System.out.println("\tROW = " + maxRow);
System.out.println("\tCOLUMN = " + maxCol);
public void showMinDetails()
computeMinimumPosition(0,0);
System.out.println("SMALLEST NUMBER: " + min);
System.out.println("\tROW = " + minRow);
System.out.println("\tCOLUMN = " + minCol);
}
public void sortAscending()
int temp;
for (int row=0;row<M;row++)
for (int col=0;col<N;col++)
computeMinimumPosition(row, col);
temp = A[minRow][minCol];
A[minRow][minCol] = A[row][col];
A[row][col] = temp;
public class ISC2012Q3
public static void main(String[] args)
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\tM = ");
int M = in.nextInt();
System.out.print("\tN = ");
int N = in.nextInt();
if (M > 2 && N > 2 && M < 20 && N < 20)
Matrix obj = new Matrix(M,N);
obj.getInput();
System.out.println("OUPUT:");
System.out.println("ORIGINAL MATRIX");
System.out.println(obj);
obj.showMaxDetails();
obj.showMinDetails();
obj.sortAscending();
System.out.println("REARRANGED MATRIX");
System.out.println(obj);
else
System.out.println("OUTPUT:\n\tSIZE OUT OF RANGE");
in.close();
//2012
1.
import java.util.*;
class Number
private int n;
private String answer;
public Number(int n)
this.n = n;
answer ="";
convertToWords();
public void convertToWords()
String arr1[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX","SEVEN", "EIGHT", "NINE",
"TEN", "ELEVEN", "TWELVE","THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN",
"EIGHTEEN", "NINETEEN", "TWENTY", "THIRTY", "FOURTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY",
"NINETY" };
String arr2[]={ "TWENTY","THIRTY","FORTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"};
int factor[] = { 1000000000, 10000000, 100000, 1000, 100, 1 };
String units[] = { "ARAB", "CRORE", "LAKHS", "THOUSAND", "HUNDRED", "" };
int quotient, number = n, ones, tens;
for (int i = 0; i < factor.length; i++)
quotient = number / factor[i];
if (quotient > 0)
if (quotient < 20)
answer = answer + arr1[quotient - 1] + " ";
else
ones = quotient % 10;
tens = quotient / 10;
if (ones != 0)
answer += arr2[tens - 2] + " "+ arr1[ones - 1] + "";
else
answer += arr2[tens - 2] +"";
number = number % factor[i];
if(number ==0)
break;
}
public String toString()
return answer.trim();
class ISC2011Q1
public static void main(String[] args)
Scanner in=new Scanner(System.in);
System.out.print("INPUT:\t");
int n=in.nextInt();
if(n>0&&n<1000)
Number obj=new Number(n);
System.out.println("OUTPUT:\t"+obj);
else
System.out.println("OUTPUT: OUT OF RANGE");
in.close();
2.
import java.util.*;
class Encrypt
{
private String sentence[],answer;
public Encrypt(String sentence[])
this.sentence = sentence.clone();
answer = "";
encrypt();
private void encrypt()
int lineCount = 0;
char ch;
String output, words[];
for (String line : sentence)
output ="";
lineCount++;
line = line.substring(0, line.length() - 1);
if (lineCount % 2 == 1)
for (int position = 0; position < line.length(); position++)
ch = line.charAt(position);
if (Character.isLetter(ch))
ch = (char) (ch + 2);
if (ch > 'Z')
ch = (char) (ch - 26);
output=output+ch;
}
else
words=line.split(" ");
for(String word : words)
output=word+ " "+output;
output=output.trim();
output+=".";
answer+=output+"\n";
public String toString()
return answer;
class ISC2011Q2
public static void main(String args[])
Scanner in=new Scanner(System.in);
System.out.print("INPUT:\tn=");
int n=in.nextInt();
in.nextLine();
if (n > 1 && n < 10)
String sentence[] = new String[n];
for (int i = 0; i < n; i++)
{
sentence[i] = in.nextLine();
Encrypt obj = new Encrypt(sentence);
System.out.println("OUTPUT:");
System.out.println(obj);
else
System.out.println("OUTPUT\t:INVALID ENTRY");
in.close();
3.
import java.util.*;
class MyDate
private int dd, mm, yyyy;
private static int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
public MyDate(int dd, int mm, int yyyy)
this.dd = dd;
this.mm = mm;
this.yyyy = yyyy;
if (isLeap())
days[1] = 29;
public boolean isLeap()
int divisor = (yyyy % 100 == 0) ? 400 : 4;
return yyyy % divisor == 0;
}
public boolean isValidDate()
if (yyyy < 0)
return false;
if (mm < 0 || mm > 12)
return false;
if (dd < 1 || dd > days[mm - 1])
return false;
return true;
public int getDayNumber()
int dayNumber = dd;
for (int i = 0; i < mm - 1; i++)
dayNumber += days[i];
return dayNumber;
class ISC2011Q3
public static void main(String[] args)
Scanner in = new Scanner(System.in);
System.out.println("INPUT:\tEnter your date of birth in dd mm yyyy format");
int dd, mm, yyyy;
System.out.print("\t");
dd = in.nextInt();
System.out.print("\t");
mm = in.nextInt();
System.out.print("\t");
yyyy = in.nextInt();
MyDate obj = new MyDate(dd, mm, yyyy);
if (obj.isValidDate())
System.out.println("OUTPUT:\n\tVALID DATE\n\t" + obj.getDayNumber());
else
System.out.println("Output:\n\tINVALID DATE");
in.close();