18 Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 60

/**Write a Program in Java to fill a square matrix of size ‘n*n” in a

circular fashion (clockwise) with natural numbers from 1 to n*n, taking ‘n’

as input.For example: if n = 4, then n*n = 16, hence the array will be

filled as given below.Note: This program is also known as Spiral Matrix*/

import java.util.*;

class Circular_Matrix

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.print("Enter the size: ");

int n = sc.nextInt();

int A[][] = new int[n][n];

int k=1, c1=0, c2=n-1, r1=0, r2=n-1;

while(k<=n*n)

for(int i=c1;i<=c2;i++)

A[r1][i]=k++;

for(int j=r1+1;j<=r2;j++)

A[j][c2]=k++;

for(int i=c2-1;i>=c1;i--)

A[r2][i]=k++;

}
for(int j=r2-1;j>=r1+1;j--)

A[j][c1]=k++;

c1++;

c2--;

r1++;

r2--;

/* Printing the Circular matrix */

System.out.println("The Circular Matrix is:");

for(int i=0;i<n;i++)

for(int j=0;j<n;j++)

System.out.print(A[i][j]+ "\t");

System.out.println();

/*We will take a variable ‘k’ which will begin with 1 and

will do the work of filling. i.e. for every cell, it will

increase by 1. The below given processes will repeat till the

value of ‘k’ becomes ‘n*n’

•c1 denotes the index of the column from where we have to begin.

Hence its initial value will be 0.

•C2 denotes the index of the column where we have to end.

Hence its initial value will be ‘n-1’

(n is the size of the matrix).


•R1 denotes the index of the row from where we have to begin.

Hence its initial value will be 0.

•R2 denotes the index of the row where we have to end.

Hence its initial value will be ‘n-1’

(n is the size of the matrix).

The filling up of the matrix in circular fashion will consist

of 4 different steps which will continue till the matrix is

filled completely.

Step 1: We will fill the elements of Row 0 (R1),

starting from Column 0 (C1) till ‘n-1’ (C2).

The cells which will be filled are marked in the image above

in yellow color.

The elements will be accessed as follows: A[R1][i],

where ‘i’ will go from C1 to C2 (A[ ][ ] is the array)

Step 2: Now, we will fill the elements of Column ‘n-1’ (C2),

starting from Row R1+1 till R2. (The cells which will be filled

are marked in the image above in grey color).

The elements will be accessed as follows: A[j][C2],

where ‘j’ will go from R1+1 to R2 (A[ ][ ] is the array)

Step 3: Next we will fill the elements of Row ‘n-1’ (R2),

starting from Column C2-1 till C1. (The cells which will be

filled are marked in the image above in green color.)

The elements will be accessed as follows: A[R2][i], where ‘i’

will go from C2-1 to C1 (A[ ][ ] is the array)

Step 4: Now, we will fill the elements of Column C1,

starting from Row R2-1 till R1+1. (The cells which will be

filled are marked in the image above in blue color.)

The elements will be accessed as follows: A[j][C1],


where ‘j’ will go from R2-1 to R1+1 (A[ ][ ] is the array)

The above 4 steps will now repeat with the inner matrix.

For the inner matrix,

C1 will increase by 1 i.e. it will be C1+1.

C2 will decrease by 1 i.e. it will be C2-1.

R1 will increase by 1 i.e. it will be R1+1.

R2 will decrease by 1 i.e. it will be R2-1.

The above processes will repeat till we have filled in

‘n*n’ values.

*/

/**Wondrous Square..

A wondrous square is an n by n grid which fulfils the

following conditions:

(i) It contains integers from 1 to n^2, where each

integer appears only once.

(ii) The sum of integers in any row or column must

add up to 0.5 * n * (n^2 + 1).

For example the following grid is a wondrous square

where the sum of each row or column is 65 when n = 5 :

17 24 2 8 15

23 5 7 14 16

4 6 13 20 22

10 12 19 21 3

11 18 25 2 9

Write a program to read n (2 < = n < = 10) and the values stored

in these n by n cells and output if the grid represents a

wondrous square or not.Also output all the prime numbers in

the grid along with their

row index and column index as shown in the output.

A natural number is said to be prime if it has exactly two


divisors.

E.g. 2, 3, 5, 7, 11,.......

The first element of the given grid i.e. 17 is stored at row

index 0 and column index

0 and the next element in the row i.e. 24 is stored at row

index 0 and column index 1.

Test your program for the following data and some random data.

SAMPLE DATA:

INPUT : N=4

16 15 1 2

6 4 10 14

9 8 12 5

3 7 11 13

OUTPUT: YES IT REPRESENTS A WONDROUS SQUARE.

PRIME ROW INDEX COLUMN INDEX

2 0 3

3 3 0

5 2 3

7 3 1

11 3 2

13 3 3

*/

import java.util.*;

class wonderous

int arr[][],arr1[];

int n,i,j,x=0,r,c;
int flag;

void perform()

Scanner sc=new Scanner(System.in);

System.out.println("Enter the size of array(row and column same):");

n=sc.nextInt();

if(n<=2||n>=10)

System.out.println("Invalid Range");

System.exit(0);

arr=new int[n][n];

arr1=new int[2*n];

for(i=0;i<n;i++)

for(j=0;j<n;j++)

System.out.print("Enter the value:");

