Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh
Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh
Vinay Singh
Copyright © 2022 Vinay Singh
All products and brand names used in this book are trademarks, registered trademarks or trade
names of their respective owners.
This book is sold subject to the condition that it shall not, by way of trade or otherwise, be
lent, resold, hired out, or otherwise circulated without the author’s prior written consent in
any form of binding or cover other than that in which it is published and without a similar
condition including this condition being imposed on the subsequent purchaser and without
limiting the rights under copyright reserved above, no solution of this publication may be
reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording or otherwise, without the prior
written permission of the author of this book.
The software distributed with this book is “AS IS ” BASIS , WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND , either express or implied.
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
4
I mean, if 10 years from now, when you are doing something quick and dirty,
you suddenly visualize that I am looking over your shoulders and say to yourself
"Dijkstra would not have liked this", well, that would be enough immortality for
me.
- Edsger W. Dijkstra, Computer Scientist
I Front Matter
1 Foreward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2 Acknowledgement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3 Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6 ISC-2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Title Case/Sort Words In Descending Order By Length . . . . . . . . . . . . . . . . . . 33
Test for Wonderous Square . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Anagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
6 CONTENTS
7 ISC-2006 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Consecutive Integer Sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Merging of two arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Security Code Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
8 ISC-2007 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Day on a given date and date validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Reverse the order of words in a sentence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Unique-digits integers in a range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
9 ISC-2008 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Smith Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Statistics Histogram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Largest and Second Largest Elements in a 2D array . . . . . . . . . . . . . . . . . . . . 66
10 ISC-2009 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Day After ’N’ days . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
2D Array: Sort the Boundary Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Sorting words in a sentence in ascending order by length . . . . . . . . . . . . . . 79
11 ISC-2010 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Denomination Breakup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Kaprekar number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Frequency table of words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
12 ISC-2011 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Number to words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Encryption based on character position . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Date Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
13 ISC-2012 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Prime palindrome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Sort words of a string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Sort a 2D array in ascending order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
14 ISC-2013 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Verify an ISBN number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Mirror image of a 2D array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Count palindromic words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
15 ISC-2014 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Composite magic number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Symmetry and sum of diagonals of a 2D array . . . . . . . . . . . . . . . . . . . . . . . 120
Remove redundant spaces and delete a word . . . . . . . . . . . . . . . . . . . . . . 123
16 ISC-2015 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Sum of digits to a given limit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Rotate a 2D matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Frequency of vowels and consonants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
17 ISC-2016 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Circular prime number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Sorting of boundary elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Manipulate words begining and ending with a vowel . . . . . . . . . . . . . . . . . 144
18 ISC-2017 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Cartoon capacity break-up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Quiz score evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
ROT13 encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
19 ISC-2018 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Goldbach numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Sort rows of a 2D array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Vertical banner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
20 ISC-2019 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Convert day number into date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Sort and transfer 1D array into a 2D array . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Make palindromic words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
21 ISC-2020 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Prime Adam integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Rows of 2D array as octal representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Sort words in ascending order of lengths . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
22 ISC-2021 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Fascinating Number In A Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Frequency of common words in two sentences . . . . . . . . . . . . . . . . . . . . . . 194
Sum of border elements & sorting of a 2D array . . . . . . . . . . . . . . . . . . . . . . 198
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
I
Front Matter
1 Foreward . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2 Acknowledgement . . . . . . . . . . . . . . 13
3 Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4 Introduction . . . . . . . . . . . . . . . . . . . . . . 17
1. Foreward
Mr Vinay Singh and my association with him has been fairly long, not only as a Principal, but
as a very good colleague too. He is a wonderful human being and a very diligent teacher, a
dynamic person in the field of Computer Science. While we as principals discuss anything
about Information Technology, it makes me proud to hear, "Madam, why would you worry
when you have a genius with you?"- his knowledge of the subject is that immense and
progressive.
Mr Vinay Singh is a perfect guide and a source of constant inspiration to his students, and
I am pleased that he has written, "Pragmatic Solutions to ISC Computer Science Practical
Problems", which was much required by ISC students. In this book, he has dealt with practical
solutions to ISC problems in detail. The book has step-by-step solutions to the practical
questions asked in the Board Examination from the year 2004 to 2021.
I congratulate and thank Mr Vinay Singh for his initiative. May all ISC students benefit from
his efforts.
God Bless.
Dr Zareen F. Rizvi
Principal, Saint John’s Academy,
Karchana, Prayagraj-211001, UP, India.
2. Acknowledgement
I would like to give special thanks to my family as a whole for their continuous support and
understanding when undertaking my research and writing my book.
I am also thankful to my son Vijjwal for going through the inital drafts and providing his
valuable suggestions.
I want to acknowledge the directors of Saint John’s Academy, Mr. C. V. Innes and Mrs D. Innes
who have made my professional life such a pleasure. I can’t thank them enough for being my
unrelenting source of inspiration, to challenge how things get done in the real world, since my
school days.
Vinay Singh
Computer Teacher, Saint John’s Academy
3. Preface
The book contains Java programs for problems asked in ISC Computer Science -2 (Practical)
examinations. The emphasis is not just on correct solutions but also on efficient and readable
code. The included programs not only employ best practices but also enable students to see
the theory in practice.
I have been teaching computer science for the last two decades; in my experience, books
targetted at this level focus and excel in illustrating theoretical concepts but are a bit lacking
for complex programs like the one asked in the practical examination. These questions and
their answers are available on internet but the most of them are not of good quality with not
much attention paid to efficiency. This book attempts to fill that perceived defficiency and
focuses on the implementation aspect.
Students will notice that descriptive names have been used for identifiers to enhance readability
and provide context. The programs use object-oriented principles instead of a monolithic
’main()’ method doing all the jobs.
4. Introduction
The recommended method to use this book is to first attempt the problems on your own and
then look at the solutions. Programming is better learned by doing than by viewing. The
time and effort spent on trying to solve a problem has a learning value even when one is
unsuccessful in solving the problem as it forces one to think. This helps one to develop
instincts which help one to solve new problems. The QR code at the beginning of most of
the chapters points to Youtube videos where I have walked through the entire program from
beginning to end.
In ISC Computer Practical Examination, marks are alloted for input and output. Due diligence
must be exercised to accept the input in the prescribed format along with the prompts and
messages. Always read the questions carefully at least twice, do not assume anything, as
problems are not repeated verbatim. Some variation is almost always present.
One should make a conscious effort to break down the overall logic into smaller distinct
methods as it makes debugging simpler. A single method doing multiple tasks cannot be tested
separately and consumes precious time during examination.
Do comment your program especially when you are doing something in a different way or
for mathematical optimization. There is scope for optimization in even simple things, for
example, when testing an integer for prime palindrome, isPalindrome(n) isPrime(n) is faster
than isPrime(n) isPalindrome(n) because test for primality is computationally expensive than
test for palindrome. Note that logical and () is short circuited, later methods are not invoked
when an earlier method returns false.
ISC computer science problems have a definite pattern, one of three problems have to be
attempted. Question one is usually from number theory (digit extraction, factorial based,
series, testing for certain types like Prime, Kaprekar etc), date/time concept (difference
between dates/times, date/time after a given duration etc). Question two is invariably from
single or double dimension arrays while question three is almost always from strings (word
18 Chapter 4. Introduction
extraction, encryption, pattern etc). An attempt must be made to write methods for these with
generalizations. The future problems will not be the same but definitely be similar to those
already asked.
At some places in this book, you will notice that a block of code is placed inside a comment,
to show a better way of doing the same thing in the following lines. The toString() method,
which is usually not given prominence in textbooks, is used in some programs and deserves
special mention. The method toString(), if present inside a class, is automatically invoked
whenever an object needs to be converted into a string. In other words, this method is used
to return a string representation of an object and can be used to print an object directly from
within System.out.print().
The source code of this book is available in a GitHub repository at https://github.com/
vinay-singh-in/book-isc-pract-solutions-download. If you have any questions or
clarification, you may post the same to the forum setup for ’Pragmatic Solution To ISC
Computer Science Practical Problems’ at https://www.codewithvinay.com/forums.
5 ISC-2004 . . . . . . . . . . . . . . . . . . . . . . . . . . 21
6 ISC-2005 . . . . . . . . . . . . . . . . . . . . . . . . . . 33
7 ISC-2006 . . . . . . . . . . . . . . . . . . . . . . . . . . 43
8 ISC-2007 . . . . . . . . . . . . . . . . . . . . . . . . . . 53
9 ISC-2008 . . . . . . . . . . . . . . . . . . . . . . . . . . 61
10 ISC-2009 . . . . . . . . . . . . . . . . . . . . . . . . . . 71
11 ISC-2010 . . . . . . . . . . . . . . . . . . . . . . . . . . 81
12 ISC-2011 . . . . . . . . . . . . . . . . . . . . . . . . . . 91
13 ISC-2012 . . . . . . . . . . . . . . . . . . . . . . . . . . 99
14 ISC-2013 . . . . . . . . . . . . . . . . . . . . . . . . . 109
15 ISC-2014 . . . . . . . . . . . . . . . . . . . . . . . . . 117
16 ISC-2015 . . . . . . . . . . . . . . . . . . . . . . . . . 127
17 ISC-2016 . . . . . . . . . . . . . . . . . . . . . . . . . 137
18 ISC-2017 . . . . . . . . . . . . . . . . . . . . . . . . . 149
19 ISC-2018 . . . . . . . . . . . . . . . . . . . . . . . . . 159
20 ISC-2019 . . . . . . . . . . . . . . . . . . . . . . . . . 169
21 ISC-2020 . . . . . . . . . . . . . . . . . . . . . . . . . 181
22 ISC-2021 . . . . . . . . . . . . . . . . . . . . . . . . . 191
5. ISC-2004
ISC-2004, Question 1
Smallest Base Problem
Numbers have different representations depending on the bases on which they are expressed.
For example in base 3, the number 12 is written as 110 (1 ∗ 32 + 1 ∗ 31 + 0 ∗ 30 ), but in base 8
it is written as 14 (1 ∗ 81 + 4 ∗ 80 ).
Consider, for example, the integers 12 and 5. Certainly these are not equal if base 10 is used
for each. But suppose 12 was a base 3 number and 5 was a base 6 number then what happens,
12 base 3 = 1 ∗ 31 + 2 ∗ 30 , or 5 base 6 or 5 base 10 (5 in any base is equal to 5 base 10). So 12
and 5 can be equal if you select the right bases for each of them.
Write a program to input two integers, X and Y, and calculate the smallest base for X and
smallest base for Y (likely different from X) so that X and Y represent the same value.
The base associated with X and Y will be between 1 and 20 (both inclusive). In representing
these numbers the digits 0 to 9 have their usual decimal interpretations.
The upper case alphabetic characters A through J represent digits 10 through 19 respectively.
Test your program for the following data and some random data:
SAMPLE DATA:
INPUT:
X = 12
22 Chapter 5. ISC-2004
Y = 5
OUTPUT:
12(base 3) = 5 (base 6)
INPUT:
X = 10
Y = A
OUTPUT:
10 (base 10) = A (base 11)
INPUT:
X = 12
Y = 34
OUTPUT:
12 (base 17) = 34 (base 5)
INPUT:
X = 123
Y = 456
OUTPUT:
1 2 3 is not equal to 456 in any base between 2 to 20
INPUT:
X = 42
Y = 36
OUTPUT:
42 (base 7) = 36 (base 8)
Solution
1 import java . util .*;
2
3 class Base {
4 private String X , Y ;
5 private String output ;
6 private boolean found ;
7
8 public Base ( String X , String Y ) {
9 this . X = X ;
10 this . Y = Y ;
11 process () ;
12 }
13
14 private void process () {
15 int value1 , value2 ;
16 output = " " ;
17 found = false ;
18 int initial_i = maximumDigit ( X ) + 1;
19 int initial_x = maximumDigit ( Y ) + 1;
20
21 for ( int i = initial_i ; i <= 20; i ++) {
22 value1 = convertFromBaseNToDecimal (X , i ) ;
23 for ( int x = initial_x ; x <= 20; x ++) {
24 value2 = convertFromBaseNToDecimal (Y , x ) ;
25 if ( value1 == value2 ) {
26 output += X + " ( base ␣ " + i + " ) ␣ = ␣ " + Y
+ " ( base ␣ " + x + " ) \ n " ;
27 found = true ;
28 }
29 if ( found )
30 break ;
31 }
32
33 }
34 }
35
36 public String toString () {
37 if ( found )
38 return output ;
39 else
40 return X + " ␣ is ␣ not ␣ equal ␣ to ␣ " + Y + " ␣ in ␣ any ␣
base ␣ between ␣ 2 ␣ to ␣ 20 " ;
41 }
42
43 private static int convertFromBaseNToDecimal ( String
number , int baseN ) {
44 int answer = 0;
45 number = number . toUpperCase () ;
46 int length = number . length () ;
47 int digit , multiplier ;
48 for ( int i = 0; i < length ; i ++) {
49 multiplier = ( int ) Math . pow ( baseN , length - i -
1) ;
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
24 Chapter 5. ISC-2004
94 }
95 }
ISC-2004, Question 2
Decoding Secret Message
INPUT:
Encoded Message:
23511011501782351112179911801562340161171141148
OUTPUT:
THE DECODED MESSAGE: Truth Always Wins
Solution
1 import java . util .*;
2
3 class Message {
4 private String encodedMessage , decodedMessage ;
5
6 public Message ( String encodedMessage ) {
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
26 Chapter 5. ISC-2004
ISC-2004, Question 3
Machine Usage Problem
The manager of a company wants to analyse the machine usage from the records to find the
utilization of the machine. He wants to know how long each user used the machine. When the
user wants to use the machine he must login to the machine and after finishing the work he
must log off the machine.
Each log record consists of:
• Hours
• Minutes
Date consists of:
• Day
• Month
You may assume all logins and logouts are in the same year and there are 100 users at the
most. The time format is 24 hour machine hours and minutes.
Design a program:
1. To find the duration for which each user has logged. Output all records along with the
duration in hours (format hours: minutes).
2. Output the record of the user who logged for the longest duration. You may assume that
no user will login for more than 48 hours.
Test your program for the following data values and some more random data.
SAMPLE DATA:
INPUT:
Number of users: 3
USER LOGIN LOGOUT
IDENTIFICATION TIME & DATE TIME & DATE
149 20:10 20-12 2:50 21-12
173 12:30 20-12 12:30 21-12
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
28 Chapter 5. ISC-2004
OUTPUT:
USER LOGIN LOGOUT DURATION
IDENTIFICATION TIME & DATE TIME & DATE HOURS:MINS
149 20:10 20-12 2:50 21-12 6:40
173 12:30 20-12 12:30 21-12 24:00
142 16:20 20-12 16:30 20-12 00:10
THE USER WHO LOGGED IN FOR THE LONGEST DURATION
173 12:30 20-12 12:30 21-12 24:00
Solution
1 import java . util .*;
2
3 class UserLog {
4 private int id [];
5 private String loginTime [] , loginDate [] , logoutTime [] ,
logoutDate [];
6 int duration [] , maxIndex ;
7 private static int month [] = { 31 , 28 , 31 , 30 , 31 , 30 ,
31 , 31 , 30 , 31 , 30 , 31 };
8
9 public UserLog ( int id [] , String loginTime [] , String
loginDate [] , String logoutTime [] , String logoutDate
[]) {
10 this . id = id ;
11 this . loginTime = loginTime ;
12 this . loginDate = loginDate ;
13 this . logoutTime = logoutTime ;
14 this . logoutDate = logoutDate ;
15 duration = new int [ id . length ];
16 process () ;
17 }
18
19 private void process () {
20 String temp [];
21 int hr , min , dd , mm , finalMinutes , initialMinutes ,
delta , maxDelta = 0;
22 for ( int i = 0; i < id . length ; i ++) {
23 temp = loginTime [ i ]. split ( " : " ) ;
24 hr = Integer . parseInt ( temp [0]) ;
25 min = Integer . parseInt ( temp [1]) ;
26
27 temp = loginDate [ i ]. split ( " -" ) ;
28 dd = Integer . parseInt ( temp [0]) ;
29 mm = Integer . parseInt ( temp [1]) ;
30
31 initialMinutes = getMinutes ( dd , mm , hr , min ) ;
32
33 temp = logoutTime [ i ]. split ( " : " ) ;
34 hr = Integer . parseInt ( temp [0]) ;
35 min = Integer . parseInt ( temp [1]) ;
36
37 temp = logoutDate [ i ]. split ( " -" ) ;
38 dd = Integer . parseInt ( temp [0]) ;
39 mm = Integer . parseInt ( temp [1]) ;
40
41 finalMinutes = getMinutes ( dd , mm , hr , min ) ;
42 delta = finalMinutes - initialMinutes ;
43 duration [ i ] = delta ;
44 if ( delta > maxDelta ) {
45 maxDelta = delta ;
46 maxIndex = i ;
47 }
48
49 }
50 }
51
52 private static int getMinutes ( int dd , int mm , int hr ,
int min ) {
53 int days = dd ;
54 for ( int i = 0; i < mm - 1; i ++) {
55 days += month [ i ];
56 }
57 return ( days * 24 * 60) + ( hr * 60) + min ;
58 }
59
60 public String toString () {
61 String output = " " ;
62 output += " USER \ t \ tLOGIN \ t \ tLOGOUT \ tDURATION \ n " ;
63 output += " IDENTIFICATION \ tTIME ␣ & ␣ DATE \ tTIME ␣ & ␣ DATE \
tHOURS : MINS \ n " ;
64 for ( int i = 0; i < id . length ; i ++) {
65 output += id [ i ] + " \ t \ t " + loginTime [ i ] + " ␣ " +
loginDate [ i ] + " \ t " + logoutTime [ i ] + " ␣ " +
logoutDate [ i ]
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
30 Chapter 5. ISC-2004
104
105 in . close () ;
106 }
107 }
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
6. ISC-2005
ISC-2005, Question 1
Title Case/Sort Words In Descending Order By Length
Write a program which takes a string (maximum 80 characters) terminated by a full stop. The
words in this string are assumed to be separated by one or more blanks.
Arrange the words of the input string in descending order of their lengths. Same length words
should be sorted alphabetically.
Each word must star with an uppercase letter and the sentence should be terminated by a full
stop.
Test your program for the following data and some random data.
SAMPLE DATA:
INPUT:
This is human resource department.
OUTPUT:
Department Resource Human This Is.
INPUT:
To handle yourself use your head and to handle others use your heart.
34 Chapter 6. ISC-2005
OUTPUT:
Yourself Handle Handle Others Heart Head Your Your And Use Use To To.
Solution
1 import java . util .*;
2
3 class MyString {
4 private String sentence , words [] , output ;
5
6 public MyString ( String sentence ) {
7 this . sentence = sentence . toLowerCase () ;
8 arrange () ;
9 }
10
11 private void arrange () {
12 String temp ;
13 boolean swapped ;
14 words = sentence . split ( " [ ␣ .]+ " ) ;
15 for ( int i = 0; i < words . length - 1; i ++) {
16 swapped = false ;
17 for ( int x = 0; x < words . length - 1 - i ; x ++) {
18 if ( words [ x + 1]. length () > words [ x ]. length
() ) {
19 temp = words [ x + 1];
20 words [ x + 1] = words [ x ];
21 words [ x ] = temp ;
22 swapped = true ;
23 } else if ( words [ x + 1]. length () == words [ x
]. length () ) {
24 if ( words [ x + 1]. compareTo ( words [ x ]) <
0) {
25 temp = words [ x + 1];
26 words [ x + 1] = words [ x ];
27 words [ x ] = temp ;
28 swapped = true ;
29 }
30 }
31 }
32 if (! swapped )
33 break ;
34 }
35 assemble () ;
36 }
37
38 private void assemble () {
39 output = " " ;
40 for ( String word : words ) {
41 output += Character . toUpperCase ( word . charAt (0) )
+ word . substring (1) + " ␣ " ;
42 }
43 output = output . trim () + " . " ;
44 }
45
46 public String toString () {
47 return output ;
48 }
49 }
50
51 class ISC2005Q1 {
52 public static void main ( String [] args ) {
53 Scanner in = new Scanner ( System . in ) ;
54 String sentence ;
55 System . out . println ( " INPUT : " ) ;
56 sentence = in . nextLine () ;
57 MyString str = new MyString ( sentence ) ;
58 System . out . println ( " OUTPUT :\ n " + str ) ;
59
60 in . close () ;
61 }
62 }
ISC-2005, Question 2
Test for Wonderous Square
For example the following grid is a wondrous square where the sum of each row or column is
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
36 Chapter 6. ISC-2005
65 when n = 5:
17 24 1 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
15 0 1 <-- 15 is not a prime
INPUT:
N = 3
1 2 4
3 7 5
8 9 6
OUTPUT:
INPUT:
N = 2
2 3
3 2
OUTPUT:
NOT A WONDROUS SQUARE.
PRIME ROW INDEX COLUMN INDEX
2 0 0
3 0 1
3 1 0
2 1 1
Solution
1 import java . util .*;
2
3 class Square {
4 private int a [][] , N , sum ;
5
6 public Square ( int a [][]) {
7 this . a = a ;
8 this . N = a . length ;
9 sum = ( int ) (0.5 * N * ( N * N + 1) ) ;
10 }
11
12 public boolean isWondrousSquare () {
13 boolean flag [] = new boolean [ N * N ];
14 int sumOfRows , sumOfCols ;
15 for ( int row = 0; row < N ; row ++) {
16 sumOfRows = sumOfCols = 0;
17 for ( int col = 0; col < N ; col ++) {
18 sumOfRows += a [ row ][ col ];
19 sumOfCols += a [ col ][ row ];
20 if ( flag [ a [ row ][ col ] - 1])
21 return false ;
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
38 Chapter 6. ISC-2005
ISC-2005, Question 3
Anagram
We would like to generate all possible anagrams of a word. For example if the given word is
‘TOP’, there will be 6 possible anagrams: TOP TPO OTP OPT POT PTO
An anagram must be printed only once. You may output the anagrams in any order.
Also, output the total number of anagrams.
You may assume that the number of letters, N, in the word will be 7 at most, i.e. N < 7
Test your program for the given data and some random data.
SAMPLE DATA:
INPUT:
TO
OUTPUT:
TO
OT
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
40 Chapter 6. ISC-2005
INPUT:
LEAN
OUTPUT:
LEAN
LENA
LAEN
LANE
LNAE
LNEA
ELAN
ELNA
EALN
EANL
ENAL
ENLA
AELN
AENL
ALEN
ALNE
ANLE
ANEL
NEAL
NELA
NAEL
NALE
NLAE
NLEA
Total number of anagrams = 24
Solution
1 import java . util .*;
2
3 class Anagram {
4 private String word , output ;
5 private int count ;
6
7 public Anagram ( String word ) {
8 this . word = word ;
9 char s [] = word . toCharArray () ;
10 output = " " ;
11 count = 0;
12 permute (s , 0) ;
13 }
14
15 public void permute ( char s [] , int k ) {
16 if ( k == s . length - 1) {
17 output += new String ( s ) + " \ n " ;
18 count ++;
19 } else {
20 char temp ;
21 for ( int i = k ; i < s . length ; i ++) {
22 if ( s [ i ] == s [ k ] && i != k )
23 continue ;
24 temp = s [ k ];
25 s [ k ] = s [ i ];
26 s [ i ] = temp ;
27 permute (s , k + 1) ;
28 temp = s [ k ];
29 s [ k ] = s [ i ];
30 s [ i ] = temp ;
31 }
32 }
33 }
34
35 public String toString () {
36 return output ;
37 }
38
39 public int length () {
40 return count ;
41 }
42 }
43
44 class ISC2012Q1 {
45 public static void main ( String [] args ) {
46 Scanner in = new Scanner ( System . in ) ;
47 System . out . println ( " INPUT : " ) ;
48 String word = in . next () ;
49 Anagram obj = new Anagram ( word ) ;
50 System . out . println ( " OUTPUT :\ n " + obj ) ;
51 System . out . println ( " Total ␣ number ␣ of ␣ anagrams = " + obj
. length () ) ;
52 in . close () ;
53 }
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
42 Chapter 6. ISC-2005
54 }
ISC-2006, Question 1
Consecutive Integer Sum
INPUT: N = 15
OUTPUT:
7 + 8
44 Chapter 7. ISC-2006
1 + 2 + 3 + 4 + 5
4 + 5 + 6
INPUT: N = 21
OUTPUT:
10 + 11
1 + 2 + 3 + 4 + 5 + 6
6 + 7 + 8
Solution
1 import java . util .*;
2
3 class ConsecutiveIntegers {
4 private int N ;
5 private String output ;
6 private boolean found ;
7
8 public ConsecutiveIntegers ( int N ) {
9 this . N = N ;
10 found = false ;
11 findSum () ;
12 }
13
14 public void findSum () {
15 output = " " ;
16 int half = N / 2;
17 for ( int beg = 1 , end = 1 , sum = 1; beg <= half ;) {
18 if ( sum < N ) {
19 end += 1;
20 sum += end ;
21 } else if ( sum > N ) {
22 sum -= beg ;
23 beg += 1;
24 } else if ( sum == N ) {
25 output += " \ t " ;
26 for ( int term = beg ; term <= end ; term ++) {
27 output += term ;
28 if ( term < end )
29 output += " ␣ + ␣ " ;
30 }
31 output += " \ n " ;
32 found = true ;
33 sum -= beg ;
34 beg += 1;
35 }
36 }
37 }
38
39 public String toString () {
40 if ( found )
41 return output ;
42 else
43 return " \ tNO ␣ SOLUTION ␣ EXISTS " ;
44 }
45 }
46
47 class ISC2006Q1 {
48 public static void main ( String [] args ) {
49 Scanner in = new Scanner ( System . in ) ;
50 System . out . print ( " INPUT :\ tN ␣ = ␣ " ) ;
51 int N = in . nextInt () ;
52 ConsecutiveIntegers obj = new ConsecutiveIntegers ( N )
;
53 System . out . println ( " OUTPUT :\ n " + obj ) ;
54 in . close () ;
55 }
56 }
ISC-2006, Question 2
Merging of two arrays
Write a program that inputs the names of people into two different arrays A and B. Array A
has N number of names while Array B has M number of names, with no duplicates in either
of them. Merge arrays A and B into a single array C, such that the resulting array is sorted
alphabetically.
Display all the three arrays, A, B and C, sorted alphabetically.
Test your program for the given data and some random data.
SAMPLE DATA:
INPUT:
Enter number of names in Array A, N = 2
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
46 Chapter 7. ISC-2006
Solution
1 import java . util .*;
2
3 class MyArray {
4 private String A [] , B [] , C [];
5
6 public MyArray ( String A [] , String B []) {
7 this . A = A ;
8 this . B = B ;
9 C = new String [ A . length + B . length ];
10 sort () ;
11 }
12
13 private void sort () {
14 Arrays . sort ( A ) ;
15 Arrays . sort ( B ) ;
16 merge () ;
17 }
18
19 private void merge () {
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
48 Chapter 7. ISC-2006
ISC-2006, Question 3
Security Code Validation
A new advanced Operating System, incorporating the latest hi-tech features has been designed
by Opera Computer Systems.
The task of generating copy protection codes to prevent software piracy has been entrusted to
the Security Department.
The Security Department has decided to have codes containing a jumbled combination of
alternate uppercase letters of the alphabet starting from A upto K (namely among A,C,E,G,I,K).
The code may or may not be in the consecutive series of alphabets.
Each code should not exceed 6 characters and there should be no repetition of characters. If it
exceeds 6 characters, display an appropriate error message.
Write a program to input a code and its length. At the first instance of an error display
"Invalid!" stating the appropriate reason. In case of no error, display the message "Valid!".
Test your program for the following data and some random data.
SAMPLE DATA
INPUT: N = 4
ABCE
OUTPUT:
Invalid! Only alternate letters permitted!
INPUT: N = 4
AcIK
OUTPUT:
Invalid! Only upper case letters permitted!
INPUT: N = 4
AAKE
OUTPUT:
Invalid! Repetition of characters not permitted!
INPUT: N = 7
OUTPUT:
Error! Length of string should not exceed 6 characters!
INPUT: N = 4
AEGIK
OUTPUT:
Invalid! String length not the same as specified!
INPUT: N = 3
ACE
OUTPUT:
Valid!
INPUT: N = 5
GEAIK
OUTPUT:
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
50 Chapter 7. ISC-2006
Valid!
Solution
1 import java . util .*;
2
3 class CopyProtectionCode {
4 private String code , output ;
5 private static String validCharacters = " ACEGIKacegik " ;
6 private int N ;
7
8 public CopyProtectionCode ( String code , int N ) {
9 this . code = code ;
10 this . N = N ;
11 validate () ;
12 }
13
14 private void validate () {
15 if ( N != code . length () ) {
16 output = " Invalid ! ␣ String ␣ length ␣ not ␣ the ␣ same ␣ as
␣ specified ! " ;
17 return ;
18 }
19 char ch ;
20 int foundAtPosition ;
21 for ( int position = 0; position < code . length () ;
position ++) {
22 ch = code . charAt ( position ) ;
23 foundAtPosition = validCharacters . indexOf ( ch ) ;
24 if ( foundAtPosition == -1) {
25 output = " Invalid ! ␣ Only ␣ alternate ␣ letters ␣
permitted ! " ;
26 return ;
27 }
28 if ( foundAtPosition > 5) {
29 output = " Invalid ! ␣ Only ␣ upper ␣ case ␣ letters ␣
permitted ! " ;
30 return ;
31 }
32 if ( code . lastIndexOf ( ch ) != position ) {
33 output = " Invalid ! ␣ Repetition ␣ of ␣ characters ␣
not ␣ permitted ! " ;
34 return ;
35 }
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
8. ISC-2007
ISC-2007, Question 1
Day on a given date and date validation
Write a program to accept a date in the string format dd/mm/yyyy and accept the name of the
day on 1st of January of the corresponding year. Find the day for the given date.
Example:
INPUT
Date : 5/7/2001
Day on 1st January : MONDAY
OUTPUT
Day on 5/7/2001 : THURSDAY
Test run the program on the following inputs.
INPUT DATE DAY ON 1ST JANUARY OUTPUT DAY FOR INPUT DATE
4/9/1998 THURSDAY FRIDAY
31/8/1999 FRIDAY TUESDAY
6/12/2000 SATURDAY WEDNESDAY
The program should include the part for validating the inputs namely the date and the day on
1st January of that year.
Solution
54 Chapter 8. ISC-2007
41 }
42
43 public int getDayNumber () {
44 int dayNumber = dd ;
45 for ( int i = 0; i < mm - 1; i ++) {
46 dayNumber += days [ i ];
47 }
48 return dayNumber ;
49 }
50
51 private int getDayIndex () {
52 return (( getDayNumber () - 1 + index ) % 7) ;
53 }
54
55 public String toString () {
56 return weekDays [ getDayIndex () ];
57 }
58 }
59
60 class ISC2007Q1 {
61 public static void main ( String [] args ) {
62 Scanner in = new Scanner ( System . in ) ;
63 System . out . println ( " INPUT " ) ;
64 System . out . print ( " Date \ t \ t \ t \ t : ␣ " ) ;
65 String inputDate = in . nextLine () ;
66 System . out . print ( " Day ␣ on ␣ 1 st ␣ January \ t \ t : ␣ " ) ;
67 String inputDay = in . nextLine () ;
68 MyDate dt = new MyDate ( inputDate , inputDay ) ;
69 if ( dt . validate () ) {
70 System . out . println ( " OUTPUT :\ nDAY ␣ ON ␣ " +
inputDate + " \ t \ t : ␣ " + dt ) ;
71 } else {
72 System . out . println ( " INVALID ␣ DATE / DAY " ) ;
73 }
74 in . close () ;
75 }
76 }
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
56 Chapter 8. ISC-2007
ISC-2007, Question 2
Reverse the order of words in a sentence
The input in this problem will consist of a number of lines of English text consisting of the
letters of the English alphabet, the punctuation marks (‘) apostrophe, (.) full stop, (,) comma,
(;) semicolon, (:) colon and white space characters (blank, newline). Your task is to print the
words of the text in reverse order without any punctuation marks other than blanks.
For example consider the following input text:
This is a sample piece of text to illustrate this problem. If you are smart you will solve this
right.
The corresponding output would read as: right this solve will you smart are you If problem
this illustrate to text of piece sample a is This That is, the lines are printed in reverse order.
Individual words are not reversed
Input format: The first line of input contains a single integer N (<= 20), indicating the number
of lines in the input. This is followed by N lines of input text. Each line should accept a
maximum of 80 characters.
Output format: Output the text containing the input lines in reverse order without punctuation
except blanks as illustrated above.
Test your program for the following data and some random data.
SAMPLE DATA
INPUT: 2
Emotions, controlled and directed to work, is character.
By Swami Vivekananda.
OUTPUT:
Vivekananda Swami By character is work to directed and controlled Emotions
INPUT: 1
Do not judge a book by its cover.
OUTPUT:
Cover its by book a judge not Do
Solution
1 import java . util .*;
2
3 class Sentence {
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
58 Chapter 8. ISC-2007
ISC-2007, Question 3
Unique-digits integers in a range
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, 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:
SAMPLE DATA:
INPUT: m = 100
n = 120
OUTPUT: THE UNIQUE-DIGIT INTEGERS ARE:-
102, 103, 104, 105, 106, 107, 108, 109, 120.
FREQUENCY OF UNIQUE-DIGIT IN INTEGERS IS : 9
INPUT: m = 2500
n = 2550
OUTPUT: THE UNIQUE-DIGIT INTEGERS ARE:-
2501, 2503, 2504, 2506, 2507, 2508, 2509, 2510, 2513,
2514, 2516, 2517, 2518, 2519, 2530, 2531, 2534, 2536,
2537, 2538, 2539, 2540, 2541, 2543, 2546, 2547, 2548,
2549.
FREQUENCY OF UNIQUE-DIGIT INTEGERS IS : 28
Solution
1 import java . util .*;
2
3 class Unique {
4 private int m , n , frequency ;
5 private String output ;
6
7 public Unique ( int m , int n ) {
8 this . m = m ;
9 this . n = n ;
10 findUniqueDigitNumbers () ;
11 }
12
13 public void findUniqueDigitNumbers () {
14 frequency = 0;
15 output = " THE ␣ UNIQUE - DIGIT ␣ INTEGERS ␣ ARE : -\ n \ t " ;
16 for ( int i = m ; i <= n ; i ++) {
17 if ( isUniqeNumber ( i ) ) {
18 frequency ++;
19 output += i + " ,␣ " ;
20 }
21 }
22 output = output . substring (0 , output . length () - 2) +
".";
23 }
24
25 private boolean isUniqeNumber ( int n ) {
26 boolean unique [] = new boolean [10];
27 int digit ;
28 for ( int temp = n ; temp > 0; temp /= 10) {
29 digit = temp % 10;
30 if ( unique [ digit ])
31 return false ;
32 unique [ digit ] = true ;
33 }
34 return true ;
35 }
36
37 public String toString () {
38 return output ;
39 }
40
41 public int getFrequency () {
42 return frequency ;
43 }
44 }
45
46 class ISC2007Q3 {
47 public static void main ( String [] args ) {
48 Scanner in = new Scanner ( System . in ) ;
49 System . out . print ( " INPUT :\ tm ␣ = ␣ " ) ;
50 int m = in . nextInt () ;
51 System . out . print ( " \ tn ␣ = ␣ " ) ;
52 int n = in . nextInt () ;
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
60 Chapter 8. ISC-2007
ISC-2008, Question 1
Smith Number
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, ...
Example :
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:
62 Chapter 9. ISC-2008
Input: 94
Output: SMITH Number
Input: 102
Output: NOT SMITH Number
Input: 666
Output: SMITH Number
Input: 999
Output: NOT SMITH Number
Solution
1 import java . util .*;
2
3 class Number {
4 private int n , temp ;
5
6 public Number ( int n ) {
7 this . n = n ;
8 }
9
10 private static int getSumOfDigits ( int n ) {
11 int sum = 0;
12 while ( n > 0) {
13 sum += n % 10;
14 n /= 10;
15 }
16 return sum ;
17 }
18
19 private int getSumOfFactors ( int divisor ) {
20 int sumOfDigits = 0 , divisiorSum = getSumOfDigits (
divisor ) ;
21 while ( temp % divisor == 0) {
22 temp /= divisor ;
23 sumOfDigits += divisiorSum ;
24 }
25 return sumOfDigits ;
26 }
27
28 public boolean isSmithNumber () {
29 if ( n == 2)
30 return false ;
31 temp = n ;
32 int divisor = 2 , sumOfPrimeFactors = 0 , half = n /
2;
33 sumOfPrimeFactors += getSumOfFactors ( divisor ) ;
34 for ( divisor = 3; divisor <= half ; divisor += 2) {
35 sumOfPrimeFactors += getSumOfFactors ( divisor ) ;
36 }
37 if ( temp > 1 || sumOfPrimeFactors != getSumOfDigits (
n))
38 return false ;
39 else
40 return true ;
41 }
42 }
43
44 class ISC2008Q1 {
45 public static void main ( String [] args ) {
46 Scanner in = new Scanner ( System . in ) ;
47 System . out . print ( " INPUT :\ t " ) ;
48 int n = in . nextInt () ;
49 Number obj = new Number ( n ) ;
50 System . out . print ( " OUTPUT :\ t " ) ;
51 if ( obj . isSmithNumber () ) {
52 System . out . println ( " SMITH ␣ Number " ) ;
53 } else {
54 System . out . println ( " NOT ␣ SMITH ␣ Number " ) ;
55 }
56 in . close () ;
57 }
58 }
ISC-2008, Question 2
Statistics Histogram
A sentence is terminated by either ".", "!" or "?" followed by a space. Input a piece of text
consisting of sentences. Assume that there will be a maximum of 10 sentences in block letters.
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
64 Chapter 9. ISC-2008
1. Obtain the length of the sentence (measured in words) and the frequency of vowels in
each sentence.
2. Generate the output as shown below using the given data.
Sample data:
INPUT: HELLO! HOW ARE YOU? HOPE EVERYTHING IS FINE. BEST OF LUCK.
OUTPUT
Sentence No. of Vowels No. of words
1 2 1
2 5 3
3 8 4
4 3 3
2 VVVVVVVVVVVVVVV
WWWWWWWWW
3 VVVVVVVVVVVVVVVVVVVVVVVV
WWWWWWWWWWWW
4 VVVVVVVVV
WWWWWWWWW
Solution
1 import java . util .*;
2
3 class Text {
4 private String sentence [];
5 static String vowels = " aeiouAEIOU " ;
6 private int word [] , vowel [] , max ;
7
8 public Text ( String paragraph ) {
9 sentence = paragraph . split ( " [. ,?!] " ) ;
10 word = new int [ sentence . length ];
11 vowel = new int [ sentence . length ];
12
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
66 Chapter 9. ISC-2008
ISC-2008, Question 3
Largest and Second Largest Elements in a 2D array
Given a square matrix list [ ] [ ] of order ‘n’. The maximum value possible for ‘n’ is 20. Input
the value for ‘n’ and the positive integers in the matrix and perform the following tasks:
1. Display the original matrix.
2. Print the row and column position of the largest element of the matrix.
3. Print the row and column position of the second largest element of the matrix.
4. Sort the elements of the rows in ascending order and display the new matrix.
Sample data:
INPUT: N = 3
List [] []
5 1 3
7 4 6
9 8 2
OUTPUT
5 1 3
7 4 6
9 8 2
Sorted list
1 3 5
4 6 7
2 8 9
Solution
1 import java . util .*;
2
3 class Matrix {
4 private int list [][] , n , largest , secondLargest ;
5 String largestLocation , secondLargestLocation ;
6
7 public Matrix ( int n ) {
8 this . n = n ;
9 list = new int [ n ][ n ];
10 }
11
12 public void input () {
13 Scanner in = new Scanner ( System . in ) ;
14 System . out . println ( " List ␣ [] ␣ [] " ) ;
15 for ( int row = 0; row < n ; row ++) {
16 for ( int col = 0; col < n ; col ++) {
17 list [ row ][ col ] = in . nextInt () ;
18 }
19 }
20 in . close () ;
21 compute () ;
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
68 Chapter 9. ISC-2008
22 }
23
24 public void compute () {
25 largest = secondLargest = Integer . MIN_VALUE ;
26 largestLocation = secondLargestLocation = " " ;
27 for ( int row = 0; row < n ; row ++) {
28 for ( int col = 0; col < n ; col ++) {
29 if ( list [ row ][ col ] > largest ) {
30 secondLargest = largest ;
31 secondLargestLocation = largestLocation ;
32 largest = list [ row ][ col ];
33 largestLocation = " row ␣ " + ( row + 1) + "
␣ and ␣ column ␣ is ␣ " + ( col + 1) ;
34 } else if ( list [ row ][ col ] > secondLargest &&
list [ row ][ col ] != largest ) {
35 secondLargest = list [ row ][ col ];
36 secondLargestLocation = " row ␣ " + ( row +
1) + " ␣ and ␣ column ␣ is ␣ " + ( col + 1) ;
37 } else if ( list [ row ][ col ] == largest ) {
38 largestLocation += " ,␣ row ␣ " + ( row + 1)
+ " ␣ and ␣ column ␣ is ␣ " + ( col + 1) ;
39 } else if ( list [ row ][ col ] == secondLargest )
{
40 secondLargestLocation += " ,␣ row ␣ " + ( row
+ 1) + " ␣ and ␣ column ␣ is ␣ " + ( col + 1)
;
41 }
42 }
43 }
44 }
45
46 public void largestDetails () {
47 System . out . println ( " The ␣ largest ␣ element ␣ " + largest
+ " ␣ is ␣ in ␣ " + largestLocation ) ;
48 }
49
50 public void secondLargestDetails () {
51 if ( secondLargest != Integer . MIN_VALUE ) {
52 System . out . println ( " The ␣ second ␣ largest ␣ element ␣ "
+ secondLargest + " ␣ is ␣ in ␣ " +
secondLargestLocation ) ;
53 } else {
54 System . out . println ( " No ␣ second ␣ largest ␣ element " ) ;
55 }
56 }
57 public void sortRows () {
58 for ( int row =0; row < n ; row ++) {
59 Arrays . sort ( list [ row ]) ;
60 }
61 }
62 public String toString () {
63 String output = " " ;
64 for ( int row = 0; row < n ; row ++) {
65 for ( int col = 0; col < n ; col ++) {
66 output += list [ row ][ col ];
67 if ( col <n -1) output += " \ t " ;
68 }
69 output += " \ n " ;
70 }
71 return output ;
72 }
73 }
74
75 class ISC2008Q3 {
76 public static void main ( String [] args ) {
77 Scanner in = new Scanner ( System . in ) ;
78 System . out . print ( " INPUT :\ tN ␣ = ␣ " ) ;
79 int n = in . nextInt () ;
80 Matrix obj = new Matrix ( n ) ;
81 obj . input () ;
82 System . out . println ( " OUTPUT : " ) ;
83 System . out . println ( obj ) ;
84 obj . largestDetails () ;
85 obj . secondLargestDetails () ;
86 obj . sortRows () ;
87 System . out . println ( " Sorted ␣ list " ) ;
88 System . out . println ( obj ) ;
89 in . close () ;
90 }
91 }
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
10. ISC-2009
ISC-2009, Question 1
Day After ’N’ days
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: 233
YEAR: 2008
DATE AFTER (N DAYS): 17
OUTPUT:
DATE: 20 TH AUGUST, 2008
DATE AFTER 17 DAYS: 6 TH SEPTEMBER, 2008
72 Chapter 10. ISC-2009
Example 2
INPUT:
DAY NUMBER: 360
YEAR: 2008
DATE AFTER (N DAYS): 45
OUTPUT:
DATE: 20 TH AUGUST, 2008
DATE AFTER 45 DAYS: 8 TH FEBRUARY, 2009
Solution
1 import java . util .*;
2
3 class Date {
4 private int day , month , year ;
5
6 public Date ( int day , int month , int year ) {
7 this . day = day ;
8 this . month = month ;
9 this . year = year ;
10 }
11
12 public String toString () {
13 String suffix [] = { " TH " , " ST " , " ND " , " RD " , " TH " , "
TH " , " TH " , " TH " , " TH " , " TH " };
14 String monthName [] = { " JANUARY " , " FEBRUARY " , " MARCH
" , " APRIL " , " MAY " , " JUNE " , " JULY " , " AUGUST " , "
SEPTEMBER " ,
15 " OCTOBER " , " NOVEMBER " , " DECEMBER " };
16 String finalSuffix ;
17 if ( day > 9 && day < 21)
18 finalSuffix = " TH " ;
19 else
20 finalSuffix = suffix [ day % 10];
21 return day + " ␣ " + finalSuffix + " ␣ " + monthName [
month - 1] + " ,␣ " + year ;
22 }
23 }
24
25 class ISC2009Q1 {
26 static int dayNumber , year , N ;
27
28 public static void main ( String [] args ) {
29 Scanner in = new Scanner ( System . in ) ;
30 System . out . println ( " INPUT " ) ;
31 System . out . print ( " DAY ␣ NUMBER :\ t \ t " ) ;
32 dayNumber = in . nextInt () ;
33 System . out . print ( " YEAR :\ t \ t \ t " ) ;
34 year = in . nextInt () ;
35 System . out . print ( " DATE ␣ AFTER ␣ ( N ␣ DAYS ) :\ t " ) ;
36 N = in . nextInt () ;
37
38 System . out . println ( " OUTPUT " ) ;
39 if ( dayNumber < 1 || dayNumber > 366) {
40 System . out . println ( " DAY ␣ NUMBER ␣ OUT ␣ OF ␣ RANGE . " ) ;
41 } else if ( year < 1000 || year > 9999) {
42 System . out . println ( " YEAR ␣ OUT ␣ OF ␣ RANGE . " ) ;
43 } else if ( N < 1 || N > 100) {
44 System . out . println ( " DATE ␣ AFTER ␣ ( N ␣ DAYS ) ␣ OUT ␣ OF ␣
RANGE . " ) ;
45 } else {
46 Date dt = dateFromDayNumber ( dayNumber , year ) ;
47 System . out . println ( " DATE :\ t \ t \ t " + dt ) ;
48 int nextDayNumber = dayNumber + N , nextYear ;
49 if ( nextDayNumber > 365) {
50 nextYear = year + 1;
51 } else {
52 nextYear = year ;
53 }
54 if ( isLeap ( year ) ) {
55 nextDayNumber = nextDayNumber % 366;
56 } else {
57 nextDayNumber = nextDayNumber % 365;
58 }
59 Date dateAfterNDays = dateFromDayNumber (
nextDayNumber , nextYear ) ;
60 System . out . println ( " DATE ␣ AFTER ␣ " + N + " ␣ DAYS :\ t
" + dateAfterNDays ) ;
61 }
62 in . close () ;
63 }
64
65 private static Date dateFromDayNumber ( int dayNumber , int
year ) {
66 int month [] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 ,
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
74 Chapter 10. ISC-2009
31 , 30 , 31 };
67 if ( isLeap ( year ) )
68 month [1] = 29;
69 int monthNumber ;
70 for ( monthNumber = 0; monthNumber < month . length ;
monthNumber ++) {
71 if ( dayNumber <= month [ monthNumber ])
72 break ;
73 dayNumber -= month [ monthNumber ];
74 }
75 return new Date ( dayNumber , monthNumber + 1 , year ) ;
76 }
77
78 public static boolean isLeap ( int year ) {
79 // int divisor ;
80 // if ( year %100==0) divisor =400;
81 // else divisor =4;
82 // return year % divisor ==0;
83 return year % ( year % 100 == 0 ? 400 : 4) == 0;
84 }
85 }
ISC-2009, Question 2
2D Array: Sort the Boundary Elements
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 positive integers into this matrix.
Perform the following tasts on the matrix:
1. Sort the elements of the outer rows and columns in ascending order using any standard
sorting technique and arrange them in an array.
2. Calculate the sum of the outer row and column elements.
3. Output the original matrix, rearranged matrix and only the border elements of the
rearranged array with their sum.
Test your program for the following data and some random data:
Example 1:
INPUT: M = 3
N = 3
1 7 4
8 2 5
6 3 9
OUTPUT:
ORIGINAL MATRIX
1 7 4
8 2 5
6 3 9
REARRANGED MATRIX
1 3 4
9 2 5
8 7 6
BOUNDARY ELEMENTS
1 3 4
9 5
8 7 6
SUM OF THE OUTER ROW AND COLUMN ELEMENTS = 43
Example 2:
INPUT: M = 2
N = 3
7 1 6
8 9 2
OUTPUT:
ORIGINAL MATRIX
7 1 6
8 9 2
REARRANGED MATRIX
1 2 6
9 8 7
BOUNDARY ELEMENTS
1 2 6
9 8 7
SUM OF THE OUTER ROW AND COLUMN ELEMENTS = 33
Example 4:
INPUT: M = 4
N = 4
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
OUTPUT:
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
76 Chapter 10. ISC-2009
ORIGINAL MATRIX
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
REARRANGED MATRIX
1 2 4 5
23 13 8 7
15 6 3 8
12 11 9 8
BOUNDARY ELEMENTS
1 2 4 5
23 7
15 8
12 11 9 8
SUM OF THE OUTER ROW AND COLUMN ELEMENTS = 105
Solution
1 import java . util .*;
2
3 class Matrix {
4 private int A [][] , row , col , M , N , outer [] , sum ;
5
6 public Matrix ( int M , int N ) {
7 this . M = M ;
8 this . N = N ;
9 A = new int [ M ][ N ];
10 outer = new int [2 * N + 2 * ( M - 2) ];
11 }
12
13 public void input () {
14 Scanner in = new Scanner ( System . in ) ;
15 for ( row = 0; row < M ; row ++) {
16 for ( col = 0; col < N ; col ++) {
17 A [ row ][ col ] = in . nextInt () ;
18 }
19 }
20 }
21
22 public String toString () {
23 String output = " " ;
24 for ( row = 0; row < M ; row ++) {
25 output += " \ t " ;
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
78 Chapter 10. ISC-2009
109 }
110 }
ISC-2009, Question 3
Sorting words in a sentence in ascending order by length
Read a single sentence which terminates with a full stop(.). The words are to be separated
with a single blank space and are in lower case.
Arrange the words contained in the sentence according to the length of the words in ascending
order. If two words are of the same length then the word occurring first in the input sentence
should come first.
For both input and output, the sentence must begin in upper case.
Test your program for the following data and some random data.
INPUT: The lines are printed in reverse order.
OUTPUT: In the are lines order printed reverse.
Solution
1 import java . util .*;
2
3 class Sentence {
4 private String sentence , word [] , output ;
5
6 public Sentence ( String sentence ) {
7 this . sentence = sentence . toLowerCase () ;
8 word = this . sentence . split ( " [ ␣ .] " ) ;
9 sort () ;
10 }
11
12 public void sort () {
13 String temp ;
14 boolean swapped ;
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
80 Chapter 10. ISC-2009
ISC-2010, Question 1
Denomination Breakup
A bank intends to design a program to display the denomination of an input amount of upto
five digits. The available denomination with the bank are of rupees 1000, 500, 100, 50, 20, 10,
5, 2, 1.
Design a program to accept the amount from the user and display the breakup in descending
order of denomination, (i.e. preference shoud be given to the highest denomination available)
along with the total number of notes. [Note: Only the denominations used should be displayed.
Also print the amount in words according to the digits.
Example 1
INPUT: 14856
OUTPUT: ONE FOUR EIGHT FIVE SIX
DENOMINATION
1000 x 14 = 14000
500 x 1 = 500
100 x 3 = 300
50 x 1 = 50
5 x 1 = 5
1 x 1 = 1
TOTAL = 14586
82 Chapter 11. ISC-2010
Solution
1 import java . util .*;
2
3 class Amount {
4 private int amount ;
5 private String output ;
6 private static String digit [] = { " ZERO " , " ONE " , " TWO " ,
" THREE " , " FOUR " , " FIVE " , " SIX " , " SEVEN " , " EIGHT " , "
NINE " };
7 private static int notes [] = { 1000 , 500 , 100 , 50 , 20 ,
10 , 5 , 2 , 1 };
8
9 public Amount ( int amount ) {
10 this . amount = amount ;
11 output = " " ;
12 digitsToWord () ;
13 }
14
15 public String toString () {
16 return output ;
17 }
18
19 public void digitsToWord () {
20 for ( int temp = amount ; temp > 0; temp /= 10) {
21 output = digit [ temp % 10] + " ␣ " + output ;
22 }
23 output = output . trim () ;
24 }
25
26 public void showDenomination () {
27 System . out . println ( " \ tDENOMINATION " ) ;
28 int quantity , amt = amount , noteCount = 0;
29 for ( int note : notes ) {
30 quantity = amt / note ;
31 if ( quantity > 0) {
32 System . out . printf ( " \ t % -8 d ␣ x ␣ % -13 d ␣ = ␣ %9 d \ n " ,
note , quantity , ( note * quantity ) ) ;
33 noteCount += quantity ;
34 }
35 amt %= note ;
36 }
ISC-2010, Question 2
Kaprekar number
A positive whole number ‘n’ that has ‘d’ number of digits is squared and split into two pieces,
a right-hand piece that has ‘d’ digits and a left-hand piece that has remaining ‘d’ or ‘d-1’ digits.
If the sum of the two pieces is equal to the number, then ‘n’ is a Kaprekar number. The first
few Kaprekar numbers are: 9, 45, 297, ...
Example 1:
9
9*9 = 81,
right-hand piece of 81 = 1 and left hand piece of 81 = 8
Sum = 1 + 8 = 9, i.e. equal to the number.
Example 2:
45
45*45 = 2025,
right-hand piece of 2025 = 25 and left hand piece of 2025 = 20
Sum = 25 + 20 = 45, i.e. equal to the number.
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
84 Chapter 11. ISC-2010
Example 3:
297
297*297 = 88209,
right-hand piece of 88209 = 209 and left hand piece of 88209 = 88
Sum = 209 + 88 = 297, i.e. equal to the number.
Given the two positive integers p and q, where p < q, write a program to determine how many
Kaprekar numbers are there in the range between p and q (both inclusive) and output them.
The input contains two positive integers p and q. Assume p < 5000 and q < 5000. You are to
output the number of Kaprekar numbers in the specified range along with their values in the
format specified below:
SAMPLE DATA:
INPUT:
p = 1
q = 1000
OUTPUT:
THE KAPREKAR NUMBERS ARE:-
1, 9, 45, 55, 99, 297, 703, 999
FREQUENCY OF KAPREKAR NUMBERS IS: 8
Solution
1 import java . util .*;
2
3 class Kaprekar {
4 private int p , q , frequency ;
5 String output ;
6
7 public Kaprekar ( int p , int q ) {
8 this . p = p ;
9 this . q = q ;
10 output = " " ;
11 processRange () ;
12 }
13
14 private void processRange () {
15 for ( int i = p ; i <= q ; i ++) {
16 if ( isKaprekarNumber ( i ) ) {
17 output += i + " ,␣ " ;
18 frequency ++;
19 }
20 }
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
86 Chapter 11. ISC-2010
ISC-2010, Question 3
Frequency table of words
Input a paragraph containing ‘n’ number of sentences where (1 =< n < 4). The words are to
be separated with a single blank space and are in UPPERCASE. A sentence may be terminated
either with a full stop ‘.’ or a question mark ‘?’ only.
Any other character may be ignored. Perform the following operations:
1. Accept the number of sentences. If the number of sentences exceeds the limit, an
appropriate error message must be displayed.
2. Find the number of words in the whole paragraph.
3. Display the words in ascending order of their frequency.
Words with same frequency may appear in any order.
Example 1
INPUT:
Enter number of sentences: 1
Enter sentences
TO BE OR NOT TO BE.
OUTPUT:
Total number of words: 6
WORD FREQUENCY
OR 1
NOT 1
TO 2
BE 2
Example 2
INPUT:
Enter number of sentences: 3
Enter sentences:
THIS IS A STRING PROGRAM.IS THIS EASY?YES,IT IS.
OUTPUT:
Total number of words: 11
WORD FREQUENCY
A 1
STRING 1
PROGRAM 1
EASY 1
YES 1
IT 1
THIS 2
IS 3
Example 3
INPUT:
Enter number of sentences: 5
OUTPUT:
INVALID ENTRY
Solution
1 import java . util .*;
2
3 class Frequency {
4 private String word [];
5 private int frequency [];
6 private String output ;
7
8 public Frequency ( String paragraph ) {
9 output = " " ;
10 word = paragraph . split ( " [.? , ␣ ] " ) ;
11 frequency = new int [ word . length ];
12 prepareFrequencyTable () ;
13 }
14
15 private void prepareFrequencyTable () {
16 // Populating the arrays
17 int limit = word . length ;
18 for ( int i = 0; i < limit ; i ++) {
19 frequency [ i ] = 1;
20 for ( int j = i + 1; j < limit ; j ++) {
21 if ( word [ i ]. equals ( word [ j ]) ) {
22 word [ j ] = word [ limit - 1];
23 frequency [ i ]++;
24 limit - -;
25 j - -;
26 }
27 }
28 }
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
88 Chapter 11. ISC-2010
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
12. ISC-2011
ISC-2011, Question 1
Number to words
Write a program to input a natural number less than 1000 and output it in words. Test your
program for the following set of data:
INPUT: 29
OUTPUT: TWENTY NINE
INPUT: 17001
OUTPUT: OUT OF RANGE
INPUT: 119
OUTPUT: ONE HUNDRED NINETEEN
INPUT: 500
OUTPUT: FIVE HUNDRED
Solution
1 import java . util .*;
2
3 class Number {
4 private int n ;
92 Chapter 12. ISC-2011
40 break ;
41 }
42 }
43
44 public String toString () {
45 return answer . trim () ;
46 }
47 }
48
49 class ISC2011Q1 {
50 public static void main ( String [] args ) {
51 Scanner in = new Scanner ( System . in ) ;
52 System . out . print ( " INPUT :\ t " ) ;
53 int n = in . nextInt () ;
54 if ( n > 0 && n < 1000) {
55 Number obj = new Number ( n ) ;
56 System . out . println ( " OUTPUT :\ t " + obj ) ;
57 } else {
58 System . out . println ( " OUTPUT : ␣ OUT ␣ OF ␣ RANGE " ) ;
59 }
60 in . close () ;
61 }
62 }
ISC-2011, Question 2
Encryption based on character position
Encryption is a technique of coding messages to maintain their secrecy. A String array of size
‘n’ where ‘n’ is greater than 1 and less than 10, stores single sentences (each sentence ends
with a full stop) in each row of the array.
Write a program to accept the size of the array. Display an appropriate message if the size is
not satisfying the given condition.
Define a string array of the inputted size and fill it with sentences row-wise.
Change the sentence of the odd rows with an encryption of two characters ahead of the original
character. Also change the sentence of the even rows by storing the sentence in reverse order.
Display the encrypted sentences as per the sample data given below.
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
94 Chapter 12. ISC-2011
Test your program on the sample data and some random data.
Input: n=4
IT IS CLOUDY.
IT MAY RAIN.
THE WEATHER IS FINE.
IT IS COOL.
Output:
KV KU ENQWFA.
RAIN MAY IT.
VJG YGCVJGT KU HKPG.
COOL IS IT.
Input: n=13
Output: INVALID ENTRY
Solution
1 import java . util .*;
2
3 class Encrypt {
4 private String sentence [] , answer ;
5
6 public Encrypt ( String sentence []) {
7 this . sentence = sentence . clone () ;
8 answer = " " ;
9 encrypt () ;
10 }
11
12 private void encrypt () {
13 int lineCount = 0;
14 char ch ;
15 String output , words [];
16 for ( String line : sentence ) {
17 output = " " ;
18 lineCount ++;
19 line = line . substring (0 , line . length () - 1) ;
20 if ( lineCount % 2 == 1) {
21 // odd line
22 for ( int position = 0; position < line .
length () ; position ++) {
23 ch = line . charAt ( position ) ;
24 if ( Character . isLetter ( ch ) ) {
25 ch = ( char ) ( ch + 2) ;
26 if ( ch > 'Z ')
27 ch = ( char ) ( ch - 26) ;
28
29 }
30 output += ch ;
31 }
32
33 } else {
34 // even line
35 words = line . split ( " ␣ " ) ;
36 for ( String word : words ) {
37 output = word + " ␣ " + output ;
38 }
39 }
40 output = output . trim () ;
41 output += " . " ;
42 answer += output + " \ n " ;
43 }
44 }
45
46 public String toString () {
47 return answer ;
48 }
49 }
50
51 class ISC2011Q2 {
52 public static void main ( String [] args ) {
53 Scanner in = new Scanner ( System . in ) ;
54 System . out . print ( " INPUT :\ tn ␣ = ␣ " ) ;
55 int n = in . nextInt () ;
56 in . nextLine () ;
57 if ( n > 1 && n < 10) {
58 String sentence [] = new String [ n ];
59 for ( int i = 0; i < n ; i ++) {
60 sentence [ i ] = in . nextLine () ;
61 }
62 Encrypt obj = new Encrypt ( sentence ) ;
63 System . out . println ( " OUTPUT : " ) ;
64 System . out . println ( obj ) ;
65 } else {
66 System . out . println ( " OUTPUT \ t : INVALID ␣ ENTRY " ) ;
67 }
68 in . close () ;
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
96 Chapter 12. ISC-2011
69 }
70 }
ISC-2011, Question 3
Date Validation
Design a program which accepts your date of birth in dd mm yyyy format. Check whether the
date entered is valid or not. If it is valid, display “VALID DATE”, also compute and display
the day number of the year for the date of birth. If it is invalid, display “INVALID DATE” and
then terminate the program.
Test your program for the given sample data and some random data.
Example 1
Input: Enter your date of birth in dd mm yyyy format
05
01
2010
Output:
VALID DATE
5
Example 2
Input: Enter your date of birth in dd mm yyyy format
03
04
2010
Output:
VALID DATE
93
Example 3
Input: Enter your date of birth in dd mm yyyy format
34
06
2010
Output:
INVALID DATE
Solution
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
98 Chapter 12. ISC-2011
ISC-2012, Question 1
Prime palindrome
A prime palindrome integer is a positive integer (without leading zeros) which is prime as well
as a palindrome.
Given two positive integers m and n, where m < n, write a program to determine how many
prime-palindrome integers are there in the range between m and n (both inclusive) and output
them.
The input contains two positive integers m and n where m < 3000 and n < 3000. Display the
number of prime-palindrome integers in the specified range along with their values in the
format specified below:
Test your program with the sample data and some random data:
Example 1
INPUT: m = 100
n = 1000
OUTPUT: THE PRIME PALINDROME INTEGERS ARE:
101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 797, 919, 929
FREQUENCY OF PRIME PALINDROME INTEGERS: 15
Example 2
100 Chapter 13. ISC-2012
INPUT: m = 100
n = 5000
OUTPUT: OUT OF RANGE
Solution
1 import java . util .*;
2
3 class PrimePalindrome {
4 private int m , n , frequency ;
5 private String output ;
6
7 public PrimePalindrome ( int m , int n ) {
8 this . m = m ;
9 this . n = n ;
10 process () ;
11 }
12
13 private static boolean isPrime ( int n ) {
14 if ( n < 2)
15 return false ;
16 else if ( n == 2)
17 return true ;
18 else if ( n % 2 == 0)
19 return false ;
20 else {
21 int limit = ( int ) Math . sqrt ( n ) ;
22 for ( int divisor = 3; divisor <= limit ; divisor
+= 2) {
23 if ( n % divisor == 0)
24 return false ;
25 }
26 }
27 return true ;
28 }
29
30 private static boolean isPalindrome ( int n ) {
31 int reverse = 0;
32 for ( int temp = n ; temp > 0; temp /= 10) {
33 reverse = reverse * 10 + ( temp % 10) ;
34 }
35 return n == reverse ;
36 }
37
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
102 Chapter 13. ISC-2012
ISC-2012, Question 2
Sort words of a string
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:
1. Obtain the length of the sentence (measured in words).
2. 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
Solution
1 import java . util .*;
2
3 class Sentence {
4 private String sentence , output ;
5 private int length ;
6
7 public Sentence ( String sentence ) {
8 this . sentence = sentence . substring (0 , sentence .
length () - 1) ;
9 process () ;
10 }
11
12 private void process () {
13 String word [] = sentence . split ( " ␣ " ) ;
14 length = word . length ;
15 sort ( word ) ;
16 output = " " ;
17 for ( String w : word ) {
18 output += w + " ␣ " ;
19 }
20 output = output . trim () ;
21 }
22
23 private void sort ( String word []) {
24 String temp ;
25 boolean swapped ;
26 for ( int pass = 0; pass < word . length - 1; pass ++) {
27 swapped = false ;
28 for ( int pos = 0; pos < word . length - 1 - pass ;
pos ++) {
29 if ( word [ pos + 1]. compareTo ( word [ pos ]) < 0)
{
30 temp = word [ pos + 1];
31 word [ pos + 1] = word [ pos ];
32 word [ pos ] = temp ;
33 swapped = true ;
34 }
35 }
36 if (! swapped )
37 break ;
38 }
39 }
40
41 public int getLength () {
42 return length ;
43 }
44
45 public String toString () {
46 return output ;
47 }
48 }
49
50 public class ISC2012Q2 {
51 public static void main ( String [] args ) {
52 Scanner in = new Scanner ( System . in ) ;
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
104 Chapter 13. ISC-2012
ISC-2012, Question 3
Sort a 2D array in ascending order
Write a program to declare a matrix A [ ] [ ] of order (MXN) 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:
1. Display the input matrix
2. Find the maximum and minimum value in the matrix and display them along with their
position.
3. Sort the elements of the matrix in ascending order using any standard sorting technique
and rearrange them in the matrix.
4. Output the rearranged matrix.
Test your program with the sample data and some random data:
Example 1
INPUT: M = 3
N = 4
8 7 9 3
-2 0 4 5
1 3 6 -4
OUTPUT:
ORIGINAL MATRIX
8 7 9 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 3 4 5
6 7 8 9
Example 2
INPUT: M = 3
N = 22
OUTPUT:
SIZE OUT OF RANGE
Solution
1 import java . util .*;
2
3 class Matrix {
4 private int A [][] , M , N , minRow , minCol , maxRow , maxCol ,
min , max ;
5
6 public Matrix ( int M , int N ) {
7 A = new int [ M ][ N ];
8 this . M = M ;
9 this . N = N ;
10 }
11
12 public void getInput () {
13 Scanner in = new Scanner ( System . in ) ;
14 for ( int row = 0; row < M ; row ++) {
15 for ( int col = 0; col < N ; col ++) {
16 A [ row ][ col ] = in . nextInt () ;
17 }
18 }
19 in . close () ;
20 }
21
22 public void computeMinimumPosition ( int startRow , int
startCol ) {
23 min = A [ startRow ][ startCol ];
24 minRow = startRow ;
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
106 Chapter 13. ISC-2012
25 minCol = startCol ;
26 for ( int row = startRow ; row < M ; row ++) {
27 // startRow =0;
28 for ( int col = startCol ; col < N ; col ++) {
29 startCol = 0;
30 if ( A [ row ][ col ] < min ) {
31 min = A [ row ][ col ];
32 minRow = row ;
33 minCol = col ;
34 }
35 }
36 }
37 }
38
39 public void computeMaxiumPosition ( int startRow , int
startCol ) {
40 max = A [ startRow ][ startCol ];
41 maxRow = startRow ;
42 maxCol = startCol ;
43 for ( int row = startRow ; row < M ; row ++) {
44 // startRow =0;
45 for ( int col = startCol ; col < N ; col ++) {
46 startCol = 0;
47 if ( A [ row ][ col ] > max ) {
48 max = A [ row ][ col ];
49 maxRow = row ;
50 maxCol = col ;
51 }
52 }
53 }
54 }
55
56 public String toString () {
57 String output = " " ;
58 for ( int row = 0; row < M ; row ++) {
59 output += " \ t " ;
60 for ( int col = 0; col < N ; col ++) {
61 output += A [ row ][ col ];
62 if ( col < N - 1)
63 output += " \ t " ;
64 }
65 output += " \ n " ;
66 }
67 return output ;
68 }
69
70 public void showMaxDetails () {
71 computeMaxiumPosition (0 , 0) ;
72 System . out . println ( " LARGEST ␣ NUMBER : ␣ " + max ) ;
73 System . out . println ( " \ tROW ␣ = ␣ " + maxRow ) ;
74 System . out . println ( " \ tCOLUMN ␣ = ␣ " + maxCol ) ;
75 }
76
77 public void showMinDetails () {
78 computeMinimumPosition (0 , 0) ;
79 System . out . println ( " SMALLEST ␣ NUMBER : ␣ " + min ) ;
80 System . out . println ( " \ tROW ␣ = ␣ " + minRow ) ;
81 System . out . println ( " \ tCOLUMN ␣ = ␣ " + minCol ) ;
82 }
83
84 public void sortAscending () {
85 int temp ;
86 for ( int row = 0; row < M ; row ++) {
87 for ( int col = 0; col < N ; col ++) {
88 computeMinimumPosition ( row , col ) ;
89 temp = A [ minRow ][ minCol ];
90 A [ minRow ][ minCol ] = A [ row ][ col ];
91 A [ row ][ col ] = temp ;
92 }
93 }
94 }
95 }
96
97 public class ISC2012Q3 {
98 public static void main ( String [] args ) {
99 Scanner in = new Scanner ( System . in ) ;
100 System . out . print ( " INPUT :\ tM ␣ = ␣ " ) ;
101 int M = in . nextInt () ;
102 System . out . print ( " \ tN ␣ = ␣ " ) ;
103 int N = in . nextInt () ;
104 if ( M > 2 && N > 2 && M < 20 && N < 20) {
105 Matrix obj = new Matrix (M , N ) ;
106 obj . getInput () ;
107 System . out . println ( " OUPUT : " ) ;
108 System . out . println ( " ORIGINAL ␣ MATRIX " ) ;
109 System . out . println ( obj ) ;
110 obj . showMaxDetails () ;
111 obj . showMinDetails () ;
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
108 Chapter 13. ISC-2012
ISC-2013, Question 1
Verify an ISBN number
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:
02011003311 = 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 divisible by 11, hence it is 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:
110 Chapter 14. ISC-2013
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
Solution
1 import java . util .*;
2
3 class ISBN {
4 private String isbn ;
5 private int sum ;
6
7 public ISBN ( String isbn ) {
8 this . isbn = isbn ;
9 if ( isbn . length () != 10)
10 throw new IllegalArgumentException () ;
11
12 for ( int position = 0; position < 9; position ++) {
13 if (! Character . isDigit ( isbn . charAt ( position ) ) ) {
14 throw new IllegalArgumentException () ;
15 }
16 }
17 char lastCharacter = isbn . charAt (9) ;
18 if (!( Character . isDigit ( lastCharacter ) || (
lastCharacter == 'x ' || lastCharacter == 'X ') ) ) {
19 throw new IllegalArgumentException () ;
20 }
21 computeSum () ;
22 }
23
24 public void computeSum () {
25 int digit ;
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
112 Chapter 14. ISC-2013
ISC-2013, Question 2
Mirror image of a 2D array
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:
1. Display the input matrix.
2. Create a mirror image of the inputted matrix.
3. Display the mirror image matrix.
Test your program for the following data and some random data:
Example 1
INPUT: M = 3
4 16 12
8 2 14
6 1 3
OUTPUT: ORIGINAL MATRIX
4 16 12
8 2 14
6 1 3
Example 2
INPUT : M = 22
OUTPUT : SIZE OUT OF RANGE
Solution
1 import java . util .*;
2
3 class Matrix {
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
114 Chapter 14. ISC-2013
ISC-2013, Question 3
Count palindromic words
A palindrome is a word that may be read the same way in either direction. Accept a sentence
in UPPER CASE which is terminated by either “.”, “?”, or “!”. Each word of the sentence is
separated by a single blank space.
Perform the following tasks:
1. Display the count of palindromic words in the sentence.
2. Display the palindromic words in the sentence.
Example of palindromic words:
Test your program with the sample data and some random data:
Example 1
Example 2
INPUT: NITIN ARORA USES LIRIL SOAP.
OUTPUT: NITIN ARORA LIRIL
NUMBER OF PALINDROMIC WORDS: 3
Example 3
INPUT: HOW ARE YOU?
OUTPUT: NO PALINDROMIC WORDS
Solution
1 import java . util .*;
2
3 class Palindrome {
4 private String sentence , answer ;
5 private int frequency ;
6
7 public Palindrome ( String sentence ) {
8 this . sentence = sentence ;
9 frequency = 0;
10 answer = " " ;
11 process () ;
12 }
13
14 private boolean isPalindrome ( String word ) {
15 int half = word . length () / 2;
16 for ( int position = 0; position < half ; position ++)
{
17 if ( word . charAt ( position ) != word . charAt ( word .
length () - 1 - position ) )
18 return false ;
19 }
20 return true ;
21 }
22
23 private void process () {
24 char ch ;
25 String delimters = " .?! ␣ " , word = " " ;
26 for ( int position = 0; position < sentence . length () ;
position ++) {
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
116 Chapter 14. ISC-2013
ISC-2014, Question 1
Composite magic number
Input: m=10
n=100
Output: THE COMPOSITE INTEGERS ARE:
10, 28, 46, 55, 64, 82, 91, 100
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 8
118 Chapter 15. ISC-2014
Example 2:
Input: m=1200
n=1300
Output: THE COMPOSITE 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
Solution
1 import java . util .*;
2
3 class CompositeMagicNumber {
4 private int m , n , frequency ;
5 String output ;
6
7 public CompositeMagicNumber ( int m , int n ) {
8 this . m = m ;
9 this . n = n ;
10 }
11
12 public void process () {
13 frequency = 0;
14 output = " THE ␣ COMPOSITE ␣ INTEGERS ␣ ARE :\ n \ t " ;
15 for ( int i = m ; i <= n ; i ++) {
16 if ( isMagicNumber ( i ) && isCompositeNumber ( i ) ) {
17 frequency ++;
18 output += i + " ,␣ " ;
19 }
20 }
21 output = output . substring (0 , output . length () - 2) ;
22 }
23
24 public String toString () {
25 return output ;
26 }
27
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
120 Chapter 15. ISC-2014
ISC-2014, Question 2
Symmetry and sum of diagonals of a 2D array
Write a program to declare a square matrix A[][] of order MxM 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:
1. Display the original matrix.
2. Check if the given matrix is symmetric or not. A square matrix is said to be symmetric,
if the element in the i th row and j th column is equal to the element of the j th row and i
th column.
3. Find the sum of the elements of left diagonal and the sum of the elements of right
diagonal of the matrix and display them.
Test your program for the following data and some random data:
Example 1
INPUT: M = 3
1 2 3
2 4 5
3 5 6
OUTPUT: ORIGINAL MATRIX
1 2 3
2 4 5
3 5 6
THE GIVEN MATRIX IS SYMMETRIC
The sum of the left diagonal = 11
The sum of the right diagonal = 10
Example 2
INPUT: M = 4
7 8 9 2
4 5 6 3
8 5 3 1
7 6 4 2
OUTPUT: ORIGINAL MATRIX
7 8 9 2
4 5 6 3
8 5 3 1
7 6 4 2
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: THE MATRIX SIZE IS OUT OF RANGE
Solution
1 import java . util .*;
2
3 class Matrix {
4 private int A [][] , M ;
5
6 public Matrix ( int M ) {
7 this . M = M ;
8 A = new int [ M ][ M ];
9 }
10
11 public String toString () {
12 String output = " " ;
13 for ( int row = 0; row < M ; row ++) {
14 output += " \ t " ;
15 for ( int col = 0; col < M ; col ++) {
16 output += A [ row ][ col ];
17 if ( col < M - 1)
18 output += " \ t " ;
19 }
20 output += " \ n " ;
21 }
22 return output ;
23 }
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
122 Chapter 15. ISC-2014
24
25 public void getInput () {
26 Scanner in = new Scanner ( System . in ) ;
27 for ( int row = 0; row < M ; row ++) {
28 for ( int col = 0; col < M ; col ++) {
29 A [ row ][ col ] = in . nextInt () ;
30 }
31 }
32 in . close () ;
33 }
34
35 public boolean isSymmetric () {
36 for ( int row = 0; row < M ; row ++) {
37 for ( int col = 0; col < row ; col ++) {
38 if ( A [ row ][ col ] != A [ col ][ row ])
39 return false ;
40 }
41 }
42 return true ;
43 }
44
45 public void computeAndDisplaySumOfDiagonals () {
46 int sumOfLeftDiagonal = 0 , sumOfRightDiagonal = 0;
47 for ( int i = 0; i < M ; i ++) {
48 sumOfLeftDiagonal += A [ i ][ i ];
49 sumOfRightDiagonal += A [ i ][ M - 1 - i ];
50 }
51 System . out . println ( " The ␣ sum ␣ of ␣ the ␣ left ␣ diagonal ␣ = ␣ "
+ sumOfLeftDiagonal ) ;
52 System . out . println ( " The ␣ sum ␣ of ␣ the ␣ right ␣ diagonal ␣ = ␣
" + sumOfRightDiagonal ) ;
53 }
54 }
55
56 class ISC2014Q2 {
57 public static void main ( String [] args ) {
58 Scanner in = new Scanner ( System . in ) ;
59 System . out . print ( " INPUT :\ tM ␣ = ␣ " ) ;
60 int M = in . nextInt () ;
61 if ( M > 2 && M < 10) {
62 Matrix objMatrix = new Matrix ( M ) ;
63 objMatrix . getInput () ;
64 System . out . println ( " OUTPUT :\ tORIGINAL ␣ MATRIX " ) ;
65 System . out . println ( objMatrix ) ;
66 if ( objMatrix . isSymmetric () ) {
67 System . out . println ( " THE ␣ GIVEN ␣ MATRIX ␣ IS ␣
SYMMETRIC " ) ;
68 } else {
69 System . out . println ( " THE ␣ GIVEN ␣ MATRIX ␣ IS ␣ NOT ␣
SYMMETRIC " ) ;
70 }
71 objMatrix . computeAndDisplaySumOfDiagonals () ;
72 } else {
73 System . out . println ( " OUTPUT :\ tTHE ␣ MATRIX ␣ SIZE ␣ IS ␣
OUT ␣ OF ␣ RANGE " ) ;
74 }
75 in . close () ;
76 }
77 }
ISC-2014, Question 3
Remove redundant spaces and delete a word
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 seperated by more than one blank space and are in UPPER CASE.
Perform the following tasks:
1. Accept the sentence and reduce all the extra blank space between two words to a single
blank space.
2. Accept a word from the user which is a part of the sentence along with its position
number and delete the word and display the sentence.
Test your program for the following data and some random data:
Example 1
Example 2
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
124 Chapter 15. ISC-2014
Example 3
Solution
1 import java . util .*;
2
3 class Sentence {
4 private String source , wordList [] , answer ;
5 private char terminator ;
6
7 public Sentence ( String source ) {
8 this . source = source ;
9 }
10
11 public boolean isValid () {
12 terminator = source . charAt ( source . length () - 1) ;
13 return ( new String ( " .?! " ) ) . indexOf ( terminator ) > -1;
14 }
15
16 public void delete ( String wordToBeDeleted , int position )
{
17 answer = " " ;
18 wordList = source . split ( " ␣ " ) ;
19 System . out . println ( Arrays . toString ( wordList ) ) ;
20 int wordCount = 0;
21 for ( String word : wordList ) {
22 if ( word . length () == 0)
23 continue ;
24 wordCount ++;
25 if ( wordCount == position && word . equals (
wordToBeDeleted ) )
26 continue ;
27 answer += word + " ␣ " ;
28 }
29 }
30
31 public String toString () {
32 return answer . trim () ;
33 }
34 }
35
36 class ISC2014Q3 {
37 public static void main ( String [] args ) {
38 Scanner in = new Scanner ( System . in ) ;
39 System . out . print ( " INPUT :\ t " ) ;
40 String source = in . nextLine () ;
41 Sentence objSentence = new Sentence ( source ) ;
42
43 if ( objSentence . isValid () ) {
44 System . out . print ( " WORD ␣ TO ␣ BE ␣ DELETED : ␣ " ) ;
45 String wordToBeDeleted = in . nextLine () ;
46 System . out . print ( " WORD ␣ POSITION ␣ IN ␣ THE ␣ SENTENCE :
␣");
47 int position = in . nextInt () ;
48 objSentence . delete ( wordToBeDeleted , position ) ;
49 System . out . println ( " OUTPUT :\ t " + objSentence ) ;
50 } else {
51 System . out . println ( " OUTPUT :\ tINVALID ␣ INPUT . " ) ;
52 }
53 in . close () ;
54 }
55 }
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
16. ISC-2015
ISC-2015, Question 1
Sum of digits to a given limit
Given two positive numbers M and N, such that M is between 100 and 10000 and N is less
than 100.
Find the smallest integer that is greater than M and whose digits add up to N. For example, if
M = 100 and N = 11, then the smallest integer greater than 100 whose digits add up to 11 is
119.
Write a program to accept the numbers M and N from the user and print the smallest required
number whose sum of all its digits is equal to N.
Also, print the total number of digits present in the required number.
The program should check for the validity of the inputs and display an appropriate message
for an invalid input.
Test your program with the sample data and some random data:
Example 1
INPUT: M = 100
N = 11
128 Chapter 16. ISC-2015
Example 2
INPUT: M = 1500
N = 25
Example 3
INPUT: M = 99
N = 11
Example 4
INPUT: M = 112
N = 130
Solution
1 import java . util .*;
2
3 class Number {
4 private int M , N ;
5 String output ;
6 private boolean status ;
7
8 public Number ( int M , int N ) {
9 this . M = M ;
10 this . N = N ;
11 output = " " ;
12 status = false ;
13 }
14
15 public void findSolution () {
16 int M = this . M ;
17 int sum = 0;
18 do {
19 sum = sumOfDigits ( M ) ;
20 if ( sum == N ) {
21 output = " OUTPUT :\ tThe ␣ required ␣ number ␣ = ␣ " +
M + " \ n \ tTotal ␣ number ␣ of ␣ digits ␣ = ␣ " +
countDigits ( M ) ;
22 status = true ;
23 }
24 M ++;
25 if ( M == Integer . MAX_VALUE ) {
26 status = false ;
27 break ;
28 }
29 } while ( sum != N ) ;
30 }
31
32 public boolean hasSolution () {
33 return status ;
34 }
35
36 private int sumOfDigits ( int n ) {
37 int sum = 0;
38 for ( int temp = n ; temp > 0; temp /= 10) {
39 sum += temp % 10;
40 }
41 return sum ;
42 }
43
44 private int countDigits ( int n ) {
45 int count = 0;
46 for ( int temp = n ; temp > 0; temp /= 10) {
47 count ++;
48 }
49 return count ;
50 }
51
52 public String toString () {
53 return output ;
54 }
55 }
56
57 class ISC2015Q1 {
58 public static void main ( String [] args ) {
59 Scanner in = new Scanner ( System . in ) ;
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
130 Chapter 16. ISC-2015
ISC-2015, Question 2
Rotate a 2D matrix
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 10. Accept
the value of M as user input. Display an appropriate message for an invalid input. Allow the
user to input integers into the matrix.
3. Find the sum of the elements of the four corners of the matrix.
Test your program for the following data and some random data:
Example 1:
INPUT: M = 3
3 4 9
2 5 8
1 6 7
OUTPUT:
ORIGINAL MATRIX
3 4 9
2 5 8
1 6 7
MATRIX AFTER ROTATION
1 2 3
6 5 4
7 8 9
Sum of the corner elements = 20
Example 2:
INPUT: M = 4
1 2 4 9
2 5 8 3
1 6 7 4
3 7 6 5
OUTPUT:
ORIGINAL MATRIX
1 2 4 9
2 5 8 3
1 6 7 4
3 7 6 5
MATRIX AFTER ROTATION
3 1 2 1
7 6 5 2
6 7 8 4
5 4 3 9
Sum of the corner elements = 18
EXAMPLE 3
INPUT: M = 14
OUTPUT: SIZE OUT OF RANGE
Solution
1 import java . util .*;
2
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
132 Chapter 16. ISC-2015
3 class Matrix {
4 private int A [][] , M ;
5
6 public Matrix ( int M ) {
7 A = new int [ M ][ M ];
8 this . M = M ;
9 }
10
11 public void getInput () {
12 Scanner in = new Scanner ( System . in ) ;
13 for ( int row = 0; row < M ; row ++) {
14 for ( int col = 0; col < M ; col ++) {
15 A [ row ][ col ] = in . nextInt () ;
16 }
17 }
18 in . close () ;
19 }
20
21 public String toString () {
22 String output = " " ;
23 for ( int row = 0; row < M ; row ++) {
24 output += " \ t " ;
25 for ( int col = 0; col < M ; col ++) {
26 output += A [ row ][ col ];
27 if ( col < M )
28 output += " \ t " ;
29 }
30 output += " \ n " ;
31 }
32 return output ;
33 }
34
35 public void rotate () {
36 int half = M / 2 , temp ;
37 for ( int i = 0; i < half ; i ++) {
38 for ( int x = i ; x < M - i - 1; x ++) {
39 temp = A [ M - x - 1][ i ];
40 A [ M - x - 1][ i ] = A [ M - i - 1][ M - x - 1];
41 A [ M - i - 1][ M - x - 1] = A [ x ][ M - i - 1];
42 A [ x ][ M - i - 1] = A [ i ][ x ];
43 A [ i ][ x ] = temp ;
44 }
45 }
46 }
47
48 public int getCornerSum () {
49 return A [0][0] + A [0][ M - 1] + A [ M - 1][ M - 1] + A [ M
- 1][0];
50 }
51 }
52
53 public class ISC2015Q2 {
54 public static void main ( String [] args ) {
55 Scanner in = new Scanner ( System . in ) ;
56 System . out . print ( " INPUT :\ tM ␣ = ␣ " ) ;
57 int M = in . nextInt () ;
58 if ( M > 2 && M < 10) {
59 Matrix objMatrix = new Matrix ( M ) ;
60 objMatrix . getInput () ;
61 System . out . println ( " OUTPUT : " ) ;
62 System . out . println ( " ORIGINAL ␣ MATRIX " ) ;
63 System . out . println ( objMatrix ) ;
64 objMatrix . rotate () ;
65 System . out . println ( " MATRIX ␣ AFTER ␣ ROTATION " ) ;
66 System . out . println ( objMatrix ) ;
67 System . out . println ( " Sum ␣ of ␣ the ␣ corner ␣ elements ␣ =
␣ " + objMatrix . getCornerSum () ) ;
68 } else {
69 System . out . println ( " OUTPUT : ␣ SIZE ␣ OUT ␣ OF ␣ RANGE " ) ;
70 }
71 in . close () ;
72 }
73 }
ISC-2015, Question 3
Frequency of vowels and consonants
Write a program to accept a sentence which may be terminated by either ‘.’ Or ‘?’ only.
The words are to be separated by a single blank space. Print an error message if the input does
not terminate with ‘.’ Or ‘?’.
You can assume that no word in the sentence exceeds 15 characters, so that you get a proper
formatted output.
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
134 Chapter 16. ISC-2015
Example 2
INPUT: God is great.
OUTPUT: God Is Great
Word Vowels Consonants
God 1 2
Is 1 1
Great 2 3
Example 3
INPUT: All the best!
OUTPUT: INVALID INPUT
Solution
1 import java . util .*;
2
3 class Sentence {
4 private String sentence ;
5 private static String vowels = " aeiouAEIOU " ;
6
7 public Sentence ( String sentence ) {
8 this . sentence = sentence ;
9 }
10
11 public boolean isValid () {
12 return ( new String ( " .? " ) ) . indexOf ( sentence . charAt (
sentence . length () - 1) ) >= 0;
13 }
14
15 public String toString () {
16 String output = " " ;
17 String source = sentence . substring (0 , sentence .
length () - 1) ;
18 String wordList [] = source . split ( " ␣ " ) ;
19 for ( String word : wordList ) {
20 output += Character . toUpperCase ( word . charAt (0) )
+ ( word . substring (1) ) . toLowerCase () + " ␣ " ;
21 }
22 return output . trim () ;
23 }
24
25 private static boolean isVowel ( char ch ) {
26 return vowels . indexOf ( ch ) >= 0;
27 }
28
29 private static boolean isConsonant ( char ch ) {
30 return Character . isLetter ( ch ) && ! isVowel ( ch ) ;
31 }
32
33 public void displayStats () {
34 System . out . printf ( " % -15 s ␣ % -12 s ␣ % -12 s \ n " , " Word " , "
Vowels " , " Consonants " ) ;
35 String source = sentence . substring (0 , sentence .
length () - 1) ;
36 String wordList [] = source . split ( " ␣ " ) ;
37 String titleCase = " " ;
38 int vowels = 0 , consonants = 0;
39 char ch ;
40 for ( String word : wordList ) {
41 titleCase = Character . toUpperCase ( word . charAt (0)
) + ( word . substring (1) ) . toLowerCase () + " ␣ " ;
42 vowels = 0;
43 consonants = 0;
44 for ( int position = 0; position < titleCase .
length () ; position ++) {
45 ch = titleCase . charAt ( position ) ;
46 if ( isVowel ( ch ) )
47 vowels ++;
48 else if ( isConsonant ( ch ) )
49 consonants ++;
50 }
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
136 Chapter 16. ISC-2015
ISC-2016, Question 1
Circular prime number
A Circular Prime is a prime number that remains prime under cyclic shifts of its digits. When
the leftmost digit is removed and replaced at the end of the remaining string of digits, the
generated number is still prime. The process is repeated until the original number is reached
again. A number is said to be prime if it has only two factors I and itself.
Example:
131
311
113
Hence, 131 is a circular prime.
Test your program with the sample data and some random data:
Example 1
INPUT: N = 197
OUTPUT: 197
971
719
197 IS A CIRCULAR PRIME
Example 2
138 Chapter 17. ISC-2016
INPUT: N = 1193
OUTPUT: 1193
1931
9311
3119
1193 IS A CIRCULAR PRIME
Example 3
INPUT: N = 29
OUTPUT: 29
92
29 IS NOT A CIRCULAR PRIME
Solution
1 import java . util .*;
2
3 class CircularPrime {
4 private int N , copy , divisor ;
5 private boolean isPrime ;
6
7 public CircularPrime ( int N ) {
8 this . N = N ;
9 copy = N ;
10 divisor = 1;
11 for ( int temp = N ; temp > 9; temp /= 10) {
12 divisor *= 10;
13 }
14 isPrime = true ;
15 }
16
17 private void shift ( int n ) {
18 copy = ( n % divisor ) * 10 + ( n / divisor ) ;
19 }
20
21 public void display () {
22 do {
23 System . out . println ( " \ t " + copy ) ;
24 if (! isPrime ( copy ) ) {
25 isPrime = false ;
26 }
27 shift ( copy ) ;
28 } while ( N != copy ) ;
29 if ( isPrime ) {
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
140 Chapter 17. ISC-2016
ISC-2016, Question 2
Sorting of boundary elements
Write a program to declare a square matrix A[][] of order (MxM) where ‘M’ must be greater
than 3 and less than 10. Allow the user to input positive integers into this matrix.
Perform the following tasks on the matrix:
1. Sort the non-boundary elements in ascending order using any standard sorting technique
and rearrange them in the matrix.
2. Calculate the sum of both the diagonals.
3. Display the original matrix, rearranged matrix and only the diagonal elements of the
rearranged matrix with their sum.
Test your program with the sample data and some random data:
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
Example 2
INPUT: M = 5
7 4 1 9 5
8 2 6 10 19
13 1 3 4 1
10 0 5 12 16
1 8 17 6 8
OUTPUT:
ORIGINAL MATRIX
7 4 1 9 5
8 2 6 10 19
13 1 3 4 1
10 0 5 12 16
1 8 17 6 8
REARRANGED MATRIX
7 4 1 9 5
8 0 1 2 19
13 3 4 5 1
10 6 10 12 16
1 8 17 6 8
DIAGONAL ELEMENTS
7 5
0 2
4
6 12
1 8
INPUT: M = 3
OUTPUT: THE MATRIX IS OUT OF RANGE
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
142 Chapter 17. ISC-2016
Solution
1 import java . util .*;
2
3 class Matrix {
4 private int M , A [][];
5
6 public Matrix ( int M ) {
7 this . M = M ;
8 A = new int [ M ][ M ];
9 }
10
11 public void input () {
12 Scanner in = new Scanner ( System . in ) ;
13 for ( int row = 0; row < M ; row ++) {
14 for ( int col = 0; col < M ; col ++) {
15 A [ row ][ col ] = in . nextInt () ;
16 }
17 }
18 in . close () ;
19 }
20
21 public void display () {
22 for ( int row = 0; row < M ; row ++) {
23 System . out . print ( " \ t " ) ;
24 for ( int col = 0; col < M ; col ++) {
25 System . out . print ( A [ row ][ col ]) ;
26 if ( col < M - 1)
27 System . out . print ( " \ t " ) ;
28 }
29 System . out . println () ;
30 }
31 }
32
33 public void displayDiagonals () {
34 for ( int row = 0; row < M ; row ++) {
35 System . out . print ( " \ t " ) ;
36 for ( int col = 0; col < M ; col ++) {
37 if ( row == col || row + col == M - 1) {
38 System . out . print ( A [ row ][ col ]) ;
39 }
40 if ( col < M - 1)
41 System . out . print ( " \ t " ) ;
42 }
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
144 Chapter 17. ISC-2016
ISC-2016, Question 3
Manipulate words begining and ending with a vowel
Write a program to accept a sentence which may be terminated by either ’.’, ’?’ or ’!’ only.
The words may be separated by more than one blank space and are in UPPER CASE.
Test your program with the sample data and some random data:
Example 1
INPUT: ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.
OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL
Example 2
INPUT: YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE TODAY.
OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 2
A ARE YOU MUST AIM TO BE BETTER PERSON TOMMORROW THAN YOU TODAY
Example 3
OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 0
LOOK BEFORE YOU LEAP
Example 4
INPUT: HOW ARE YOU@
OUTPUT: INVALID INPUT
Solution
1 import java . util .*;
2
3 class Sentence {
4 private String sentence , answer ;
5 private static String vowels = " AEIOUaeiou " ;
6
7 public Sentence ( String sentence ) {
8 this . sentence = sentence ;
9 answer = " " ;
10 }
11
12 private static boolean isVowel ( char ch ) {
13 return vowels . indexOf ( ch ) >= 0;
14 }
15
16 public int countWordsLeadingAndTrailingVowels () {
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
146 Chapter 17. ISC-2016
17 int count = 0;
18 String sentenceCopy = sentence . substring (0 , sentence
. length () - 1) ;
19 String wordList [] = sentenceCopy . split ( " ␣ " ) ;
20 String beginingPart = " " , endingPart = " " ;
21 for ( String word : wordList ) {
22 if ( word . length () == 0)
23 continue ;
24 if ( isVowel ( word . charAt (0) ) && isVowel ( word .
charAt ( word . length () - 1) ) ) {
25 beginingPart += word + " ␣ " ;
26 count ++;
27 } else {
28 endingPart += word + " ␣ " ;
29 }
30 }
31 answer = beginingPart + endingPart ;
32 return count ;
33 }
34
35 public boolean isValid () {
36 String terminators = " .?! " ;
37 return terminators . indexOf ( sentence . charAt ( sentence .
length () - 1) ) >= 0;
38 }
39
40 public String toString () {
41 return answer . trim () ;
42 }
43 }
44
45 class ISC2016Q3 {
46 public static void main ( String [] args ) {
47 Scanner in = new Scanner ( System . in ) ;
48 System . out . print ( " INPUT :\ t " ) ;
49 String source = in . nextLine () ;
50 Sentence sentence = new Sentence ( source ) ;
51 if ( sentence . isValid () ) {
52 int count = sentence .
countWordsLeadingAndTrailingVowels () ;
53 System . out . println ( " OUTPUT :\ tNUMBER ␣ OF ␣ WORDS ␣
BEGINNING ␣ AND ␣ ENDING ␣ WITH ␣ A ␣ VOWEL ␣ = ␣ " + count
);
54 System . out . println ( " \ t " + sentence ) ;
55 } else {
56 System . out . println ( " OUTPUT :\ tINVALID ␣ INPUT " ) ;
57 }
58 in . close () ;
59 }
60 }
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
18. ISC-2017
ISC-2017, Question 1
Cartoon capacity break-up
A company manufactures packing cartons in four sizes, i.e. cartons to accommodate 6 boxes,
12 boxes, 24 boxes and 48 boxes. Design a program to accept the number of boxes to be
packed (N) by the user (maximum up to 1000 boxes) and display the break-up of the cartons
used in descending order of capacity (i.e. preference should be given to the highest capacity
available, and if boxes left are less than 6, an extra carton of capacity 6 should be used.)
Test your program with the sample data and some random data:
Example 1
INPUT : N = 726
OUTPUT :
48 x 15 = 720
6 x 1 = 6
Remaining boxes = 0
Total number of boxes = 726
Total number of cartons = 16
Example 2
150 Chapter 18. ISC-2017
INPUT: N = 140
OUTPUT:
48 X 2 = 96
24 x 1 = 24
12 x 1 = 12
6 x 1 = 6
Remaining boxes 2 x 1 = 2
Total number of boxes = 140
Total number of cartons = 6
Example 3
INPUT : N = 4296
Solution
1 import java . util .*;
2
3 class ISC2017Q1 {
4 public static void main ( String [] args ) {
5 Scanner in = new Scanner ( System . in ) ;
6 System . out . print ( " INPUT ␣ : ␣ N ␣ = ␣ " ) ;
7 int N = in . nextInt () ;
8 if ( N < 1 || N > 1000) {
9 System . out . println ( " OUTPUT : ␣ INVALID ␣ INPUT " ) ;
10 } else {
11 System . out . println ( " OUTPUT : " ) ;
12 int cartons [] = { 48 , 24 , 12 , 6 } , count = 0 ,
totalCartons = 0 , quantity = N ;
13 for ( int i = 0; i < cartons . length ; i ++) {
14 count = quantity / cartons [ i ];
15 if ( count > 0) {
16 System . out . printf ( " \ t \ t \ t \ t %4 d ␣ x ␣ %4 d ␣ = ␣
%4 d \ n " , cartons [ i ] , count , ( cartons [ i
] * count ) ) ;
17 }
18 totalCartons += count ;
19 quantity = quantity % cartons [ i ];
20 }
21 if ( quantity == 0) {
ISC-2017, Question 2
Quiz score evaluation
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
152 Chapter 18. ISC-2017
Note: Array entries are line fed (i.e. one entry per line)
Test your program for the following data and some random data:
Example 1
INPUT: N = 5
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.
Solution
1 import java . util .*;
2
3 class Quiz {
4 private int N ;
5 private char response [][] , answer [];
6 private int score [] , highestScore ;
7
8 public Quiz ( int N ) {
9 this . N = N ;
10 response = new char [ N ][5];
11 answer = new char [5];
12 score = new int [ N ];
13 }
14
15 public void input () {
16 Scanner in = new Scanner ( System . in ) ;
17 String input ;
18
19 for ( int participant = 0; participant < N ;
participant ++) {
20 System . out . printf ( " % -10 s ␣ %15 s ␣ " , " " , "
Participant ␣ " + ( participant + 1) + " ␣ " ) ;
21 input = in . nextLine () ;
22 for ( int question = 0; question < 5; question ++)
{
23 response [ participant ][ question ] = input .
charAt (2 * question ) ;
24 }
25 }
26 System . out . printf ( " % -10 s ␣ %15 s ␣ " , " " , " Key : ␣ " ) ;
27 input = in . nextLine () ;
28 for ( int question = 0; question < 5; question ++) {
29 answer [ question ] = input . charAt (2 * question ) ;
30 }
31 in . close () ;
32 }
33
34 public void display () {
35 for ( int row = 0; row < N ; row ++) {
36 System . out . println ( Arrays . toString ( response [ row
]) ) ;
37 }
38
39 System . out . println ( Arrays . toString ( answer ) ) ;
40
41 }
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
154 Chapter 18. ISC-2017
42
43 public void prepareResult () {
44 int max = 0 , question , participant ;
45 for ( question = 0; question < 5; question ++) {
46 for ( participant = 0; participant < N ;
participant ++) {
47 if ( response [ participant ][ question ] ==
answer [ question ]) {
48 score [ participant ]++;
49 }
50 if ( score [ participant ] > max ) {
51 max = score [ participant ];
52 }
53 }
54
55 }
56 highestScore = max ;
57 }
58
59 public void displayResult () {
60 System . out . printf ( " % -10 s ␣ % -15 s ␣ \ n " , " OUTPUT : " , "
Scores " ) ;
61 for ( int participant = 0; participant < N ;
participant ++) {
62 System . out . printf ( " % -10 s ␣ %15 s ␣ \ n " , " " , "
Participant ␣ " + ( participant + 1) + " ␣ = ␣ " +
score [ participant ]) ;
63 }
64 String msg = " Highest ␣ score : ␣ " ;
65 for ( int participant = 0; participant < N ;
participant ++) {
66 if ( highestScore == score [ participant ]) {
67 System . out . printf ( " % -10 s ␣ %15 s ␣ % -20 s \ n " , " " ,
msg , " Participant ␣ " + ( participant + 1) ) ;
68 msg = " " ;
69 }
70 }
71 }
72 }
73
74 public class ISC2017Q2 {
75 public static void main ( String [] args ) {
76 Scanner in = new Scanner ( System . in ) ;
77 System . out . printf ( " % -10 s ␣ %15 s ␣ " , " INPUT : " , " N ␣ = ␣ " ) ;
78 int N = in . nextInt () ;
79 Quiz quiz = new Quiz ( N ) ;
80 quiz . input () ;
81 // quiz . display () ;
82 quiz . prepareResult () ;
83 quiz . displayResult () ;
84 in . close () ;
85 }
86 }
ISC-2017, Question 3
ROT13 encryption
Example 2
INPUT: Encryption helps to secure data.
OUTPUT: The cipher text is:
Rapelcgvba Urycf gb frpher gngn.
Example 3
INPUT: You
OUTPUT: INVALID LENGTH
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
156 Chapter 18. ISC-2017
Solution
1 import java . util .*;
2
3 class CaesarCipher {
4 private String L , msg ;
5
6 public CaesarCipher ( String L ) {
7 this . L = L ;
8 }
9
10 public void encode () {
11 msg = " " ;
12 for ( int position = 0; position < L . length () ;
position ++) {
13 msg += rot13 ( L . charAt ( position ) ) ;
14 }
15 }
16
17 private static char rot13 ( char ch ) {
18 if ( Character . isLetter ( ch ) ) {
19 if ( ch >= 'A ' && ch <= 'M ') {
20 ch = ( char ) ( ch + 13) ;
21 } else if ( ch >= 'N ' && ch <= 'Z ') {
22 ch = ( char ) ( ch - 13) ;
23 } else if ( ch >= 'a ' && ch <= 'm ') {
24 ch = ( char ) ( ch + 13) ;
25 } else if ( ch >= 'n ' && ch <= 'z ') {
26 ch = ( char ) ( ch - 13) ;
27 }
28 }
29 return ch ;
30 }
31
32 public String toString () {
33 return " \ t " + msg ;
34 }
35 }
36
37 public class ISC2017Q3 {
38 public static void main ( String [] args ) {
39 Scanner in = new Scanner ( System . in ) ;
40 System . out . print ( " INPUT :\ t " ) ;
41 String L = in . nextLine () ;
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
19. ISC-2018
ISC-2018, Question 1
Goldbach numbers
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 and 7, 5
and 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
160 Chapter 19. ISC-2018
Example 2:
INPUT N=30
OUTPUT:
PRIME PAIRS 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 OUT OF RANGE.
Solution
1 import java . util .*;
2
3 class ISC2018Q1 {
4 private static int primes [] = { 3 , 5 , 7 , 11 , 13 , 17 , 19 ,
23 , 29 , 31 , 37 , 41 , 43 , 47 };
5
6 public static void main ( String [] args ) {
7 Scanner in = new Scanner ( System . in ) ;
8 System . out . print ( " INPUT : ␣ N ␣ = ␣ " ) ;
9 int N = in . nextInt () ;
10 System . out . println ( " OUTPUT : ␣ " ) ;
11
12 if ( N > 9 && N < 50) {
13 if ( N % 2 == 0) {
14 System . out . println ( " PRIME ␣ PAIRS ␣ ARE : " ) ;
15 int half = N / 2;
16 for ( int i = 0; primes [ i ] <= half ; i ++) {
17 if ( isPrime ( N - primes [ i ]) ) {
18 System . out . println ( primes [ i ] + " ,␣ "
+ ( N - primes [ i ]) ) ;
19 }
20 }
21 } else {
22 System . out . println ( " INVALID ␣ INPUT . ␣ NUMBER ␣ IS
␣ ODD " ) ;
23 }
24 } else {
25 System . out . println ( " INVALID ␣ INPUT . ␣ NUMBER ␣ OUT ␣ OF
␣ RANGE . " ) ;
26
27 }
28 in . close () ;
29 }
30
31 public static boolean isPrime ( int number ) {
32
33 // for ( int i =0; i < primes . length ; i ++) {
34 // if ( number == primes [ i ]) return true ;
35 // }
36 for ( int prime : primes ) {
37 if ( number == prime )
38 return true ;
39 }
40
41 return false ;
42 }
43 }
ISC-2018, Question 2
Sort rows of a 2D array
Write a program to declare a matrix A[][] of order (MxN) where ’M’ is the number of rows
and ’N’ is the number of coloumns such that the values of both ’M’ and ’N’ must be greater
than 2 and less than 10. Allow the user to input integers into this matrix. Perform the following
tasks on the matrix:
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
162 Chapter 19. ISC-2018
Example 1:
INPUT: M=3
N=3
ENTER ELEMENTS OF MATRIX
11 -2 3
5 16 7
9 0 4
3 1 8
OUTPUT:
ORIGNAL MATRIX
11 -2 3
5 16 7
9 0 4
3 1 8
Example 2:
INPUT: M=3
N=3
ENTER ELEMENTS OF MATRIX
22 5 19
7 36 12
9 13 6
OUTPUT:
ORIGINAL MATRIX
22 5 19
7 36 12
9 13 6
Example 3:
INPUT: M=11
N=5
OUTPUT:
MATRIX OUT OF RANGE
Solution
1 import java . util .*;
2
3 class ISC2018Q2 {
4
5 public static void main ( String [] args ) {
6 Scanner in = new Scanner ( System . in ) ;
7 System . out . println ( " INPUT : ␣ M ␣ = ␣ " ) ;
8 int M = in . nextInt () ;
9 System . out . println ( " ␣ ␣ ␣ ␣ ␣ ␣ ␣ N ␣ = ␣ " ) ;
10 int N = in . nextInt () ;
11 if ( M > 2 && M < 10 && N > 2 && N < 10) {
12 int A [][] = new int [ M ][ N ];
13 input ( A ) ;
14 display (A , " ORIGINAL ␣ MATRIX " ) ;
15 for ( int row = 0; row < M ; row ++) {
16 sort ( A [ row ]) ;
17 }
18 display (A , " MATRIX ␣ AFTER ␣ SORTING ␣ ROWS " ) ;
19
20 } else {
21 System . out . println ( " OUTPUT :\ nMATRIX ␣ OUT ␣ OF ␣ RANGE
");
22 }
23 in . close () ;
24 }
25
26 public static void input ( int A [][]) {
27 System . out . println ( " ENTER ␣ ELEMENTS ␣ OF ␣ MATRIX " ) ;
28 Scanner in = new Scanner ( System . in ) ;
29 for ( int row = 0; row < A . length ; row ++) {
30 for ( int col = 0; col < A [ row ]. length ; col ++) {
31 A [ row ][ col ] = in . nextInt () ;
32 }
33 }
34 in . close () ;
35 }
36
37 public static void display ( int A [][] , String header ) {
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
164 Chapter 19. ISC-2018
82
83 }
84 }
ISC-2018, Question 3
Vertical banner
Example 1:
INPUT: N=3
Team 1: Emus
Team 2: Road Rols
Team 3: Coyote
OUTPUT:
E R C
m o o
u a y
s d o
t
R e
o
l
s
Example 2:
INPUT: N=4
Team 1: Royal
Team 2: Mars
Team 3: De Rose
Team 4: Kings
OUTPUT:
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
166 Chapter 19. ISC-2018
R M D K
o a e i
y r n
a s R g
l o s
s
e
Example 3:
INPUT: N=10
OUTPUT:
INVALID INPUT
Solution
1 import java . util .*;
2
3 public class ISC2018Q3 {
4 public static void main ( String [] args ) {
5 Scanner in = new Scanner ( System . in ) ;
6 System . out . print ( " INPUT : ␣ N ␣ = ␣ " ) ;
7 int N = in . nextInt () ;
8 in . nextLine () ;
9 if ( N > 2 && N < 9) {
10 int max = 0;
11 String team [] = new String [ N ];
12 for ( int i = 0; i < N ; i ++) {
13 System . out . print ( " Team ␣ " + ( i + 1) + " : ␣ " ) ;
14 team [ i ] = in . nextLine () ;
15 if ( team [ i ]. length () > max )
16 max = team [ i ]. length () ;
17 }
18 System . out . println ( " OUTPUT " ) ;
19 for ( int row = 0; row < max ; row ++) {
20 for ( int col = 0; col < N ; col ++) {
21 if ( row < team [ col ]. length () ) {
22 System . out . print ( team [ col ]. charAt (
row ) + " \ t " ) ;
23 } else {
24 System . out . print ( " \ t " ) ;
25 }
26 }
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
20. ISC-2019
ISC-2019, Question 1
Convert day number into date
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: 12 TH SEPTEMBER, 2018
DATE AFTER 22 DAYS: 4 TH OCTOBER, 2018
170 Chapter 20. ISC-2019
Example 2
INPUT:
DAY NUMBER: 360
YEAR: 2018
DATE AFTER (N DAYS): 45
OUTPUT:
DATE: 26 TH DECEMBER, 2018
DATE AFTER 45 DAYS: 9 TH 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.
Solution
1 import java . util .*;
2
3 class Date {
4 private int day , month , year ;
5
6 public Date ( int day , int month , int year ) {
7 this . day = day ;
8 this . month = month ;
9 this . year = year ;
10 }
11
12 public String toString () {
13 String suffix [] = { " TH " , " ST " , " ND " , " RD " , " TH " , "
TH " , " TH " , " TH " , " TH " , " TH " };
14 String monthName [] = { " JANUARY " , " FEBRUARY " , " MARCH
" , " APRIL " , " MAY " , " JUNE " , " JULY " , " AUGUST " , "
SEPTEMBER " ,
15 " OCTOBER " , " NOVEMBER " , " DECEMBER " };
16 String finalSuffix ;
17 if ( day > 9 && day < 21)
18 finalSuffix = " TH " ;
19 else
20 finalSuffix = suffix [ day % 10];
21 return day + " ␣ " + finalSuffix + " ␣ " + monthName [
month - 1] + " ,␣ " + year ;
22 }
23 }
24
25 class ISC2019Q1 {
26 static int dayNumber , year , N ;
27
28 public static void main ( String [] args ) {
29 Scanner in = new Scanner ( System . in ) ;
30 System . out . println ( " INPUT " ) ;
31 System . out . print ( " DAY ␣ NUMBER :\ t \ t " ) ;
32 dayNumber = in . nextInt () ;
33 System . out . print ( " YEAR :\ t \ t \ t " ) ;
34 year = in . nextInt () ;
35 System . out . print ( " DATE ␣ AFTER ␣ ( N ␣ DAYS ) :\ t " ) ;
36 N = in . nextInt () ;
37
38 System . out . println ( " OUTPUT " ) ;
39 if ( dayNumber < 1 || dayNumber > 366) {
40 System . out . println ( " DAY ␣ NUMBER ␣ OUT ␣ OF ␣ RANGE . " ) ;
41 } else if ( year < 1000 || year > 9999) {
42 System . out . println ( " YEAR ␣ OUT ␣ OF ␣ RANGE . " ) ;
43 } else if ( N < 1 || N > 100) {
44 System . out . println ( " DATE ␣ AFTER ␣ ( N ␣ DAYS ) ␣ OUT ␣ OF ␣
RANGE . " ) ;
45 } else {
46 Date dt = dateFromDayNumber ( dayNumber , year ) ;
47 System . out . println ( " DATE :\ t \ t \ t " + dt ) ;
48 int nextDayNumber = dayNumber + N , nextYear ;
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
172 Chapter 20. ISC-2019
ISC-2019, Question 2
Sort and transfer 1D array into a 2D array
Write a program to declare a single dimensional array a[] and a square matrix b[ ] [ ] of size N,
where N > 2 and N < 10.
Allow the user to input positive integers into the single dimensional array.
Perform the following tasks on the matrix:
1. Sort the elements of the single dimensional array in ascending order using any standard
sorting technique and display the sorted elements.
2. Fill the square matrixb[][]in the following format.
If the array a[ ] = { 5, 2, 8, 1 }
Then, after sorting a[ ]= {1, 2, 5, 8}
Then, the matrix b[][] would fill as below:
1 2 5 8
1 2 5 1
1 2 1 2
1 1 2 5
3. Display the filled matrix in the above format.
Test your program for the following data and some random data:
Example 1
INPUT:
N = 3
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7
OUTPUT:
SORTED ARRAY: 1 3 7
FILLED MATRIX
1 3 7
1 3 1
1 1 3
Example 2
INPUT:
N = 13
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
174 Chapter 20. ISC-2019
OUTPUT:
MATRIX SIZE OUT OF RANGE
Example 3
INPUT:
N = 5
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 10 2 5 23 6
OUTPUT:
SORTED ARRAY: 2 5 6 10 23
FILLED MATRIX
2 5 6 10 23
2 5 6 10 2
2 5 6 2 5
2 5 2 5 6
2 2 5 6 10
Solution
1 import java . util .*;
2
3 class ISC2019Q2 {
4 public static void main ( String [] args ) {
5 Scanner in = new Scanner ( System . in ) ;
6 System . out . println ( " INPUT " ) ;
7 System . out . print ( " N ␣ = ␣ " ) ;
8 int N = in . nextInt () ;
9 if ( N > 2 && N < 10) {
10 int a [] = new int [ N ];
11 System . out . print ( " ENTER ␣ ELEMENTS ␣ OF ␣ SINGLE ␣
DIMENSIONAL ␣ ARRAY : ␣ " ) ;
12 for ( int i = 0; i < N ; i ++) {
13 a [ i ] = in . nextInt () ;
14 }
15 bubbleSort ( a ) ;
16 System . out . println ( " OUTPUT " ) ;
17 System . out . print ( " SORTED ␣ ARRAY : ␣ \ nFILLED ␣ MATRIX "
);
18 display ( a ) ;
19 int b [][] = createAndFillMatrix ( a ) ;
20 display ( b ) ;
21 } else {
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
176 Chapter 20. ISC-2019
ISC-2019, Question 3
Make palindromic words
Write a program to accept a sentence which may be terminated by either ‘.’ , ‘?’ or ‘!’ only.
The words are to be separated by a single blank space and are in UPPER CASE.
Perform the following tasks:
1. Check for the validity of the accepted sentence.
2. Convert the non-palindrome words of the sentence into palindrome words by concate-
nating the word by its reverse (excluding the last character).
Example: The reverse of the word HELP would be LEH (omitting the last alphabet) and
by concatenating both, the new palindrome word is HELPLEH. Thus, the word HELP
becomes HELPLEH.
Note: The words which end with repeated alphabets, for example ABB would become
ABBA and not ABBBA and XAZZZ becomes XAZZZAX. [Palindrome word: Spells
same from either side. Example: DAD, MADAM etc.]
3. Display the original sentence along with the converted sentence.
Test your program for the following data and some random data:
Example 1
INPUT:
THE BIRD IS FLYING.
OUTPUT:
THE BIRD IS FLYING.
THEHT BIRDRIB ISI FLYINGNIYLF
Example 2
INPUT:
IS THE WATER LEVEL RISING?
OUTPUT:
IS THE WATER LEVEL RISING?
Example 3
INPUT:
THIS MOBILE APP LOOKS FINE.
OUTPUT:
THIS MOBILE APP LOOKS FINE.
THISIHT MOBILELIBOM APPA LOOKSKOOL FINENIF
Example 4
INPUT:
YOU MUST BE CRAZY#
OUTPUT:
INVALID INPUT
Solution
1 import java . util .*;
2
3 public class ISC2019Q3 {
4 public static void main ( String [] args ) {
5 Scanner in = new Scanner ( System . in ) ;
6 System . out . println ( " INPUT " ) ;
7 String sentence = in . nextLine () ;
8 MyString str = new MyString ( sentence ) ;
9 System . out . println ( " OUPUT " ) ;
10 if ( str . isValid () ) {
11 System . out . println ( sentence ) ;
12 str . convertToPalindrome () ;
13 System . out . println ( str ) ;
14 } else {
15 System . out . println ( " INVALID ␣ INPUT " ) ;
16 }
17 in . close () ;
18 }
19 }
20
21 class MyString {
22 private String sentence , words [];
23
24 public MyString ( String sentence ) {
25 this . sentence = sentence ;
26 }
27
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
178 Chapter 20. ISC-2019
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
21. ISC-2020
ISC-2020, Question 1
Prime Adam integers
A Prime-Adam integer is a positive integer (without leading zeros) which is prime as well as
an Adam number.
Prime number:
A number which has only two factors, i.e. 1 and the number itself.
Example: 2, 3, 5, 7 ... etc.
Adam number: The square of a number and the square of it’s reverse are reverse to each other.
Example: If n=13 and reverse of ’n’=31, then,
(13)2 = 169
(31)2 = 961 which is reverse of 169
thus 13, is an Adam number.
Accept two positive integers m and n, where m is less than n as user input. Display all
Prime-Adam integers that are in the range between m and n (both inclusive) and output them
along with the frequency, in the format given below:
Test your program with following data and some random data:
182 Chapter 21. ISC-2020
Example 1
INPUT
m = 5
n = 100
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
11 13 31
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 2
INPUT:
m = 100
n = 200
OUTPUT:
THE PRIME-ADAM INTEGERS IS:
101 103 113
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 3
INPUT:
m = 50
n = 70
OUTPUT:
THE PRIME-ADAM INTEGERS IS:
NIL
FREQUENCY OF PRIME-ADAM INTEGERS IS: 0
Example 4
INPUT:
m = 700
n = 450
OUTPUT:
INVALID INPUT
Solution
1 import java . util .*;
2
3 class ISC2020Q1 {
4 // public static boolean isPrimeNumber ( int n ) {
5 // int count =0;
6 // for ( int divisor =1; divisor <= n ; divisor ++) {
7 // if ( n % divisor ==0) count ++;
8 // }
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
184 Chapter 21. ISC-2020
ISC-2020, Question 2
Rows of 2D array as octal representation
Write a program to declare a Matrix A[][] of order (M x N) where ’M’ is the number of rows
and ’N’ is the number of columns such that the value of ’M’ must be greater than 0 and less
than 10 and the value of ’N’ must be greater than 2 and less than 6.
Allow the user to input digits (0-7) only at each location, such that each row represents an
octal number.
Example:
2 3 1 (decimal equivalent of 1st row = 153 i.e. 2*8^2 + 3X8^1 + 1*8^0)
4 0 5 (decimal equivalent of 1st row = 261 i.e. 4*8^2 + 0X8^1 + 5*8^0)
Example 1
INPUT
M = 1
N = 3
ENTER ELEMENTS FOR ROW 1: 1 4 4
OUTPUT
FILLED MATRIX DECIMAL EQUIVALENT
1 4 4 100
Example 2
INPUT
M = 3
N = 4
ENTER ELEMENTS FOR ROW 1: 1 1 3 7
ENTER ELEMENTS FOR ROW 2: 2 1 0 6
ENTER ELEMENTS FOR ROW 3: 0 2 4 5
OUTPUT
FILLED MATRIX DECIMAL EQUIVALENT
1 1 3 7 607
2 1 0 6 1094
0 2 4 5 165
Example 3
INPUT
M = 3
N = 3
ENTER ELEMENTS FOR ROW 1: 2 4 8
OUTPUT
INVALID INPUT
Example 4
INPUT
M = 4
N = 6
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
186 Chapter 21. ISC-2020
OUTPUT
OUT OF RANGE
Solution
1 import java . util .*;
2
3 class Matrix {
4 private int M , N , A [][];
5
6 public Matrix ( int M , int N ) {
7 this . M = M ;
8 this . N = N ;
9 A = new int [ M ][ N ];
10 }
11
12 public void input () {
13 Scanner in = new Scanner ( System . in ) ;
14 for ( int row = 0; row < M ; row ++) {
15 System . out . print ( " ENTER ␣ ELEMENTS ␣ FOR ␣ ROW ␣ " + (
row + 1) + " : ␣ " ) ;
16 for ( int col = 0; col < N ; col ++) {
17 A [ row ][ col ] = in . nextInt () ;
18 if (!( A [ row ][ col ] >= 0 && A [ row ][ col ] <= 7) )
{
19 System . out . println ( " OUTPUT \ nINVALID ␣
INPUT " ) ;
20 System . exit (1) ;
21 }
22 }
23 }
24 in . close () ;
25 }
26
27 public void display () {
28 int decimal ;
29 System . out . println ( " \ nFILLED ␣ MATRIX \ tDECIMAL ␣
EQUIVALENT " ) ;
30 for ( int row = 0; row < M ; row ++) {
31 decimal = 0;
32 for ( int col = 0; col < N ; col ++) {
33 System . out . print ( A [ row ][ col ] + " ␣ " ) ;
34 decimal += A [ row ][ col ] * Math . pow (8 , N - col
- 1) ;
35 }
36 System . out . print ( " \ b \ t \ t " + decimal + " \ n " ) ;
37 }
38 }
39 }
40
41 public class ISC2020Q2 {
42 public static void main ( String [] args ) {
43 Scanner in = new Scanner ( System . in ) ;
44 int M , N ;
45 System . out . println ( " INPUT " ) ;
46 System . out . print ( " M ␣ = ␣ " ) ;
47 M = in . nextInt () ;
48
49 System . out . print ( " N ␣ = ␣ " ) ;
50 N = in . nextInt () ;
51
52 if ( M > 0 && M < 10 && N > 2 && N < 6) {
53 Matrix A = new Matrix (M , N ) ;
54 A . input () ;
55 System . out . println ( " OUTPUT " ) ;
56 A . display () ;
57 } else {
58 System . out . println ( " OUTPUT \ nOUT ␣ OF ␣ RANGE " ) ;
59 }
60 in . close () ;
61 }
62 }
ISC-2020, Question 3
Sort words in ascending order of lengths
Write a program to accept a sentence which may be terminated by either ’.’,’,’,’?’ or ’!’ only.
The words are to be seperated by a single blank space and are in UPPER CASE.
Perform the following tasks:
1. Check for validity of the accepted sentence only for the terminating character.
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
188 Chapter 21. ISC-2020
2. Arrange the words in ascending order of their length. If two or more words have the
same length, then sort them alphabetcally.
3. Display the original sentence along with the converted sentence.
Test your program for the following data and some random data:
Example 1
INPUT
AS YOU SOW SO SHALL YOU REAP.
OUTPUT
AS YOU SOW SO SHALL YOU REAP.
AS SO SOW YOU YOU REAP SHALL
Example 2
INPUT
SELF HELP IS THE BEST HELP.
OUTPUT
SELF HELP IS THE BEST HELP.
IS THE BEST HELP HELP SELF
Example 3
INPUT
BE KIND TO OTHERS.
OUTPUT
BE KIND TO OTHERS.
BE TO KIND OTHERS
Example 4
INPUT
NOTHING IS IMPOSSIBLE#
OUTPUT
INVALID INPUT
Solution
1 import java . util .*;
2
3 class MyString {
4 private String sentence , words [];
5
6 public MyString ( String sentence ) {
7 this . sentence = sentence ;
8 }
9
10 public boolean isValid () {
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
190 Chapter 21. ISC-2020
50 String sentence ;
51 System . out . println ( " INPUT " ) ;
52 sentence = in . nextLine () ;
53 System . out . println ( " OUTPUT " ) ;
54 MyString str = new MyString ( sentence ) ;
55 if ( str . isValid () ) {
56 System . out . println ( sentence ) ;
57 str . arrange () ;
58 System . out . println ( str ) ;
59 } else {
60 System . out . println ( " INVALID ␣ INPUT " ) ;
61 }
62 in . close () ;
63 }
64 }
ISC-2021, Question 1
Fascinating Number In A Range
A fascinating number is one which when multiplied by 2 and 3 and then, after the results are
concatenated with the original number, the new number contains all the digits from 1 to 9
exactly once.
There can be any number of zeros and are to be ignored.
Example:
273
273 * 1 = 273
273 * 2 = 546
273 * 3 = 819
Concatenating the results, we get 273546819 which contains all digits from 1 to 9 exactly
once. Thus 273 is a fascinating number.
Accept two positive intgers m and n, where m must be less than n and the values of both ’m’
and ’n’ must be greater than 99 and less than 10000 as user input.
Display all fascinating numbers that are in the range between m and n (both inclusive) and
output them along with the frequency, in the format given below:
Test your program for the following data and some random data.
Example 1:
INPUT: m = 100
192 Chapter 22. ISC-2021
n = 500
OUTPUT: THE FASCINATING NUMBERS ARE:
192 219 273 327
THE FREQUENCY OF FASCINATING NUMBER IS: 4
Example 2:
INPUT: m = 900
n = 5000
OUTPUT: THE FASCINATING NUMBERS ARE:
1902 1920 2019 2190 2703 2730 3027 3270
THE FREQUENCY OF FASCINATING NUMBER IS: 8
Example 3:
INPUT: m = 400
n = 900
OUTPUT: THE FASCINATING NUMBERS ARE:
NIL
THE FREQUENCY OF FASCINATING NUMBER IS: 0
Example 4:
INPUT: m = 70
n = 450
OUTPUT: Invalid range
Solution
1 import java . util .*;
2
3 class Fascinating {
4 private int m , n , frequency ;
5 private static int digits [];
6 private String output ;
7
8 public Fascinating ( int m , int n ) {
9 this . m = m ;
10 this . n = n ;
11 frequency = 0;
12 process () ;
13 }
14
15 private void process () {
16 output = " " ;
17 for ( int i = m ; i <= n ; i ++) {
18 if ( isFascinating ( i ) ) {
19 frequency ++;
20 output += i + " ␣ " ;
21 }
22 }
23 output = output . trim () ;
24 }
25
26 private static boolean isFascinating ( int num ) {
27 digits = new int [9];
28 boolean status = inspectDigits ( num ) && inspectDigits
( num * 2) && inspectDigits ( num * 3) ;
29 if ( status == false )
30 return false ;
31 for ( int count : digits ) {
32 if ( count != 1)
33 return false ;
34 }
35 return true ;
36 }
37
38 private static boolean inspectDigits ( int product ) {
39 int digit ;
40 for ( int temp = product ; temp > 0; temp /= 10) {
41 digit = temp % 10;
42 if ( digit > 0) {
43 if ( digits [ digit - 1] == 1)
44 return false ;
45 digits [ digit - 1] = 1;
46 }
47 }
48 return true ;
49 }
50
51 public int getFrequency () {
52 return frequency ;
53 }
54
55 public String toString () {
56 String header = " THE ␣ FASCINATING ␣ NUMBERS ␣ ARE :\ n " ;
57 if ( output == " " )
58 return header + " NIL " ;
59 else
60 return header + output ;
61 }
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
194 Chapter 22. ISC-2021
62 }
63
64 class ISC2021Q1 {
65 public static void main ( String [] args ) {
66 Scanner in = new Scanner ( System . in ) ;
67 System . out . print ( " INPUT :\ tm ␣ = ␣ " ) ;
68 int m = in . nextInt () ;
69 System . out . print ( " \ tn ␣ = ␣ " ) ;
70 int n = in . nextInt () ;
71 if ( m > n || m <= 99 || n <= 99 || m >= 10000 || n
>= 10000) {
72 System . out . println ( " OUTPUT :\ tInvalid ␣ Range " ) ;
73 } else {
74 Fascinating obj = new Fascinating (m , n ) ;
75 System . out . println ( " OUPUT :\ t " + obj ) ;
76 System . out . println ( " THE ␣ FREQUENCY ␣ OF ␣ FASCINATING
␣ NUMBER ␣ IS : ␣ " + obj . getFrequency () ) ;
77 }
78 in . close () ;
79 }
80 }
ISC-2021, Question 2
Frequency of common words in two sentences
Write a program to accept a paragraph containing TWO sentences only. The sentences may be
terminated by either ‘.’, ‘?’ or ‘!’ only. Any other character may be ignored. The words are to
be separated by a single blank space and must be in UPPER CASE.
Perform the following tasks:
1. Check for the validity of the accepted paragraph for the number of sentences and for the
terminating character.
2. Separate the two sentences from the paragraph and find the common words in the two
sentences with their frequency of occurrence in the paragraph.
3. Display both the sentences separately along with the common words and their frequency,
in the format given below:
Example 1
INPUT: IS IT RAINING? YOU MAY GET WET IF IT IS RAINING.
OUTPUT: IS IT RAINING?
YOU MAY GET WET IF IT IS RAINING.
IT 2
RAINING 2
Example 2
INPUT: INDIA IS MY MOTHERLAND AND I AM PROUD OF MY
MOTHERLAND. ALL INDIANS ARE MY BROTHERS AND SISTERS.
Example 3
INPUT: ARE YOU COMING? I AM GETTING LATE.
OUTPUT: ARE YOU COMING?
I AM GETTING LATE.
NO COMMON WORDS
Example 4
INPUT: AT LAST, THE TIGER WAS SAVED.
OUTPUT: INVALID INPUT
Solution
1 import java . util .*;
2
3 class Paragraph {
4 private String paragraph , sentence [] , delimiters , output
;
5
6 public Paragraph ( String paragraph ) {
7 this . paragraph = paragraph ;
8 delimiters = " .?! " ;
9 }
10
11 public boolean isValid () {
12 char ch = '␣ ';
13 int wordCount = 0;
14 boolean terminatorAtEnd = false ;
15 for ( int position = 0; position < paragraph . length ()
; position ++) {
16 ch = paragraph . charAt ( position ) ;
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
196 Chapter 22. ISC-2021
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
198 Chapter 22. ISC-2021
97 para . displayFrequencyTable () ;
98 } else {
99 System . out . println ( " INVALID ␣ INPUT " ) ;
100 }
101 in . close () ;
102 }
103 }
ISC-2021, Question 3
Sum of border elements & sorting of a 2D array
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 the value of both ’M’ and ’N" must be greater
than 2 and less than 8. Allow the user to input the integers into the matrix.
Perform the following tasks on the matrix:
1. Sort the matrix in descending order using any standard technique.
2. Calculate the sum of the boundary elements of the matrix before sorting and after
sorting.
3. Display the original matrix and the sorted matrix along with the sum of their boundary
elements respectively.
Test your program for the following data and some random data:
Example 1
INPUT:
M = 3
N = 4
ENTER ELEMENTS OF matrix
11 2 -3 5
1 7 13 6
0 4 9 8
OUTPUT:
ORIGINAL matrix
11 2 -3 5
1 7 13 6
0 4 9 8
Example 2
INPUT:
M = 3
N = 3
ENTER ELEMENTS OF matrix
6 2 5
14 1 17
9 4 8
OUTPUT:
ORIGINAL matrix
6 2 5
14 1 17
9 4 8
Example 3
INPUT:
M = 2
N = 6
OUTPUT:
OUT OF Range
Example 4
INPUT:
M = 4
N = 9
OUTPUT:
OUT OF Range
Solution
1 import java . util .*;
2
3 class Matrix {
4 private int A [][] , M , N , minRow , minCol , maxRow , maxCol ,
max ;
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
200 Chapter 22. ISC-2021
5
6 public Matrix ( int M , int N ) {
7 A = new int [ M ][ N ];
8 this . M = M ;
9 this . N = N ;
10 }
11
12 public void getInput () {
13 Scanner in = new Scanner ( System . in ) ;
14 for ( int row = 0; row < M ; row ++) {
15 for ( int col = 0; col < N ; col ++) {
16 A [ row ][ col ] = in . nextInt () ;
17 }
18 }
19 in . close () ;
20 }
21
22 public void computeMaxiumPosition ( int startRow , int
startCol ) {
23 max = A [ startRow ][ startCol ];
24 maxRow = startRow ;
25 maxCol = startCol ;
26 for ( int row = startRow ; row < M ; row ++) {
27 // startRow =0;
28 for ( int col = startCol ; col < N ; col ++) {
29 startCol = 0;
30 if ( A [ row ][ col ] > max ) {
31 max = A [ row ][ col ];
32 maxRow = row ;
33 maxCol = col ;
34 }
35 }
36 }
37 }
38
39 public String toString () {
40 String output = " " ;
41 for ( int row = 0; row < M ; row ++) {
42 output += " \ t " ;
43 for ( int col = 0; col < N ; col ++) {
44 output += A [ row ][ col ];
45 if ( col < N - 1)
46 output += " \ t " ;
47 }
https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
202 Chapter 22. ISC-2021