arr[i][j]=sc.nextInt();

System.out.println("The matrix is");

for(i=0;i<n;i++)

r=0;

c=0;

for(j=0;j<n;j++)

System.out.print(arr[i][j]+" ");

r=r+arr[i][j];

c=c+arr[j][i];
}

System.out.println();

arr1[x]=r;

arr1[x+n-1]=c;

x++;

for(i=0;i< x;i++)

if(arr1[i]!= 0.5 * n * (n*n + 1))

break;

if(i==x)

System.out.println("YES IT REPRESENTS A WONDROUS SQUARE.");

else

System.out.println("IT IS NOT A WONDROUS SQUARE.");

System.out.println("PRIME ROW COLUMN");

for(i=0;i< n;i++)

for(j=0;j< n;j++)

if(prime(arr[i][j]))

System.out.println(arr[i][j]+ " "+i+ " "+j);

boolean prime(int no)

{
int count=0,index;

for(index=2;index<no;index++)

if(no%index==0)

count=count+1;

if(count==0)

return true;

else

return false;

public static void main(String args[])

wonderous ob=new wonderous();

ob.perform();

import java.util.*;

class ISC2018Q3

public static void main()

Scanner sc = new Scanner(System.in);

String ar[];

int n, i, j;

System.out.println("Enter the number of names: ");

n = sc.nextInt();

ar = new String[n];

System.out.println("Enter the names: ");

for(i=0;i<n;i++)
{

ar[i] = sc.nextLine();

int max = 0;

for(i=0;i<n;i++)

if(max<ar[i].length())

max = ar[i].length();

//Finding the length of the longest string

System.out.println("OUTPUT:" );

for(i=0;i<max;i++)

for(j=0;j<n;j++)

/*character will be extracted only

when i has a value less

than the length of the string else

only tabular space will be printed

*/

if(i<ar[j].length())

System.out.print(ar[j].charAt(i)+"\t");

else

System.out.print("\t");

System.out.println();

}
/**Question:

Write a program to input a natural number less than 1000

and display it in words. [Note we have solved the program

for numbers in the range [1-9999]

Test your program for the given sample data and some random data.

Sample Data:

Input: 29

Output: TWENTY NINE

Input: 17001

Output: OUT OF RANGE

Input: 119

Output: ONE HUNDRED AND NINETEEN

Input: 500

Output: FIVE HUNDRED

*/

import java.io.*;

class Num2Word_ISC2011

public static void main(String args[]) throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String ty[]={"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
String ten[]={"","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen",

"Eighteen","Nineteen"};

String unit[]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};

System.out.print("Enter a Number : ");

int n=Integer.parseInt(br.readLine());

/*checking whether the number is in the range [1-9999] or not*/

if(n<1 || n>9999)

System.out.println("Out of Range");

else

int th=n/1000; //finding the digit at thousand's place

int h=(n/100)%10; //finding the digit at hundred's place

int t=(n/10)%10; //finding the digit at ten's place

int u=n%10; //finding the digit at unit's place

System.out.print("Output = ");

/*Condition for printing digit at thousand's place,

is that it should not be zero*/

if(th!=0)

System.out.print(unit[th]+" Thousand");

/*Condition for printing digit at hundred's place, is that it should not be zero*/

if(h!=0)

System.out.print(" "+unit[h]+" Hundred");

/*Condition for printing the word "And"*/


if((t!=0 || u!=0)&&(th!=0 || h!=0))

System.out.print(" And");

/*Condition for printing digit at ten's place*/

if(t==1)

//When digit at ten's place is 1, we have different words like Ten, Eleven etc.

System.out.print(" "+ten[u+1]);

else

//if it is not 1 then we print the words following a normal pattern

System.out.print(" "+ty[t]+" "+unit[u]);

/*Output:

1. Enter a Number : 129

Output = One Hundred And Twenty Nine

2. Enter a Number : 8307

Output = Eight Thousand Three Hundred And Seven

3. Enter a Number : 54987

Out of range*/

/**A Smith number is a composite number, the sum of whose

digits is the sum of the digits of its prime factors obtained

as a result of prime factorization (excluding 1).

The first few such numbers are 4, 22, 27, 58, 85, 94,

121 ………………..

Examples:

1. 666
Prime factors are 2, 3, 3, and 37

Sum of the digits are (6+6+6) = 18

Sum of the digits of the factors (2+3+3+(3+7)) = 18

2. 4937775

Prime factors are 3, 5, 5, 65837

Sum of the digits are (4+9+3+7+7+7+5) = 42

Sum of the digits of the factors (3+5+5+(6+5+8+3+7)) = 42

Write a program to input a number and display whether the

number is a Smith number or not.

Sample data:

Input 94 Output SMITH Number

Input 102 Output NOT SMITH Number

Input 666 Output SMITH Number

Input 999 Output NOT SMITH Number

*/

import java.util.*;

class Smith

//function for finding sum of digits

int sumDig(int n)

int d,s=0;
while(n>0)

d=n%10;

s=s+d;

n=n/10;

return s;

//function for generating prime factors and finding their sum

int sumPrimeFact(int n)

int i=2, sum=0;

while(n>1)

if(n%i==0)

sum=sum+sumDig(i);

//Here'i'is the prime factor of 'n' and we are finding its sum

n=n/i;

else

i++;

return sum;

public void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter a Number : ");

int n=sc.nextInt();

Smith ob=new Smith();


int a=ob.sumDig(n);// finding sum of digit

int b=ob.sumPrimeFact(n); //finding sum of prime factors

System.out.println("Sum of Digit = "+a);

System.out.println("Sum of Prime Factor = "+b);

if(a==b)

System.out.println("It is a Smith Number");

else

System.out.println("It is Not a Smith Number");

/**Output:

1. Enter a Number : 94

Sum of Digit = 13

Sum of Prime Factor = 13

It is a Smith Number

2. Enter a Number : 102

Sum of Digit = 3

Sum of Prime Factor = 13

It is Not a Smith Number

3. Enter a Number : 4937775

Sum of Digit = 42

Sum of Prime Factor = 42

It is a Smith Number

*/

import java.io.*;

class Date_Difference

{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

//function for checking for Leap Year

int isLeap(int y)

if((y%400==0) || ((y%100!=0)&&(y%4==0)))

return 29;

else

return 28;

//function for checking date validation

boolean dateValidate(int d, int m, int y)

month[2]=isLeap(y);

if(m<0 || m>12 || d<0 || d>month[m] || y<0 || y>9999)

return false;

else

return true;

/*function for finding day number from year = 1 till

the inputted year

*/

int dayno(int d, int m, int y)

int dn=0;
month[2]=isLeap(y);

for(int i=1;i<y;i++)

if(isLeap(i)==29)

dn=dn+366;

else

dn=dn+365;

for(int i=1;i<m;i++)

dn=dn+month[i];

dn=dn+d;

return dn;

public static void main(String args[])throws IOException

Date_Difference ob=new Date_Difference();

System.out.print("Enter the 1st date in (dd/mm/yyyy) format: ");

String date1=br.readLine().trim();

int p,q;

//Extracting the day

p=date1.indexOf("/");

int d1=Integer.parseInt(date1.substring(0,p));

//Extracting the month

q=date1.lastIndexOf("/");

int m1=Integer.parseInt(date1.substring(p+1,q));
//Extracting the year

int y1=Integer.parseInt(date1.substring(q+1));

System.out.print("Enter the 2nd date in (dd/mm/yyyy) format: ");

String date2=br.readLine().trim();

p=date2.indexOf("/");

int d2=Integer.parseInt(date2.substring(0,p));

q=date2.lastIndexOf("/");

int m2=Integer.parseInt(date2.substring(p+1,q));

int y2=Integer.parseInt(date2.substring(q+1));

//Validating both the dates

if(ob.dateValidate(d1,m1,y1)==true && ob.dateValidate(d2,m2,y2)==true)

int a=ob.dayno(d1,m1,y1);

int b=ob.dayno(d2,m2,y2);

System.out.print("Output : Difference = "+Math.abs(a-b)+" days.");

else

System.out.println("Invalid Date");

/**Write a Program in Java to input a number and check whether

it is a Keith Number or not.

Note:A Keith Number is an integer N with ‘d’ digits with the

following property:

If a Fibonacci-like sequence (in which each term in the

sequence is the sum of the ‘d’ previous terms) is formed, with


the first ‘d’ terms being the decimal digits of

the number N,then N itself occurs as a term in the sequence.

For example, 197 is a Keith number since it generates the

sequence 1, 9, 7, 17, 33, 57, 107, 197, ………..

Some keith numbers are: 14 ,19, 28 , 47 , 61, 75, 197, 742, 1104,1537……………

*/

import java.io.*;

class Keith

public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the number : "); //inputting the number

int n=Integer.parseInt(br.readLine());

int copy=n;

String s=Integer.toString(n);

int d=s.length();

//finding the number of digits (d) in the number

int arr[]=new int[n];

//array for storing the terms of the series

for(int i=d-1; i>=0; i--)

arr[i]=copy%10;

//storing the digits of the number in the array

copy=copy/10;

int i=d,sum=0;

while(sum<n)

//finding the sum till it is less than the number

sum = 0;
for(int j=1; j<=d; j++)

//loop for generating and adding the previous 'd' terms

sum=sum+arr[i-j];

arr[i]=sum; //storing the sum in the array

i++;

/* When the control comes out of the while loop, either the

sum is equal to the number or greater than it */

if(sum==n) //if sum is equal to the number, then it is a Keith number

System.out.println("The number is a Keith Number");

else

System.out.println("The number is a not a Keith Number");

/**Output:

Enter the number : 197

The number is a Keith Number

Enter the number : 14

The number is a Keith Number

Enter the number : 53

The number is a not a Keith Number

*/

/** Write a program to accept a sentence as input. The words

in the string are to be separated by a blank. Each word must

be in upper case. The sentence is terminated by either ".",

"!" or "?". Perform the following tasks:


(i) Obtain the length of the sentence. (measured in words)

(ii) Arrange the sentence in alphabetical order of the words.

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

Example 1:

INPUT: NECESSITY IS THE MOTHER OF INVENTION.

OUTPUT:Length: 6

Rearranged Sentence:

INVENTION IS MOTHER NECESSITY OF THE

Example 2:

INPUT: BE GOOD TO OTHERS.

OUTPUT:

Length: 4

Rearranged Sentence: BE GOOD OTHERS TO

*/

import java.io.*;

class WordsInSentence

public void take() throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str, words[], stk="";

int i,j,c=0,flag, len;

char ch;

while(true)

{
flag=0;

System.out.println("Enter the sentence:");

str=br.readLine();

len= str.length();

words=new String[len];

for(i=0;i<len;i++)

if(Character.isLowerCase(str.charAt(i)))

flag=1;

break;

if (flag==0)

break;

else

System.out.println("Enter the sentence again with all uppercase letters");

i=0;

while(i<len)

ch=str.charAt(i);

if(ch==' '|| ch=='.' || ch=='?' || ch=='!')

words[c]=stk;

c++;

i++;

stk="";

else

{
stk+=ch;

i++;

for(i=0;i<c;i++)

for(j=0;j<c-1;j++)

if((words[j].compareTo(words[j+1]))>0)

stk=words[j];

words[j]=words[j+1];

words[j+1]=stk;

System.out.println("Length= "+c);

System.out.println("\nRearranged Sentence:\n");

for(i=0;i<c;i++)

System.out.print(words[i]+" ");

public static void main(String args[]) throws IOException

WordsInSentence ob=new WordsInSentence();

ob.take();

/**Write a program to declare a square matrix A[ ] [ ]

of order (M x M) where ‘M’ is the number of rows and the

number of columns such that M must be greater than 2 and

less than 20. Allow the user to input integers into


this matrix. Display appropriate error message for an

invalid input. Perform the following tasks:

(a) Display the input matrix.

(b) Create a mirror image matrix.

(c) Display the mirror image matrix.

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

Example 1

INPUT : M = 3

4 16 12

8 2 14

4 1 3

OUTPUT :

ORIGINAL MATRIX

4 16 12

8 2 14

4 1 3

MIRROR IMAGE MATRIX

12 16 4

14 2 8

3 1 4

Example 2

INPUT : M = 22
OUTPUT : SIZE OUT OF RANGE

*/

import java.io.*;

class ImageMatrix_ISC2013

public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the size of the square matrix : ");

int m=Integer.parseInt(br.readLine());

if(m>2 && m<20) //checking given condition

int A[][]=new int[m][m];

System.out.println("Enter the elements of the Matrix : ");

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

A[i][j]=Integer.parseInt(br.readLine());

System.out.println("*********************");

System.out.println("The original matrix:");

System.out.println("*********************");

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

{
System.out.print(A[i][j]+"\t");

System.out.println();

// creating the Image Matrix

int B[][]=new int[m][m];

for(int i=0;i<m;i++)

int k=0;

for(int j=m-1;j>=0;j--)

B[i][k]=A[i][j];

k++;

//Printing resulted the Matrix

System.out.println("*********************");

System.out.println("The Mirror Image:");

System.out.println("*********************");

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

System.out.print(B[i][j]+"\t");

System.out.println();

else

System.out.println("Output : Size Out Of Range");

}
}

/*Output:

Enter the size of the square matrix : 4

Enter the elements of the Matrix:

10

11

12

13

14

15

16

*******

The original matrix:

*******

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

*******

The Mirror Image:

*******

4 3 2 1
8 7 6 5

12 11 10 9

16 15 14 13

*/

/**A Composite Magic number is a positive integer which is

composite as well as a magic number.

Composite number:

A composite number is a number that has more than two factors.

For example: 10

Factors are: 1, 2, 5, 10

Magic number:

A magic number is a number in which the eventual sum of the

digits is equal to 1

For example: 28=2+8=10=1+0=1

Accept two positive integers m and n, where m is less than n as

user input.Display the number of Composite magic integers that

are in the range between m and n (both inclusive) and output

them along with the frequency, in the format specified below.

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

Example 1:

INPUT:

m = 10

n = 100

OUTPUT:

THE COMPOSITE MAGIC INTEGERS ARE:

10, 28, 46, 55, 64, 82, 91, 100


FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 8

Example 2:

INPUT:

m = 1200

n = 1300

OUTPUT:

THE COMPOSITE MAGIC INTEGERS ARE:

1207, 1216, 1225, 1234, 1243, 1252, 1261, 1270, 1288

FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 9

Example 3:

INPUT:

m = 120

n = 99

OUTPUT:

INVALID INPUT

*/

import java.io.*;

class MagicComposite_ISC2014

boolean isComposite(int n)

// Function to check for Composite number


{

int count=0;

for(int i=1;i<=n;i++)

if(n%i==0)

count++;

if(count>2)

return true;

else

return false;

int sumDig(int n)

// Function to return sum of digits of a number

int d,s = 0;

while(n>0)

d=n%10;

s = s +d;

n = n/10;

return s;

boolean isMagic(int n) // Function to check for Magic number

int a = sumDig(n);

while(a>9)

a = sumDig(a);

}
if(a == 1)

return true;

else

return false;

public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader

(new InputStreamReader(System.in));

System.out.print("Enter the lower limit(m) : ");

int m=Integer.parseInt(br.readLine());

System.out.print("Enter the upper limit(n) : ");

int n=Integer.parseInt(br.readLine());

MagicComposite_ISC2014 ob = new MagicComposite_ISC2014();

int c=0;

if (m<n)

System.out.println("The Composite Magic Integers are: ");

for(int i=m; i<=n; i++)

if(ob.isComposite(i)==true && ob.isMagic(i)==true)

if (c==0)

// Printing the first number without any comma

System.out.print(i);

else

System.out.print(", "+i);

c++;

System.out.println("\nThe frequency of Composite Magic Integers is : "+c);


}

else

System.out.println("OUT OF RANGE");

/*Output:

Enter the lower limit(m) : 1200

Enter the upper limit(n) : 1300

The Composite Magic Integers are:

1207, 1216, 1225, 1234, 1243, 1252, 1261, 1270, 1288

The frequency of Composite Magic Integers is : 9

*/

/**An ISBN (International Standard Book Number) is a ten digit

code which uniquely identifies a book.

The first nine digits represent the Group, Publisher and

Title of the book and the last digit is used to check

whether ISBN is correct or not.

Each of the first nine digits of the code can take a

value between 0 and 9. Sometimes it is necessary to

make the last digit equal to ten; this is done by

writing the last digit of the code as X.

To verify an ISBN, calculate 10 times the first digit,

plus 9 times the second digit, plus 8 times the third

and so on until we add 1 time the last digit.

If the final number leaves no remainder when divided


by 11, the code is a valid ISBN.

For example:

1. 0201103311:

10 * 0 + 9 * 2 + 8 * 0 + 7 * 1 + 6 * 1 + 5 * 0 + 4 * 3 +

3 * 3 + 2 * 1 + 1 * 1 = 55.

Since 55 leaves no remainder when divided by 11,

hence it is a valid ISBN.

2. 007462542X:

10 * 0 + 9 * 0 + 8 * 7 + 7 * 4 + 6 * 6 + 5 * 2 + 4 * 5 + 3

* 4 + 2 * 2 + 1 * 10 = 176.

Since 176 leaves no remainder when divided by 11, hence it is

a valid ISBN.

3. 0112112425:

10 * 0 + 9 * 1 + 8 * 1 + 7 * 2 + 6 * 1 + 5 * 1 + 4 * 2 + 3 * 4 + 2 * 2 + 1 * 5 = 71.

Since 71 leaves a remainder when divided by 11, hence it is not

a valid ISBN.

Design a program to accept a ten digit code from the user.

For an invalid input, display an appropriate message.

Verify the code for its validity in the format specified below:

Test your program with sample data and some random data.

Example 1:

INPUT CODE: 0201530821

OUTPUT:

SUM = 99

LEAVES NO REMAINDER – VALID ISBN CODE

Example 2:
INPUT CODE: 035680324

OUTPUT: INVALID INPUT

Example 3:

INPUT CODE: 0231428031

OUTPUT:

SUM = 122

LEAVES REMAINDER – INVALID ISBN CODE

*/

import java.io.*;

class ISBN

public static void main(String args[])throws IOException

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("ISBN Code: ");

String isbn = br.readLine();

isbn = isbn.trim().toUpperCase();

int len = isbn.length();

char last = isbn.charAt(len - 1);

int sum = 0;

int n = 10;

if(len != 10)

System.out.println("INVALID INPUT");

return;

if(!(last >= '0' && last <= '9') && last != 'X')
{

System.out.println("INVALID INPUT");

return;

boolean wrong = false;

for(int i = 0; i < len - 1; i++)

char ch = isbn.charAt(i);

if(!(ch >= '0' && ch <= '9'))

wrong = true;

break;

else

int d = Integer.parseInt(Character.toString(ch));

sum += d * n;

n--;

if(wrong)

System.out.println("INVALID INPUT");

return;

if(last == 'X')

sum += 10;

else

sum += Integer.parseInt(Character.toString(last));
System.out.println("SUM = " + sum);

if(sum % 11 == 0)

System.out.println("LEAVES NO REMAINDER - VALID ISBN CODE");

else

System.out.println("LEAVES REMAINDER - INVALID ISBN CODE");

/**Write a program to accept a sentence which may be terminated by

either ‘.’ ‘?’ or ‘!’ only. Any other character may be ignored.

The words may be separated by more than one blank space and are in

UPPER CASE.

Perform the following tasks:

(a)Accept the sentence and reduce all the extra blank space between

two words to a single blank space.

(b)Accept a word from the user which is part of the sentence along

with its position number and delete the word and display the sentence.

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

Example 1

INPUT: A MORNING WALK IS A IS BLESSING FOR THE WHOLE DAY.

WORD TO BE DELETED: IS

WORD POSITION IN THE SENTENCE: 6

OUTPUT: A MORNING WALK IS A BLESSING FOR THE WHOLE DAY.


Example 2

INPUT: AS YOU SOW, SO SO YOU REAP.

WORD TO BE DELETED: SO

WORD POSITION IN THE SENTENCE: 4

OUTPUT: AS YOU SOW, SO YOU REAP.

Example 3

INPUT: STUDY WELL ##

OUTPUT: INVALID INPUT.

*/

import java.util.*;

class RemoveWord_ISC2014

public static void main (String args[])

Scanner sc = new Scanner(System.in);

System.out.print("Enter a sentence : ");

String s = sc.nextLine();

s = s.toUpperCase();

int l = s.length();

char last = s.charAt(l-1);

// Extracting the last character

/*checking whether the sentence ends with '.' or '?' or not*/


if(last != '.' && last != '?' && last != '!')

System.out.println("Invalid Input. End a sentence with either '.', '?' or '!' only");

else

StringTokenizer str = new StringTokenizer(s," .?!");

int c = str.countTokens();

String w="",ans = "";

System.out.print("Enter the word to delete : ");

String del = sc.next().toUpperCase();;

System.out.print("Enter the word position in the sentence : ");

int x = sc.nextInt();

if(x<1 || x>c)

// Checking whether integer inputted is acceptable or not

System.out.println("Sorry! The word position entered is out of range");

else

for(int i=1; i<=c; i++)

w = str.nextToken();

/* Skipping if the word to delete and the position matches */

if(w.equals(del)==true && i == x)

continue;

ans = ans + w + " ";

}
System.out.print("Output : "+ans.trim()+last);

/*Output:

1. Enter any sentence : A MORNING WALK IS A IS BLESSING FOR THE WHOLE DAY.

Enter the word to delete : IS

Enter the word position is the sentence : 6

Output : A MORNING WALK IS A BLESSING FOR THE WHOLE DAY.

2. Enter any sentence : STUDY WELL ##

OUTPUT : Invalid Input. End a sentence with either

‘.’, ‘?’ or ‘!’

*/

/**Question:

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 than 11.

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:

Note: Array entries are line fed (i.e. one entry per line)

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

Example 1

INPUT : N = 5

Participant 1 D A B C C

Participant 2 A A D C B

Participant 3 B A C D B

Participant 4 D A D C B

Participant 5 B C A D D

Key: B C D A A

OUTPUT : Scores :

Participant 1 = 0

Participant 2 = 1

Participant 3 = 1

Participant 4 = 1

Participant 5 = 2

Highest score: Participant 5

Example 2

INPUT : N = 4

Participant 1 A C C B D

Participant 2 B C A A C

Participant 3 B C B A A

Participant 4 C C D D B

Key: A C D B B

OUTPUT : Scores :

Participant 1 = 3

Participant 2 = 1
Participant 3 = 1

Participant 4 = 3

Highest score:

Participant 1

Participant 4

Example 3

INPUT : N = 12

OUTPUT : INPUT SIZE OUT OF RANGE.

*/

import java.util.*;

class QuizResult_ISC2017

char A[][],K[];

int S[],n;

void input()

Scanner sc = new Scanner(System.in);

System.out.print("Enter number of participants : ");

n = sc.nextInt();

if(n<4 || n>10)

System.out.println("INPUT SIZE OUT OF RANGE");

System.exit(0);

A = new char[n][5];

// Array to store the answers of every participants

K = new char[5]; // Array to store answer key

S = new int[n];

// Array to store score of every participant


System.out.println("\n Enter answer of each participant row-wise in a single line\n" );

for(int i = 0; i<n; i++)

System.out.print("Participant "+(i+1)+" : ");

for(int j=0; j<5; j++)

A[i][j] = sc.next().charAt(0);

System.out.print("\nEnter Answer Key : ");

for(int i = 0; i<5; i++)

K[i] = sc.next().charAt(0);

void CalcScore()

// Function to calculate score of every participant

for(int i = 0; i<n; i++)

S[i] = 0;

for(int j=0; j<5; j++)

if(A[i][j] == K[j])

/*Checking if Answer of the participants match

with the key or not*/

S[i]++;

}
}

void printScore()

int max = 0;

System.out.println("\nSCORES : ");

for(int i = 0; i<n; i++)

System.out.println("\tParticipant "+(i+1)+" = "+S[i]);

if(S[i]>max)

max = S[i]; // Storing the Highest Score

System.out.println();

System.out.println("\tHighest Score : "+max);

for(int i=0;i<n;i++)

if(max==S[i])

System.out.println("\tParticipant is: "+ (i+1));

public static void main(String args[])

QuizResult_ISC2017 ob = new QuizResult_ISC2017();

ob.input();

ob.CalcScore();

ob.printScore();

/**Question:
Write a program to declare a square matrix A[][] of order (M x M)

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:

(a) Sort the non-boundary elements in ascending order using any

standard sorting technique and rearrange them in the matrix.

(b) Calculate the sum of both the diagonals.

(c) Display the original matrix,

rearranged matrix and only the diagonal elements

of the rearranged matrix with their sum.

Example 1

INPUT :M = 4

9 2 1 5

8 13 8 4

15 6 3 11

7 12 23 8

OUTPUT:

ORIGINAL MATRIX

9 2 1 5

8 13 8 4

15 6 3 11

7 12 23 8

REARRANGED MATRIX
9 2 1 5

8 3 6 4

15 8 13 11

7 12 23 8

DIAGONAL ELEMENTS

9 5

3 6

8 13

7 8

SUM OF THE DIAGONAL ELEMENTS = 59 */

import java.util.*;

class SortNonBoundary_ISC2016

int A[][],B[],m,n;

void input() //Function for taking all the necessary inputs

Scanner sc = new Scanner(System.in);

System.out.print("Enter the size of the square matrix : ");

m=sc.nextInt();

if(m<4 || m>10)

System.out.println("Invalid Range");

System.exit(0);

else

A = new int[m][m];
n = (m-2)*(m-2);

B = new int[n]; //Array to store Non-Boundary Elements

System.out.println("Enter the elements of the Matrix : ");

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

System.out.print("Enter a value : ");

A[i][j]=sc.nextInt();

/* The below function stores Non-Boundary elements

* from array A[][] to array B[] if s = 1

* else stores the Non-Boundary elements in array A[][]

* from array B[]

*/

void convert(int s)

int x=0;

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

if(i != 0 && j != 0 && i != m-1 && j != m-1)

if(s==1)

B[x] = A[i][j];
else

A[i][j] = B[x];

x++;

void sortArray()

//Function for sorting Non-Boundary elements stored in array B[]

int c = 0;

for(int i=0; i<n-1; i++)

for(int j=i+1; j<n; j++)

if(B[i]>B[j])

c = B[i];

B[i] = B[j];

B[j] = c;

void printArray()

//Function for printing the array A[][]

for(int i=0;i<m;i++)

{
for(int j=0;j<m;j++)

System.out.print(A[i][j]+"\t");

System.out.println();

void printDiagonal()

//Function for printing the diagonal elements and their sum

int sum = 0;

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

if(i==j || (i+j)==m-1)

System.out.print(A[i][j]+"\t");

sum = sum + A[i][j];

else

System.out.print("\t");

System.out.println();

System.out.println("Sum of the Diagonal Elements : "+sum);

public static void main(String args[])

{
SortNonBoundary_ISC2016 ob = new SortNonBoundary_ISC2016();

ob.input();

System.out.println("*********************");

System.out.println("The original matrix:");

System.out.println("*********************");

ob.printArray(); //Printing the original array

ob.convert(1);

//Storing Non-Boundary elements to a 1-D array

ob.sortArray();

//Sorting the 1-D array (i.e. Non-Diagonal Elements)

ob.convert(2);

/*Storing the sorted Non-Boundary

elements back to original 2-D array*/

System.out.println("*********************");

System.out.println("The Rearranged matrix:");

System.out.println("*********************");

ob.printArray(); //Printing the rearranged array

System.out.println("*********************");

System.out.println("The Diagonal Elements:");

System.out.println("*********************");

ob.printDiagonal();

//Printing the diagonal elements and their sum

/* Output:

Enter the size of the square matrix : 4

Enter the elements of the Matrix :

Enter a value : 9

Enter a value : 2

Enter a value : 1
Enter a value : 5

Enter a value : 8

Enter a value : 13

Enter a value : 8

Enter a value : 4

Enter a value : 15

Enter a value : 6

Enter a value : 3

Enter a value : 11

Enter a value : 7

Enter a value : 12

Enter a value : 23

Enter a value : 8

*********************

The original matrix:

*********************

9 2 1 5

8 13 8 4

15 6 3 11

7 12 23 8

*********************

The Rearranged matrix:

*********************

9 2 1 5

8 3 6 4

15 8 13 11

7 12 23 8

*********************

The Diagonal Elements:

*********************

9 5
3 6

8 13

7 8

Sum of the Diagonal Elements : 59*/

/**Question on Goldbach Number ISC 2018:A Goldbach number is a

positive even integer that can be expressed as the sum of two

odd primes.

Note: All even integer numbers greater than 4 are Goldbach

numbers.Example:

6=3+3

10 = 3 + 7

10 = 5 + 5

Hence, 6 has one odd prime pair 3 and 3. Similarly, 10 has two

odd prime pairs,i.e. 3, 7 and 5, 5.

Write a program to accept an even integer ‘N’where N > 9 and

N < 50. Find all the odd prime pairs whose sum is equal to

the number ‘N’.

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

Example 1:INPUT:N = 14

OUTPUT:Prime pairs are:3, 11 7, 7

Example 2:INPUT:N = 30

OUTPUT:Prime numbers are:

7, 23

11, 19

13, 17

Example 3:

INPUT:

N = 17

OUTPUT:

Invalid input. Number is odd.


Example 4:

INPUT:

N = 126

OUTPUT:

Invalid input. Number is out of range.

*/

import java.util.*;

class Goldbach

boolean isPrime(int n)

int f = 0;

for(int i = 1; i <= n; i++)

if(n % i == 0)

f++;

if(f == 2)

return true;

else

return false;

public void main()

Scanner sc=new Scanner(System.in);

int n;

int p = 3;

int q = 0;

System.out.print("Enter the Number = ");

n =sc.nextInt();

if(n % 2 != 0)
{

System.out.println("Invalid input. Number is odd.");

System.exit(0);

if(n < 10 || n > 49)

System.out.println("Invalid input. Number is out of range.");

System.exit(0);

System.out.println("Prime pairs are:");

while(p < n)

q = n - p;

if(isPrime(p)==true && isPrime(q)==true && p <= q)

System.out.println(p + ", " + q);

p =p+ 2;

/**Here is the algorithm:

Step 1: Input the number to be checked as N.

Step 2: LET P = 3

Step 3: If N MOD 2 != 0 then N is not a Goldbach Number.

Thus Exit.

Step 4: If N < 10 OR N > 49 then N is out of range.

Thus Exit.

Step 5: While P < N, Repeat Step 6 through 8:

Step 6: Let Q = N - P

Step 7: If P and Q are Prime and P <= Q

then Print P, Q
Step 8: Let P = P + 2*/

/**

Design a program to accept a day number (between 1

and 366),year (in 4 digits) from the user to generate and

display the corresponding date. Also, accept

N (1 <= N <= 100) from the user to compute and display

the future date corresponding to ‘N’ days after the

generated date. Display an error message if the value

of the day number, year and N are not within the

limit or not according to the condition specified.

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

Example 1:

INPUT:

DAY NUMBER: 255

YEAR: 2018

DATE AFTER (N DAYS): 22

OUTPUT:

DATE: 12TH SEPTEMBER, 2018

DATE AFTER 22 DAYS: 4TH OCTOBER, 2018

Example 2:

INPUT:

DAY NUMBER: 360

YEAR: 2018

DATE AFTER (N DAYS): 45

OUTPUT:
DATE: 26TH DECEMBER, 2018

DATE AFTER 45 DAYS: 9TH FEBRUARY, 2019

Example 3:

INPUT:

DAY NUMBER: 500

YEAR: 2018

DATE AFTER (N DAYS): 33

OUTPUT:

DAY NUMBER OUT OF RANGE

Example 4:

INPUT:

DAY NUMBER: 150

YEAR: 2018

DATE AFTER (N DAYS): 330

OUTPUT:

DATE AFTER (N DAYS) OUT OF RANGE

*/

import java.io.*;

class FindDate2019

public static void main(String args[])throws IOException

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("DAY NUMBER: ");

int dayNum = Integer.parseInt(br.readLine());


System.out.print("YEAR: ");

int year = Integer.parseInt(br.readLine());

System.out.print("DATE AFTER (N DAYS): ");

int n = Integer.parseInt(br.readLine());

if(dayNum < 1 || dayNum > 366)

System.out.println("DAY NUMBER OUT OF RANGE.");

if(n < 1 || n > 100)

System.out.println("DAY AFTER (N DAYS) OUT OF RANGE.");

if(year < 1000 || year > 9999)

System.out.println("INVALID YEAR.");

if(dayNum==366&&(isLeapYear(year)==false))

System.out.println("Not a Leap Year,Day Number out of range");

System.exit(0);

int m[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

if(isLeapYear(year))

m[1] = 29;
String mn[] = {"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST",
"SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"};

int i = 0;

while(dayNum - m[i] > 0)

dayNum= dayNum -m[i];

i++;

System.out.print("DATE: " + dayNum);

if((dayNum % 10 == 1 && dayNum < 10 )||(dayNum % 10 == 1 && dayNum > 11))

System.out.print("ST ");

else if((dayNum % 10 == 2 && dayNum < 10) ||(dayNum % 10 == 2 && dayNum > 12))

System.out.print("ND ");

else if((dayNum % 10 == 3 && dayNum < 10)||(dayNum % 10 == 3 && dayNum > 13))

System.out.print("RD ");

else

System.out.print("TH ");

System.out.println(mn[i] + ", " + year);

dayNum= dayNum +n;

while(i < 12 && dayNum - m[i] > 0)

dayNum =dayNum - m[i];

i++;

if(i==12&&dayNum > 0)

i = 0;

while(dayNum - m[i] > 0 && i < 12)

dayNum =dayNum - m[i];


i++;

year=year+1;

System.out.print("DATE AFTER " + n + " DAYS: " + dayNum);

if((dayNum % 10 == 1 && dayNum < 10 )||(dayNum % 10 == 1 && dayNum > 11))

System.out.print("ST ");

else if((dayNum % 10 == 2 && dayNum < 10) ||(dayNum % 10 == 2 && dayNum > 12))

System.out.print("ND ");

else if((dayNum % 10 == 3 && dayNum < 10)||(dayNum % 10 == 3 && dayNum > 13))

System.out.print("RD ");

else

System.out.print("TH ");

System.out.println(mn[i] + ", " + year);

public static boolean isLeapYear(int y)

if((y%400==0) || ((y%100!=0)&&(y%4==0)))

return true;

else

return false;

/** ISC theory 2018

Design a class Perfect to check if a given number is a

perfect number or not. A number is said to be perfect

if the sum of the factors of the number excluding itself

is equal to the original number.

Example:
6-- 1+2+3=6,where 1,2,and 3 are factors of 6,excluding itself.

Some of the members of the class are given below:

Class name: Perfect

Data members/instance variables:

num: to store the number

Methods/Member functions:

Perfect(int n): parameterized constructor to initialize

the data member num = n.

int sumOfFactors(int i): returns the sum of the factors

of the number (num), excluding itself, using recursive

technique.

void check(): checks whether the given number is perfect

by invoking the function sumOfFactors(int) and displays

the result with an appropriate message.

Specify the class Perfect, giving details of the constructor,

int sumOfFactors(int) and void check(). Define the main()

function to create an object and call the functions

accordingly to enable the task.

*/

import java.io.*;

class Perfect

int num;

Perfect(int n)

num = n;

}
int sumOfFactors(int i)

if(i == 1)

return 1 + sumOfFactors(i + 1);

else if(i < num && num % i == 0)

return i + sumOfFactors(i + 1);

else if(i < num && num % i != 0)

return 0 + sumOfFactors(i + 1);

else

return 0;

void check()

if(num == sumOfFactors(1))

System.out.println(num + " is Perfect.");

else

System.out.println(num + " is not Perfect.");

public static void main(String args[])throws IOException

InputStreamReader in = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(in);

System.out.print("Enter the Number: ");

int n = Integer.parseInt(br.readLine());

Perfect obj = new Perfect(n);

obj.check();

You might also like