0% found this document useful (0 votes)
355 views202 pages

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh

Uploaded by

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

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh

Uploaded by

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

Pragmatic Solutions To

ISC Computer Science


Practical Problems

Vinay Singh
Copyright © 2022 Vinay Singh

VINAY @ CODEWITHVINAY. COM

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 source code in this book is available at:


https://github.com/vinay-singh-in/book-isc-pract-solutions-download.

The software distributed with this book is “AS IS ” BASIS , WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND , either express or implied.

First printing, December 2022


3

Dedicated to all my student who encouraged me to compile


this book.

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

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


Contents

I Front Matter
1 Foreward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2 Acknowledgement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

3 Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

4 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

II Problems & Solutions


5 ISC-2004 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Smallest Base Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Decoding Secret Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Machine Usage Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

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

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


CONTENTS 7

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.

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


II
Problems & Solutions

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

Explanation Video: https://www.youtube.com/watch?v=oDaIEvNL0ow

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 ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


23

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

50 if ( Character . isDigit ( number . charAt ( i ) ) ) {


51 digit = ( number . charAt ( i ) - '0 ') ;
52 } else {
53 digit = ( number . charAt ( i ) - 55) ;
54 }
55 answer += digit * multiplier ;
56 }
57 return answer ;
58 }
59
60 private static int maximumDigit ( String strNumber ) {
61 int i , result = 0 , l = strNumber . length () ;
62 int digit ;
63 for ( i = 0; i <= l - 1; i ++) {
64 digit = digitValue ( strNumber . charAt ( i ) ) ;
65 if ( digit > result )
66 result = digit ;
67 }
68 return result ;
69 }
70
71 private static int digitValue ( char c ) {
72 int result = 0;
73 if ( Character . isDigit ( c ) )
74 result = c - '0 ';
75 else
76 result = c - 'A ' + 10;
77 return result ;
78 }
79 }
80
81 class ISC2004Q1 {
82 public static void main ( String [] args ) {
83 Scanner in = new Scanner ( System . in ) ;
84
85 System . out . println ( " INPUT : " ) ;
86 System . out . print ( " X ␣ = ␣ " ) ;
87 String X = in . nextLine () ;
88 System . out . print ( " Y ␣ = ␣ " ) ;
89 String Y = in . nextLine () ;
90 Base b = new Base (X , Y ) ;
91 System . out . println ( " OUTPUT :\ n " + b ) ;
92
93 in . close () ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


25

94 }
95 }

ISC-2004, Question 2
Decoding Secret Message

Explanation Video: https://www.youtube.com/watch?v=CH2haXQcu9w

The computer department of the Agency of International Espionage is trying to decode


Intercepted messages. The agency’s spies have determined that the enemy encodes Messages
by first converting all characters to their ASCII values and then reversing the string.
For example, consider A_z (the underscore is just to highlight the space). The ASCII values
of A, , z are 65, 32, 122 respectively. Concatenate them to get 6532122, then reverse this to
get 2212356 as the coded message.
Write a program which reads a coded message and decodes it. The coded message will not
exceed 200 characters. It will contain only alphabets (A...Z, and a...z) and spaces.
ASCII values of A...Z are 65...90 and those of a...z are 97...122.
Test your Program for the following data and some random data.
SAMPLE DATA:
INPUT:
Encoded Message:
2312179862310199501872379231018117927
OUTPUT:
THE DECODED MESSAGE: Have a Nice Day

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

7 this . encodedMessage = encodedMessage ;


8 decode () ;
9 }
10
11 private void decode () {
12 String temp = " " ;
13 decodedMessage = " " ;
14 int number ;
15 for ( int index = encodedMessage . length () - 1; index
>= 0; index - -) {
16 temp += encodedMessage . charAt ( index ) ;
17 number = Integer . parseInt ( temp ) ;
18 if ( number == '␣ ' || Character . isLetter ( number ) )
{
19 decodedMessage += ( char ) number ;
20 temp = " " ;
21 }
22 }
23 }
24
25 public String toString () {
26 return decodedMessage ;
27 }
28 }
29
30 public class ISC2004Q2 {
31 public static void main ( String [] args ) {
32 Scanner in = new Scanner ( System . in ) ;
33
34 System . out . println ( " INPUT :\ nEncoded ␣ Message : " ) ;
35 String encodedMessage = in . next () ;
36 Message msg = new Message ( encodedMessage ) ;
37 System . out . println ( " OUTPUT :\ nDecoded ␣ Message :\ t " +
msg ) ;
38
39 in . close () ;
40 }
41 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


27

ISC-2004, Question 3
Machine Usage Problem

Explanation Video: https://www.youtube.com/watch?v=uahoTSKwvig

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:

• User identification number.


• Login time and date.
• Logout time and date.
Time 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

142 16:20 20-12 16:30 20-12

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]) ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


29

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

66 + " \ t " + formatTime ( duration [ i ]) + " \ n " ;


67 }
68 output += " THE ␣ USER ␣ WHO ␣ LOGGED ␣ IN ␣ FOR ␣ THE ␣ LONGEST ␣
DURATION \ n " ;
69 output += id [ maxIndex ] + " \ t \ t " + loginTime [ maxIndex
] + " ␣ " + loginDate [ maxIndex ] + " \ t " + logoutTime
[ maxIndex ]
70 + " ␣ " + logoutDate [ maxIndex ] + " \ t " +
formatTime ( duration [ maxIndex ]) + " \ n " ;
71 return output ;
72 }
73
74 private String formatTime ( int minutes ) {
75 int hr = minutes / 60;
76 int min = minutes % 60;
77 return hr + " : " + min + ( min < 10 ? " 0 " : " " ) ;
78 }
79 }
80
81 public class ISC2004Q3 {
82 public static void main ( String [] args ) {
83 Scanner in = new Scanner ( System . in ) ;
84 System . out . println ( " INPUT : " ) ;
85 System . out . print ( " Number ␣ of ␣ users : ␣ " ) ;
86 int users = in . nextInt () ;
87 int id [] = new int [ users ];
88 String loginTime [] = new String [ users ];
89 String loginDate [] = new String [ users ];
90 String logoutTime [] = new String [ users ];
91 String logoutDate [] = new String [ users ];
92 System . out . println ( " USER \ t \ tLOGIN \ t \ tLOGOUT " ) ;
93 System . out . println ( " IDENTIFICATION \ tTIME ␣ & ␣ DATE \
tTIME ␣ & ␣ DATE " ) ;
94 for ( int user = 0; user < users ; user ++) {
95 id [ user ] = in . nextInt () ;
96 loginTime [ user ] = in . next () ;
97 loginDate [ user ] = in . next () ;
98 logoutTime [ user ] = in . next () ;
99 logoutDate [ user ] = in . next () ;
100 }
101 UserLog log = new UserLog ( id , loginTime , loginDate ,
logoutTime , logoutDate ) ;
102
103 System . out . println ( " OUTPUT :\ n " + log ) ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


31

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

Explanation Video: https://www.youtube.com/watch?v=byUS0-75W0Q

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 () ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


35

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

Explanation Video: https://www.youtube.com/watch?v=0xzE1cef-JA

A wondrous square is an n by n grid which fulfills the following conditions:

• It contains integers from 1 to n2 , where each integer appears only once.


• The sum of integers in any row or column must add up to 0.5 ∗ n ∗ (n2 + 1).

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:

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


37

NOT A WONDROUS SQUARE.


PRIME ROW INDEX COLUMN INDEX
2 0 1
3 1 0
5 1 2
7 1 1

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

22 flag [ a [ row ][ col ] - 1] = true ;


23 }
24 if ( sumOfRows != sum || sumOfCols != sum )
25 return false ;
26 }
27 return true ;
28 }
29
30 public String toString () {
31 String output = " PRIME \ tROW ␣ INDEX \ tCOLUMN ␣ INDEX \ n " ;
32 for ( int row = 0; row < N ; row ++) {
33 for ( int col = 0; col < N ; col ++) {
34 if ( isPrime ( a [ row ][ col ]) ) {
35 output += a [ row ][ col ] + " \ t " + row + " \ t
\ t " + col + " \ n " ;
36 }
37 }
38 }
39 return output ;
40 }
41
42 private static boolean isPrime ( int n ) {
43 if ( n < 2)
44 return false ;
45 else if ( n == 2)
46 return true ;
47 else if ( n % 2 == 0)
48 return false ;
49 else {
50 int limit = ( int ) Math . sqrt ( n ) ;
51 for ( int i = 3; i <= limit ; i += 2) {
52 if ( n % i == 0)
53 return false ;
54 }
55 }
56 return true ;
57 }
58 }
59
60 public class ISC2005Q2 {
61 public static void main ( String [] args ) {
62 Scanner in = new Scanner ( System . in ) ;
63 System . out . print ( " INPUT :\ nN ␣ = ␣ " ) ;
64 int N = in . nextInt () ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


39

65 int a [][] = new int [ N ][ N ];


66 for ( int row = 0; row < N ; row ++) {
67 for ( int col = 0; col < N ; col ++) {
68 a [ row ][ col ] = in . nextInt () ;
69 }
70 }
71 Square sq = new Square ( a ) ;
72
73 System . out . println ( " OUPUT : " ) ;
74 if ( sq . isWondrousSquare () ) {
75 System . out . println ( " YES ␣ IT ␣ REPRESENTS ␣ A ␣ WONDROUS
␣ SQUARE . " ) ;
76 } else {
77 System . out . println ( " NOT ␣ A ␣ WONDROUS ␣ SQUARE . " ) ;
78 }
79 System . out . println ( sq ) ;
80 in . close () ;
81 }
82 }

ISC-2005, Question 3
Anagram

Explanation Video: https://www.youtube.com/watch?v=q93F6GW0Sao

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

Total number of anagrams=2

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 = " " ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


41

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 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


7. ISC-2006

ISC-2006, Question 1
Consecutive Integer Sum

Explanation Video: https://www.youtube.com/watch?v=mDXNfAkhAds

A positive natural number, (for e.g. 27), can be represented as follows:


2+3+4+5+6+7
8+9+10
13+14
Where every row represents a combination of consecutive natural numbers, which add up to
27.
Write a program which inputs a positive natural number N and prints the possible consecutive
number combinations, which when added give N.
Test your program for the following data and some random data.
SAMPLE DATA
INPUT: N = 9
OUTPUT:
4 + 5
2 + 3 + 4

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 ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


45

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

Explanation Video: https://www.youtube.com/watch?v=EvwvBYOzxH0

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

Enter number of names in Array B, M = 3


First array: (A)
Suman
Anil
Second array: (B)
Usha
Sachin
John
OUTPUT:
Sorted Merged array: (C)
Anil
John
Sachin
Suman
Usha
Sorted first array: (A)
Anil
Suman
Sorted Second array: (B)
John
Sachin
Usha

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 () {

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


47

20 int indexA , indexB , indexC ;


21 indexA = indexB = indexC = 0;
22 while ( indexA < A . length && indexB < B . length ) {
23 if ( A [ indexA ]. compareTo ( B [ indexB ]) < 0) {
24 C [ indexC ++] = A [ indexA ++];
25 } else {
26 C [ indexC ++] = B [ indexB ++];
27 }
28 }
29 if ( indexA < A . length ) {
30 for ( int i = indexA ; i < A . length ; i ++) {
31 C [ indexC ++] = A [ indexA ++];
32 }
33 } else if ( indexB < B . length ) {
34 for ( int i = indexB ; i < B . length ; i ++) {
35 C [ indexC ++] = B [ indexB ++];
36 }
37 }
38 }
39
40 public String toString () {
41 String output = " " ;
42 output += " Sorted ␣ Merged ␣ array : ␣ ( C ) \ n " ;
43 for ( String name : C ) {
44 output += name + " \ n " ;
45 }
46
47 output += " Sorted ␣ first ␣ array : ␣ ( A ) \ n " ;
48 for ( String name : A ) {
49 output += name + " \ n " ;
50 }
51
52 output += " Sorted ␣ second ␣ array : ␣ ( B ) \ n " ;
53 for ( String name : B ) {
54 output += name + " \ n " ;
55 }
56
57 return output ;
58 }
59 }
60
61 public class ISC2006Q2 {
62 public static void main ( String [] args ) {
63 Scanner in = new Scanner ( System . in ) ;

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
48 Chapter 7. ISC-2006

64 System . out . println ( " INPUT : " ) ;


65 System . out . print ( " Enter ␣ number ␣ of ␣ names ␣ in ␣ Array ␣A , ␣
N␣=␣");
66 int N = in . nextInt () ;
67 System . out . print ( " Enter ␣ number ␣ of ␣ names ␣ in ␣ Array ␣B , ␣
M␣=␣");
68 int M = in . nextInt () ;
69 in . nextLine () ;
70
71 String A [] = new String [ N ];
72 String B [] = new String [ M ];
73
74 System . out . println ( " First ␣ array : ␣ ( A ) " ) ;
75 for ( int i = 0; i < N ; i ++) {
76 A [ i ] = in . nextLine () ;
77 }
78
79 System . out . println ( " First ␣ array : ␣ ( B ) " ) ;
80 for ( int i = 0; i < M ; i ++) {
81 B [ i ] = in . nextLine () ;
82 }
83
84 MyArray obj = new MyArray (A , B ) ;
85
86 System . out . println ( " OUTPUT : " ) ;
87 System . out . println ( obj ) ;
88
89 in . close () ;
90 }
91 }

ISC-2006, Question 3
Security Code Validation

Explanation Video: https://www.youtube.com/watch?v=G1q8E8k78TE

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.

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


49

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 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


51

36 output = " Valid ! " ;


37 }
38 }
39
40 public String toString () {
41 return output ;
42 }
43 }
44
45 public class ISC2006Q3 {
46 public static void main ( String [] args ) {
47 Scanner in = new Scanner ( System . in ) ;
48 System . out . print ( " INPUT :\ t " ) ;
49 int N = in . nextInt () ;
50 in . nextLine () ;
51 if ( N > 6) {
52 System . out . println ( " OUTPUT :\ nError ! ␣ Length ␣ of ␣
string ␣ should ␣ not ␣ exceed ␣ 6 ␣ characters ! " ) ;
53 } else {
54 String code = in . nextLine () ;
55 CopyProtectionCode obj = new CopyProtectionCode (
code , N ) ;
56 System . out . println ( " OUTPUT :\ n " + obj ) ;
57 }
58
59 in . close () ;
60 }
61 }

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
8. ISC-2007

ISC-2007, Question 1
Day on a given date and date validation

Explanation Video: https://www.youtube.com/watch?v=v8qd1KoNSjw

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

1 import java . util .*;


2
3 class MyDate {
4 private int dd , mm , yyyy , index ;
5 private String inputDay ;
6 private static String weekDays [] = { " MONDAY " , " TUESDAY "
, " WEDNESDAY " , " THURSDAY " , " FRIDAY " , " SATURDAY " , "
SUNDAY " };
7 private static int days [] = { 31 , 28 , 31 , 30 , 31 , 30 ,
31 , 31 , 30 , 31 , 30 , 31 };
8
9 public MyDate ( String inputDate , String inputDay ) {
10 String part [] = inputDate . split ( " / " ) ;
11 dd = Integer . parseInt ( part [0]) ;
12 mm = Integer . parseInt ( part [1]) ;
13 yyyy = Integer . parseInt ( part [2]) ;
14 this . inputDay = inputDay ;
15 if ( isLeap () )
16 days [1] = 29;
17 }
18
19 public boolean isLeap () {
20 int divisor = yyyy % 100 == 0 ? 400 : 4;
21 return yyyy % divisor == 0;
22 }
23
24 public boolean validate () {
25 index = -1;
26 for ( int i = 0; i < weekDays . length ; i ++) {
27 if ( inputDay . equals ( weekDays [ i ]) ) {
28 index = i ;
29 break ;
30 }
31 }
32 if ( index == -1)
33 return false ; // day invalid
34 if ( yyyy < 0)
35 return false ; // checking for - ve years
36 if ( mm < 1 || mm > 12)
37 return false ; // month must be between 1 -12 (
inclusive )
38 if ( dd < 1 || dd > days [ mm - 1])
39 return false ; // number of day check
40 return true ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


55

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

Explanation Video: https://www.youtube.com/watch?v=17JL6nOvuv8

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 {

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


57

4 private String sentence , output ;


5
6 public Sentence ( String lines ) {
7 this . sentence = lines ;
8 output = " " ;
9 reverseWords () ;
10 }
11
12 private void reverseWords () {
13 String words [] = sentence . split ( " [ ' ,;:.\ n \ t ␣ ]+ " ) ;
14 output = words [0];
15 for ( int i = 1; i < words . length ; i ++) {
16 output = words [ i ] + " ␣ " + output ;
17 }
18 }
19
20 public String toString () {
21 return output ;
22 }
23 }
24
25 class ISC2007Q2 {
26 public static void main ( String [] args ) {
27 Scanner in = new Scanner ( System . in ) ;
28 String lines = " " ;
29 System . out . print ( " INPUT :\ t " ) ;
30 int N = in . nextInt () ;
31 in . nextLine () ;
32 for ( int i = 0; i < N ; i ++) {
33 lines += in . nextLine () ;
34 }
35 Sentence obj = new Sentence ( lines ) ;
36 System . out . println ( " OUTPUT :\ n " + obj ) ;
37
38 in . close () ;
39 }
40 }

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
58 Chapter 8. ISC-2007

ISC-2007, Question 3
Unique-digits integers in a range

Explanation Video: https://www.youtube.com/watch?v=ggv6ABS9npE

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 ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


59

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

53 Unique obj = new Unique (m , n ) ;


54 System . out . println ( " OUTPUT : " + obj ) ;
55 System . out . println ( " FREQUENCY ␣ OF ␣ UNIQUE - DIGIT ␣ IN ␣
INTEGERS ␣ IS ␣ : ␣ " + obj . getFrequency () ) ;
56 in . close () ;
57 }
58 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


9. ISC-2008

ISC-2008, Question 1
Smith Number

Explanation Video: https://www.youtube.com/watch?v=2K253CKT3RM

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)

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


63

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

Explanation Video: https://www.youtube.com/watch?v=aJvNBwBgNnc

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.

Write a program to:

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

Sentence No. of vowels / words


1 VVVVVV
WWW

2 VVVVVVVVVVVVVVV
WWWWWWWWW

3 VVVVVVVVVVVVVVVVVVVVVVVV
WWWWWWWWWWWW

4 VVVVVVVVV
WWWWWWWWW

Scale used 1:3

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

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


65

13 System . out . println ( Arrays . toString ( sentence ) ) ;


14 computeStats () ;
15 }
16
17 private static boolean isVowel ( char ch ) {
18 return vowels . indexOf ( ch ) >= 0;
19 }
20
21 private void computeStats () {
22 char ch ;
23 max = 0;
24 sentence [0] = " ␣ " + sentence [0];
25 for ( int i = 0; i < sentence . length ; i ++) {
26 for ( int j = 0; j < sentence [ i ]. length () ; j ++) {
27 ch = sentence [ i ]. charAt ( j ) ;
28 if ( ch == '␣ ')
29 word [ i ]++;
30 if ( isVowel ( ch ) )
31 vowel [ i ]++;
32 }
33 if ( vowel [ i ] > max )
34 max = vowel [ i ];
35 if ( word [ i ] > max )
36 max = word [ i ];
37 }
38 }
39
40 public void showStats () {
41 System . out . println ( " Sentence \ tNo . ␣ of ␣ Vowels \ tNo . ␣ of ␣
words " ) ;
42 for ( int i = 0; i < sentence . length ; i ++) {
43 System . out . println (( i + 1) + " \ t \ t " + vowel [ i ] +
" \ t \ t " + word [ i ]) ;
44 }
45 }
46
47 private String repeat ( char what , int times ) {
48 String output = " " ;
49 for ( int i = 1; i <= times ; i ++) {
50 output += what ;
51 }
52 return output ;
53 }
54

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
66 Chapter 9. ISC-2008

55 public void plotHistogram () {


56 int width = 50;
57 float scalingFactor = width / max ;
58 System . out . println ( " Sentence \ tNo . ␣ of ␣ vowels ␣ / ␣ words "
);
59 for ( int i = 0; i < sentence . length ; i ++) {
60 System . out . println (( i + 1) + " \ t " + repeat ( 'V ' ,
( int ) ( vowel [ i ] * scalingFactor ) ) ) ;
61 System . out . println ( " \ t " + repeat ( 'W ' , ( int ) (
word [ i ] * scalingFactor ) ) ) ;
62 }
63 System . out . println ( " Scale ␣ used ␣ 1: " + scalingFactor ) ;
64 }
65 }
66
67 public class ISC2008Q2 {
68 public static void main ( String [] args ) {
69 Scanner in = new Scanner ( System . in ) ;
70 System . out . print ( " INPUT :\ t " ) ;
71 String paragraph = in . nextLine () ;
72 Text obj = new Text ( paragraph ) ;
73
74 System . out . println ( " OUTPUT : " ) ;
75 obj . showStats () ;
76 obj . plotHistogram () ;
77 in . close () ;
78 }
79 }

ISC-2008, Question 3
Largest and Second Largest Elements in a 2D array

Explanation Video: https://www.youtube.com/watch?v=JkRFAYDJncI

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.

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


67

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

The largest element 9 is in row 3 and column 1


The second largest element 8 is in row 3 and column 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 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


69

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

Explanation Video: https://www.youtube.com/watch?v=2-5PdukkbnI

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 ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


73

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

Explanation Video: https://www.youtube.com/watch?v=9bvdqEzEK6Q

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

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


75

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 " ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


77

26 for ( col = 0; col < N ; col ++) {


27 output += A [ row ][ col ];
28 if ( col < N - 1)
29 output += " \ t " ;
30 }
31 output += " \ n " ;
32 }
33 return output ;
34 }
35
36 public void rearrangeMatrix () {
37 // Populating the 1 D array outer
38 int index = 0 , sum = 0;
39 for ( col = 0; col < N ; col ++) {
40 outer [ index ++] = A [0][ col ];
41 outer [ index ++] = A [ M - 1][ col ];
42 sum += A [0][ col ] + A [ M - 1][ col ];
43 }
44 for ( row = 1; row < M - 1; row ++) {
45 outer [ index ++] = A [ row ][0];
46 outer [ index ++] = A [ row ][ N - 1];
47 sum += A [ row ][0] + A [ row ][ N - 1];
48 }
49 // Sort 1 D array outer
50 Arrays . sort ( outer ) ;
51
52 // Place back the 1 D array outer into A [][]
53 index = 0;
54 for ( col = 0; col <= N - 1; col ++) { // Tol L - R
55 A [0][ col ] = outer [ index ++];
56 }
57 for ( row = 1; row <= M - 1; row ++) { // Right col T -
B
58 A [ row ][ N - 1] = outer [ index ++];
59 }
60 for ( col = N - 2; col >= 0; col - -) { // Bottom row R
-L
61 A [ M - 1][ col ] = outer [ index ++];
62 }
63 for ( row = M - 2; row >= 1; row - -) {
64 A [ row ][0] = outer [ index ++];
65 }
66 }
67

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
78 Chapter 10. ISC-2009

68 public void displayBoundaryElements () {


69 String output = " " ;
70 for ( row = 0; row < M ; row ++) {
71
72 for ( col = 0; col < N ; col ++) {
73 if ( row == 0 || col == 0 || row == M - 1 ||
col == N - 1)
74 output += " \ t " + A [ row ][ col ];
75 else
76 output += " \ t " ;
77
78 }
79 output += " \ n " ;
80 }
81 System . out . println ( output ) ;
82 }
83
84 public int getSumOfOuterElements () {
85 return sum ;
86 }
87 }
88
89 public class ISC2009Q2 {
90 public static void main ( String [] args ) {
91 Scanner in = new Scanner ( System . in ) ;
92 int M , N ;
93 System . out . print ( " INPUT :\ tM ␣ = ␣ " ) ;
94 M = in . nextInt () ;
95 System . out . print ( " \ tN ␣ = ␣ " ) ;
96 N = in . nextInt () ;
97 Matrix obj = new Matrix (M , N ) ;
98 obj . input () ;
99 System . out . println ( " OUTPUT : " ) ;
100 System . out . println ( " \ tORIGINAL ␣ MATRIX " ) ;
101 System . out . println ( obj ) ;
102 obj . rearrangeMatrix () ;
103 System . out . println ( " \ tREARRANGED ␣ ␣ MATRIX " ) ;
104 System . out . println ( obj ) ;
105 System . out . println ( " \ tBOUNDARY ␣ ELEMENTS " ) ;
106 obj . displayBoundaryElements () ;
107 System . out . println ( " \ tSUM ␣ OF ␣ THE ␣ OUTER ␣ ROW ␣ AND ␣
COLUMN ␣ ELEMENTS ␣ ␣ = ␣ ␣ " + obj . getSumOfOuterElements
() ) ;
108 in . close () ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


79

109 }
110 }

ISC-2009, Question 3
Sorting words in a sentence in ascending order by length

Explanation Video: https://www.youtube.com/watch?v=X8kYJ3kUDWY

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.

INPUT: Print the sentence in ascending order.


OUTPUT: In the print order sentence ascending.

INPUT: I love my country.


OUTPUT: I my love country.

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

15 for ( int pass = 0; pass < word . length - 1; pass ++) {


16 swapped = false ;
17 for ( int position = 0; position < word . length -
1 - pass ; position ++) {
18 if ( word [ position + 1]. length () < word [
position ]. length () ) {
19 temp = word [ position + 1];
20 word [ position + 1] = word [ position ];
21 word [ position ] = temp ;
22 swapped = true ;
23 }
24 }
25 if (! swapped )
26 break ;
27 beautify () ;
28 }
29 }
30
31 public void beautify () {
32 output = Character . toUpperCase ( word [0]. charAt (0) ) +
word [0]. substring (1) ;
33 for ( int i = 1; i < word . length ; i ++) {
34 output += " ␣ " + word [ i ];
35 }
36 output = output . trim () + " . " ;
37 }
38
39 public String toString () {
40 return output ;
41 }
42 }
43
44 public class ISC2009Q3 {
45 public static void main ( String [] args ) {
46 Scanner in = new Scanner ( System . in ) ;
47 System . out . print ( " INPUT :\ t " ) ;
48 String sentence = in . nextLine () ;
49 Sentence obj = new Sentence ( sentence ) ;
50 System . out . println ( " OUTPUT :\ t " + obj ) ;
51 in . close () ;
52 }
53 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


11. ISC-2010

ISC-2010, Question 1
Denomination Breakup

Explanation Video: https://www.youtube.com/watch?v=aGRPI6MY5nQ

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

TOTAL NUMBER OF NOTES = 21

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 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


83

37 System . out . printf ( " \ tTOTAL ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ = ␣ ␣ %9 d \


n " , amount ) ;
38 System . out . printf ( " \ tTOTAL ␣ NUMBER ␣ OF ␣ NOTES ␣ ␣ ␣ = ␣ ␣ %9 d \
n " , noteCount ) ;
39 }
40 }
41
42 class ISC2010Q1 {
43 public static void main ( String [] args ) {
44 Scanner in = new Scanner ( System . in ) ;
45 System . out . print ( " INPUT :\ t " ) ;
46 int amount = in . nextInt () ;
47 Amount amt = new Amount ( amount ) ;
48 System . out . println ( " OUTPUT :\ t " + amt ) ;
49 amt . showDenomination () ;
50 in . close () ;
51 }
52 }

ISC-2010, Question 2
Kaprekar number

Explanation Video: https://www.youtube.com/watch?v=5WWCNLZr3Kc

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 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


85

21 output = output . substring (0 , output . length () - 2) ;


22 }
23
24 private static boolean isKaprekarNumber ( int n ) {
25 int square = n * n , divisor = 1;
26 for ( int temp = n ; temp > 0; temp /= 10) {
27 divisor *= 10;
28 }
29 return n == ( square / divisor ) + ( square % divisor ) ;
30 }
31
32 public String toString () {
33 return output ;
34 }
35
36 public int getFrequency () {
37 return frequency ;
38 }
39 }
40
41 class ISC2010Q2 {
42 public static void main ( String [] args ) {
43 Scanner in = new Scanner ( System . in ) ;
44 System . out . println ( " INPUT : " ) ;
45 int p , q ;
46 System . out . print ( " p ␣ = ␣ " ) ;
47 p = in . nextInt () ;
48 System . out . print ( " q ␣ = ␣ " ) ;
49 q = in . nextInt () ;
50 Kaprekar obj = new Kaprekar (p , q ) ;
51 System . out . println ( " OUTPUT :\ nTHE ␣ KAPREKAR ␣ NUMBERS ␣
ARE : - " ) ;
52 System . out . println ( obj ) ;
53 System . out . println ( " FREQUENCY ␣ OF ␣ KAPREKAR ␣ NUMBERS ␣ IS
: ␣ " + obj . getFrequency () ) ;
54 in . close () ;
55 }
56 }

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
86 Chapter 11. ISC-2010

ISC-2010, Question 3
Frequency table of words

Explanation Video: https://www.youtube.com/watch?v=lImSg9EfAKc

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

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


87

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

29 // Sorting the arrays


30 int tempFreq ;
31 String tempWord ;
32 boolean swapped ;
33 for ( int pass = 0; pass < limit - 1; pass ++) {
34 swapped = false ;
35 for ( int position = 0; position < limit - 1 -
pass ; position ++) {
36 if ( frequency [ position + 1] < frequency [
position ]) {
37 tempFreq = frequency [ position + 1];
38 frequency [ position + 1] = frequency [
position ];
39 frequency [ position ] = tempFreq ;
40
41 tempWord = word [ position + 1];
42 word [ position + 1] = word [ position ];
43 word [ position ] = tempWord ;
44
45 swapped = true ;
46 }
47 }
48 if (! swapped )
49 break ;
50 }
51 output = " \ tWORD \ t \ tFREQUENCY \ n " ;
52 for ( int i = 0; i < limit ; i ++) {
53 output += " \ t " + word [ i ] + " \ t \ t " + frequency [ i ]
+ "\n";
54 }
55 }
56
57 public String toString () {
58 return output ;
59 }
60
61 public int getLength () {
62 return word . length ;
63 }
64 }
65
66 class ISC2010Q3 {
67 public static void main ( String [] args ) {
68 Scanner in = new Scanner ( System . in ) ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


89

69 System . out . println ( " INPUT : " ) ;


70 System . out . print ( " \ tEnter ␣ number ␣ of ␣ sentences : ␣ " ) ;
71 int n = in . nextInt () ;
72 in . nextLine () ;
73 if ( n >= 1 && n < 4) {
74 System . out . print ( " \ tEnter ␣ sentences :\ n \ t " ) ;
75 String paragraph = in . nextLine () ;
76 Frequency obj = new Frequency ( paragraph ) ;
77 System . out . println ( " \ tTotal ␣ number ␣ of ␣ words : ␣ " +
obj . getLength () ) ;
78 System . out . println ( obj ) ;
79 } else {
80 System . out . println ( " OUTPUT :\ nINVALID ␣ ENTRY " ) ;
81 }
82 in . close () ;
83 }
84 }

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
12. ISC-2011

ISC-2011, Question 1
Number to words

Explanation Video: https://www.youtube.com/watch?v=vW-ggfNlgPY

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

5 private String answer ;


6
7 public Number ( int n ) {
8 this . n = n ;
9 answer = " " ;
10 convertToWords () ;
11 }
12
13 public void convertToWords () {
14 String arr1 [] = { " ONE " , " TWO " , " THREE " , " FOUR " , "
FIVE " , " SIX " , " SEVEN " , " EIGHT " , " NINE " , " TEN " , "
ELEVEN " ,
15 " TWELVE " , " THIRTEEN " , " FOURTEEN " , " FIFTEEN " ,
" SIXTEEN " , " SEVENTEEN " , " EIGHTEEN " , "
NINETEEN " };
16 String arr2 [] = { " TWENTY " , " THIRTY " , " FORTY " , "
FIFTY " , " SIXTY " , " SEVENTY " , " EIGHT " , " NINETY " };
17 int factor [] = { 1000000000 , 10000000 , 100000 , 1000 ,
100 , 1 };
18 String units [] = { " ARAB " , " CRORE " , " LAKHS " , "
THOUSAND " , " HUNDRED " , " " };
19 int quotient , number = n , ones , tens ;
20 for ( int i = 0; i < factor . length ; i ++) {
21 quotient = number / factor [ i ];
22 if ( quotient > 0) {
23 if ( quotient < 20) {
24 answer = answer + arr1 [ quotient - 1] + "
␣";
25 } else {
26 ones = quotient % 10;
27 tens = quotient / 10;
28 if ( ones != 0) {
29 answer += arr2 [ tens - 2] + " ␣ " +
arr1 [ ones - 1] + " ␣ " ;
30 } else {
31 answer += arr2 [ tens - 2] + " ␣ " ;
32 }
33
34 }
35 answer += units [ i ] + " ␣ " ;
36 }
37
38 number = number % factor [ i ];
39 if ( number == 0)

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


93

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

Explanation Video: https://www.youtube.com/watch?v=t5rb0Dfq_oc

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 ) ) {

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


95

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

Explanation Video: https://www.youtube.com/watch?v=7Pn5mJrh9t8

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

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


97

1 import java . util .*;


2
3 class MyDate {
4 private int dd , mm , yyyy ;
5 private static int days [] = { 31 , 28 , 31 , 30 , 31 , 30 ,
31 , 31 , 30 , 31 , 30 , 31 };
6
7 public MyDate ( int dd , int mm , int yyyy ) {
8 this . dd = dd ;
9 this . mm = mm ;
10 this . yyyy = yyyy ;
11 if ( isLeap () )
12 days [1] = 29;
13 }
14
15 public boolean isLeap () {
16 int divisor = ( yyyy % 100 == 0) ? 400 : 4;
17 return yyyy % divisor == 0;
18 }
19
20 public boolean isValidDate () {
21 if ( yyyy < 0)
22 return false ;
23 if ( mm < 0 || mm > 12)
24 return false ;
25 if ( dd < 1 || dd > days [ mm - 1])
26 return false ;
27 return true ;
28 }
29
30 public int getDayNumber () {
31 int dayNumber = dd ;
32 for ( int i = 0; i < mm - 1; i ++) {
33 dayNumber += days [ i ];
34 }
35 return dayNumber ;
36 }
37
38 }
39
40 class ISC2011Q3 {
41 public static void main ( String [] args ) {
42 Scanner in = new Scanner ( System . in ) ;
43 System . out . println ( " INPUT :\ tEnter ␣ your ␣ date ␣ of ␣ birth

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
98 Chapter 12. ISC-2011

␣ in ␣ dd ␣ mm ␣ yyyy ␣ format " ) ;


44 int dd , mm , yyyy ;
45 System . out . print ( " \ t " ) ;
46 dd = in . nextInt () ;
47 System . out . print ( " \ t " ) ;
48 mm = in . nextInt () ;
49 System . out . print ( " \ t " ) ;
50 yyyy = in . nextInt () ;
51 MyDate obj = new MyDate ( dd , mm , yyyy ) ;
52 if ( obj . isValidDate () ) {
53 System . out . println ( " OUTPUT :\ n \ tVALID ␣ DATE \ n \ t " +
obj . getDayNumber () ) ;
54 } else {
55 System . out . println ( " Output :\ n \ tINVALID ␣ DATE " ) ;
56 }
57 in . close () ;
58 }
59 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


13. ISC-2012

ISC-2012, Question 1
Prime palindrome

Explanation Video: https://www.youtube.com/watch?v=Y0Xb2CtIZEI

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

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


101

38 public void process () {


39 output = " " ;
40 frequency = 0;
41 for ( int number = m % 2 == 0 ? m + 1 : m ; number <=
n ; number += 2) {
42 if ( isPalindrome ( number ) && isPrime ( number ) ) {
43 frequency ++;
44 output += number + " ,␣ " ;
45 }
46 }
47 output = output . substring (0 , output . length () - 2) ;
48 }
49
50 public String toString () {
51 return output ;
52 }
53
54 public int getFrequency () {
55 return frequency ;
56 }
57 }
58
59 class ISC2012Q1 {
60 public static void main ( String [] args ) {
61 Scanner in = new Scanner ( System . in ) ;
62 System . out . print ( " INPUT :\ tm ␣ = ␣ " ) ;
63 int m = in . nextInt () ;
64 System . out . print ( " \ tn ␣ = ␣ " ) ;
65 int n = in . nextInt () ;
66 if ( m < 3000 && n < 3000) {
67 PrimePalindrome obj = new PrimePalindrome (m , n ) ;
68 System . out . print ( " OUTPUT :\ tTHE ␣ PRIME ␣ PALINDROME ␣
INTEGERS ␣ ARE :\ n \ t " ) ;
69 System . out . println ( obj ) ;
70 System . out . println ( " \ tFREQUENCY ␣ OF ␣ PRIME ␣
PALINDROME ␣ INTEGERS : ␣ " + obj . getFrequency () ) ;
71 } else {
72 System . out . println ( " OUTPUT :\ tOUT ␣ OF ␣ RANGE " ) ;
73 }
74 in . close () ;
75 }
76 }

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
102 Chapter 13. ISC-2012

ISC-2012, Question 2
Sort words of a string

Explanation Video: https://www.youtube.com/watch?v=kNlj2XQrp0U

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 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


103

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

53 System . out . print ( " INPUT :\ n \ t " ) ;


54 String sentence = in . nextLine () ;
55 Sentence obj = new Sentence ( sentence ) ;
56 System . out . println ( " OUTPUT :\ n \ tLENGTH ␣ IS : ␣ " + obj .
getLength () ) ;
57 System . out . println ( " \ tREARRANGED ␣ SENTENCE " ) ;
58 System . out . println ( " \ t " + obj ) ;
59 in . close () ;
60 }
61 }

ISC-2012, Question 3
Sort a 2D array in ascending order

Explanation Video: https://www.youtube.com/watch?v=ifq75Zs8XIw

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

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


105

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 ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


107

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

112 obj . sortAscending () ;


113 System . out . println ( " REARRANGED ␣ MATRIX " ) ;
114 System . out . println ( obj ) ;
115 } else {
116 System . out . println ( " OUTPUT :\ n \ tSIZE ␣ OUT ␣ OF ␣ RANGE
");
117 }
118
119 in . close () ;
120 }
121 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


14. ISC-2013

ISC-2013, Question 1
Verify an ISBN number

Explanation Video: https://www.youtube.com/watch?v=ubZpUFlo7mg

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 ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


111

26 for ( int position = 0; position < isbn . length () ;


position ++) {
27 // Note that the character from 0 to 9 has codes
from 48 to 57
28 digit = isbn . charAt ( position ) - '0 '; // '0 ' will
result in subtraction of 48
29 digit = ( digit == 40 || digit == 72) ? 10 :
digit ;
30 sum += digit * (10 - position ) ;
31 }
32 }
33
34 public int getSum () {
35 return sum ;
36 }
37 }
38
39 class ISC2013Q1 {
40 public static void main ( String [] args ) {
41 Scanner in = new Scanner ( System . in ) ;
42 System . out . print ( " INPUT ␣ CODE :\ t " ) ;
43 String isbn = in . nextLine () ;
44 try {
45 ISBN objISBN = new ISBN ( isbn ) ;
46 System . out . println ( " OUTPUT :\ tSUM ␣ = ␣ " + objISBN .
getSum () ) ;
47 if ( objISBN . getSum () % 11 == 0) {
48 System . out . println ( " \ tLEAVES ␣ NO ␣ REMAINDER ␣
␣ VALID ␣ ISBN ␣ CODE " ) ;
49 } else {
50 System . out . println ( " \ tLEAVES ␣ REMAINDER ␣ ␣
INVALID ␣ ISBN ␣ CODE " ) ;
51 }
52 } catch ( IllegalArgumentException e ) {
53 System . out . println ( " OUTPUT ␣ : ␣ INVALID ␣ INPUT " ) ;
54 }
55 in . close () ;
56 }
57 }

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
112 Chapter 14. ISC-2013

ISC-2013, Question 2
Mirror image of a 2D array

Explanation Video: https://www.youtube.com/watch?v=MW1xO8AFal8

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

MIRROR IMAGE MATRIX


12 16 4
14 2 8
3 1 6

Example 2
INPUT : M = 22
OUTPUT : SIZE OUT OF RANGE

Solution
1 import java . util .*;
2
3 class Matrix {

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


113

4 private int A [][];


5 private int M ;
6
7 public Matrix ( int M ) {
8 this . M = M ;
9 A = new int [ M ][ M ];
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 < M ; col ++) {
16 A [ row ][ col ] = in . nextInt () ;
17 }
18 }
19 in . close () ;
20 }
21
22 public String toString () {
23 String output = " " ;
24 for ( int row = 0; row < M ; row ++) {
25 output += " \ t " ;
26 for ( int col = 0; col < M ; col ++) {
27 output += A [ row ][ col ];
28 if ( col < M - 1)
29 output += " \ t " ;
30 }
31 output += " \ n " ;
32 }
33 return output ;
34 }
35
36 public void mirror () {
37 int temp ;
38 for ( int row = 0; row < M ; row ++) {
39 for ( int col = 0; col < M / 2; col ++) {
40 temp = A [ row ][ col ];
41 A [ row ][ col ] = A [ row ][ M - 1 - col ];
42 A [ row ][ M - 1 - col ] = temp ;
43 }
44 }
45 }
46 }
47

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
114 Chapter 14. ISC-2013

48 public class ISC2013Q2 {


49 public static void main ( String [] args ) {
50 Scanner in = new Scanner ( System . in ) ;
51 System . out . print ( " INPUT :\ tM ␣ = ␣ " ) ;
52 int M = in . nextInt () ;
53 if ( M > 2 && M < 20) {
54 Matrix objMatrix = new Matrix ( M ) ;
55 objMatrix . getInput () ;
56 System . out . println ( " OUTPUT :\ tORIGINAL ␣ MATRIX " ) ;
57 System . out . println ( objMatrix ) ;
58 objMatrix . mirror () ;
59 System . out . println ( " \ tMIRROR ␣ IMAGE ␣ MATRIX " ) ;
60 System . out . println ( objMatrix ) ;
61 } else {
62 System . out . println ( " OUTPUT :\ tSIZE ␣ OUT ␣ OF ␣ RANGE " )
;
63 }
64 in . close () ;
65 }
66 }

ISC-2013, Question 3
Count palindromic words

Explanation Video: https://www.youtube.com/watch?v=gHaq_-xvn3E

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:

MADAM, ARORA, NOON

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

Example 1

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


115

INPUT: MOM AND DAD ARE COMING AT NOON.


OUTPUT: MOM DAD NOON
NUMBER OF PALINDROMIC WORDS: 3

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

27 ch = sentence . charAt ( position ) ;


28 if ( delimters . indexOf ( ch ) >= 0) {
29 if ( isPalindrome ( word ) ) {
30 answer += word + " ␣ " ;
31 frequency ++;
32 }
33 word = " " ;
34 } else {
35 word += ch ;
36 }
37 }
38 }
39
40 public int getFrequency () {
41 return frequency ;
42 }
43
44 public String toString () {
45 return answer . trim () ;
46 }
47 }
48
49 public class ISC2013Q3 {
50 public static void main ( String [] args ) {
51 Scanner in = new Scanner ( System . in ) ;
52 System . out . print ( " INPUT :\ t " ) ;
53 String sentence = in . nextLine () ;
54 Palindrome objPalindrome = new Palindrome ( sentence ) ;
55 System . out . print ( " OUTPUT :\ t " ) ;
56 if ( objPalindrome . getFrequency () > 0) {
57 System . out . println ( objPalindrome ) ;
58 System . out . println ( " \ tNUMBER ␣ OF ␣ PALINDROMIC ␣
WORDS : ␣ " + objPalindrome . getFrequency () ) ;
59 } else {
60 System . out . println ( " NO ␣ PALINDROMIC ␣ WORDS " ) ;
61 }
62 in . close () ;
63 }
64 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


15. ISC-2014

ISC-2014, Question 1
Composite magic number

Explanation Video: https://www.youtube.com/watch?v=gaPZsELz1zk

A Composite Magic number is a positive integer which is composite as well as a magic


number.
Composite number: A composite number is a number that has more than two factors.
For example: 10, factors are: 1, 2, 5, 10
Magic number: A magic number is a number in which the eventual sum of the digits is equal
to 1. For example: 28 = 2 + 8 = 10 = 1 + 0 = 1
Accept two positive integers m and n, where m is less than n as user input.
Display the number of Composite Magic integers that are in the range between m and n (both
inclusive) and output them along with the frequency, in the format specified below.
Example 1:

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

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


119

28 public int getFrequency () {


29 return frequency ;
30 }
31
32 private static boolean isPrimeNumber ( int n ) {
33 if ( n < 2)
34 return false ;
35 else if ( n == 2)
36 return true ;
37 else if ( n % 2 == 0)
38 return false ;
39 else {
40 // number is odd
41 int limit = ( int ) Math . sqrt ( n ) ;
42 for ( int divisor = 3; divisor <= limit ; divisor
+= 2) {
43 if ( n % divisor == 0)
44 return false ;
45 }
46 }
47 return true ;
48 }
49
50 private static boolean isCompositeNumber ( int n ) {
51 return ! isPrimeNumber ( n ) ;
52 }
53
54 private static boolean isMagicNumber ( int n ) {
55 return n % 9 == 1;
56 }
57 }
58
59 class ISC2014Q1 {
60 public static void main ( String [] args ) {
61 Scanner in = new Scanner ( System . in ) ;
62 System . out . print ( " INPUT :\ tm ␣ = ␣ " ) ;
63 int m = in . nextInt () ;
64 System . out . print ( " \ tn ␣ = ␣ " ) ;
65 int n = in . nextInt () ;
66 System . out . print ( " OUTPUT :\ t " ) ;
67 if ( m < n ) {
68 CompositeMagicNumber obj = new
CompositeMagicNumber (m , n ) ;
69 obj . process () ;

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
120 Chapter 15. ISC-2014

70 System . out . println ( obj ) ;


71 System . out . println ( " FREQUENCY ␣ OF ␣ COMPOSITE ␣ MAGIC
␣ INTEGERS ␣ IS : ␣ " + obj . getFrequency () ) ;
72 } else {
73 System . out . println ( " INVALID ␣ INPUT " ) ;
74 }
75 in . close () ;
76 }
77 }

ISC-2014, Question 2
Symmetry and sum of diagonals of a 2D array

Explanation Video: https://www.youtube.com/watch?v=5Ec0CIBQiRY

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

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


121

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 ) ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


123

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

Explanation Video: https://www.youtube.com/watch?v=ptNxqOZisGY

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

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


WORD TO BE DELETED: IS
WORD POSITION IN THE SENTENCE: 6
OUTPUT: A MORNING WALK IS A BLESSING FOR THE WHOLE DAY.

Example 2

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
124 Chapter 15. ISC-2014

INPUT: AS YOU SOW, SO SO YOU REAP.


WORD TO BE DELETED: SO
WORD POSITION IN THE SENTENCE : 4
OUTPUT: AS YOU SOW, SO YOU REAP.

Example 3

INPUT: STUDY WELL ##


OUTPUT: INVALID INPUT.

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 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


125

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

Explanation Video: https://www.youtube.com/watch?v=GlgefOejImg

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

OUTPUT: The required number = 119


Total number of digits = 3

Example 2

INPUT: M = 1500
N = 25

OUTPUT: The required number = 1699


Total number of digits = 4

Example 3

INPUT: M = 99
N = 11

OUTPUT: INVALID INPUT

Example 4

INPUT: M = 112
N = 130

OUTPUT: INVALID INPUT

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;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


129

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

60 System . out . print ( " INPUT :\ tM ␣ = ␣ " ) ;


61 int M = in . nextInt () ;
62 System . out . print ( " \ tN ␣ = ␣ " ) ;
63 int N = in . nextInt () ;
64 if ( M < 100 || M > 10000 || N < 0 || N >= 100) {
65 System . out . println ( " OUTPUT :\ tINVALID ␣ INPUT " ) ;
66 } else {
67 Number num = new Number (M , N ) ;
68 num . findSolution () ;
69 if ( num . hasSolution () ) {
70 System . out . println ( num ) ;
71 } else {
72 System . out . println ( " OUTPUT :\ tNo ␣ solution ␣
exists " ) ;
73 }
74
75 }
76 in . close () ;
77 }
78 }

ISC-2015, Question 2
Rotate a 2D matrix

Explanation Video: https://www.youtube.com/watch?v=qQdGZGIri9U

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.

Perform the following tasks:

1. Display the original matrix.


2. Rotate the matrix 90◦ clockwise as shown below:
Original matrix Rotated matrix
1 2 3 7 4 1
4 5 6 8 5 2
7 8 9 9 6 3

3. Find the sum of the elements of the four corners of the matrix.

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


131

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 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


133

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

Explanation Video: https://www.youtube.com/watch?v=e4Kni3t0z3o

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

Perform the following tasks:


1. Convert the first letter of each word to uppercase.
2. Find the number of vowels and consonants in each word and display them with proper
heading along with the words.
Test your program with the following inputs:
Example 1
INPUT: Intelligence plus character is education.
OUTPUT: Intelligence Plus Character Is Education
Word Vowels Consonants
Intelligence 5 7
Plus 1 3
Character 3 6
Is 1 1
Education 5 4

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;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


135

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

51 System . out . printf ( " % -15 s ␣ % -12 d ␣ % -12 d \ n " ,


titleCase , vowels , consonants ) ;
52 }
53 }
54 }
55
56 class ISC2015Q3 {
57 public static void main ( String [] args ) {
58 Scanner in = new Scanner ( System . in ) ;
59 System . out . print ( " INPUT :\ t " ) ;
60 String input = in . nextLine () ;
61 System . out . print ( " OUTPUT :\ t " ) ;
62 Sentence objSentence = new Sentence ( input ) ;
63 if ( objSentence . isValid () ) {
64 System . out . println ( objSentence ) ;
65 objSentence . displayStats () ;
66 } else {
67 System . out . println ( " INVALID ␣ INPUT " ) ;
68 }
69 in . close () ;
70 }
71 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


17. ISC-2016

ISC-2016, Question 1
Circular prime number

Explanation Video: https://www.youtube.com/watch?v=eIwSZdVDisg

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 ) {

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


139

30 System . out . println ( " \ t " + N + " ␣ IS ␣ A ␣ CIRCULAR ␣


PRIME " ) ;
31 } else {
32 System . out . println ( " \ t " + N + " ␣ IS ␣ NOT ␣ A ␣
CIRCULAR ␣ PRIME " ) ;
33 }
34 }
35
36 private static boolean isPrime ( int n ) {
37 if ( n < 2)
38 return false ;
39 else if ( n == 2)
40 return true ;
41 else if ( n % 2 == 0)
42 return false ;
43 else {
44 // number is odd
45 int limit = ( int ) Math . sqrt ( n ) ;
46 for ( int divisor = 3; divisor <= limit ; divisor
+= 2) {
47 if ( n % divisor == 0)
48 return false ;
49 }
50 }
51 return true ;
52 }
53 }
54
55 class ISC2016Q1 {
56 public static void main ( String [] args ) {
57 Scanner in = new Scanner ( System . in ) ;
58 System . out . print ( " INPUT :\ tN ␣ = ␣ " ) ;
59 int N = in . nextInt () ;
60 CircularPrime obj = new CircularPrime ( N ) ;
61 System . out . print ( " OUTPUT : " ) ;
62 obj . display () ;
63 in . close () ;
64 }
65 }

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
140 Chapter 17. ISC-2016

ISC-2016, Question 2
Sorting of boundary elements

Explanation Video: https://www.youtube.com/watch?v=O16c5CRBQpE

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

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


141

8 13
7 8

SUM OF THE DIAGONAL ELEMENTS = 59

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

SUM OF THE DIAGONAL ELEMENTS = 45

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 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


143

43 System . out . println () ;


44 }
45 }
46
47 public void displaySumOfDiagonals () {
48 System . out . println ( " SUM ␣ OF ␣ THE ␣ DIAGONAL ␣ ELEMENTS ␣ = ␣ "
+ getSumOfDiagonals ( A ) ) ;
49 }
50
51 private static int getSumOfDiagonals ( int A [][]) {
52 int sum = 0;
53 for ( int i = 0; i < A . length ; i ++) {
54 sum += A [ i ][ i ] + A [ i ][ A . length - 1 - i ];
55 }
56 return ( A . length % 2 == 1) ? sum - A [ A . length / 2][ A
. length / 2] : sum ;
57 }
58
59 public void sort () {
60 int temp [] = new int [( M - 2) * ( M - 2) ] , index = 0 ,
buffer ;
61 for ( int row = 1; row < M - 1; row ++) {
62 for ( int col = 1; col < M - 1; col ++) {
63 temp [ index ++] = A [ row ][ col ];
64 }
65 }
66 boolean swapped ;
67 for ( int pass = 0; pass < temp . length - 1; pass ++) {
68 swapped = false ;
69 for ( int x = 0; x < temp . length - 1 - pass ; x ++)
{
70 if ( temp [ x + 1] < temp [ x ]) {
71 buffer = temp [ x + 1];
72 temp [ x + 1] = temp [ x ];
73 temp [ x ] = buffer ;
74 swapped = true ;
75 }
76 }
77 if (! swapped )
78 break ;
79 }
80
81 index = 0;
82 for ( int row = 1; row < M - 1; row ++) {

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
144 Chapter 17. ISC-2016

83 for ( int col = 1; col < M - 1; col ++) {


84 A [ row ][ col ] = temp [ index ++];
85 }
86 }
87 }
88
89 }
90
91 public class ISC2016Q2 {
92 public static void main ( String [] args ) {
93 Scanner in = new Scanner ( System . in ) ;
94 System . out . print ( " INPUT :\ tM ␣ = ␣ " ) ;
95 int M = in . nextInt () ;
96 if ( M <= 3 || M >= 10) {
97 System . out . println ( " OUTPUT :\ tTHE ␣ MATRIX ␣ IS ␣ OUT ␣
OF ␣ RANGE " ) ;
98 } else {
99 Matrix A = new Matrix ( M ) ;
100 A . input () ;
101 System . out . println ( " OUTPUT : " ) ;
102 System . out . println ( " ORIGINAL ␣ MATRIX " ) ;
103 A . display () ;
104 System . out . println ( " REARRANGED ␣ MATRIX " ) ;
105 A . sort () ;
106 A . display () ;
107 System . out . println ( " DIAGONAL ␣ ELEMENTS " ) ;
108 A . displayDiagonals () ;
109 A . displaySumOfDiagonals () ;
110
111 }
112
113 in . close () ;
114 }
115 }

ISC-2016, Question 3
Manipulate words begining and ending with a vowel

Explanation Video: https://www.youtube.com/watch?v=eIwSZdVDisg

Write a program to accept a sentence which may be terminated by either ’.’, ’?’ or ’!’ only.

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


145

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

Perform the following tasks:

1. Find the number of words beginning an ending with a vowel.


2. Place the words which begin and with a vowel at the beginning, followed by the
remaining words as they occur in the sentence.

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

Example 1
INPUT: ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.
OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL

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 ) ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


147

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

Explanation Video: https://www.youtube.com/watch?v=Qn9zrUh8Z64

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

OUTPUT : INVALID INPUT

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) {

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


151

22 System . out . printf ( " % -43 s ␣ = ␣ %4 d \ n " , "


Remaining ␣ boxes " , N ) ;
23 } else {
24 System . out . printf ( " % -31 s ␣ %4 d ␣ x ␣ %4 d ␣ = ␣ %4 d \ n " ,
" Remaining ␣ boxes " , quantity , 1 , quantity
);
25 }
26 System . out . printf ( " % -43 s ␣ = ␣ %4 d \ n " , " Total ␣ mumber
␣ of ␣ boxes " , N ) ;
27 System . out . printf ( " % -43 s ␣ = ␣ %4 d \ n " , " Total ␣ mumber
␣ of ␣ cartons " , totalCartons ) ;
28 }
29 in . close () ;
30 }
31 }

ISC-2017, Question 2
Quiz score evaluation

Explanation Video: https://www.youtube.com/watch?v=8cvJCMdJLSE

The result of a quiz competition is to be prepared as follows:


The quiz has five questions with four multiple choices (A, B, C, D), with each question
carrying 1 mark for the correct answer.
Design a program to accept the number of participants N such that N must be greater than 3
and less than 11. Create a double dimensional array of size (N×5) to store the answers of each
participant row-wise.
Calculate the marks for each participant by matching the correct answer stored in a single
dimensional array of size 5. Display the scores for each participant and also the participant(s)
having the highest score.
Example: If the value of N = 4, then the array would be :
Q.1 Q.2 Q.3 Q.4 Q.5
Participant 1 A B B C A
Participant 2 D A D C B
Participant 3 A A B A C
Participant 4 D C C A B

Key to the question D C C A B

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

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


153

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 ␣ = ␣ " ) ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


155

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

Explanation Video: https://www.youtube.com/watch?v=L2XJHQyDKMs

Caesar Cipher is an encryption technique which is implemented as ROT13 (‘rotate by 13


places’). It is a simple letter substitution cipher that replaces a letter with the letter 13 places
after it in the alphabets, with the other characters remaining unchanged.
ROT13
A/a B/b C/c D/d E/e F/f G/g H/h I/i J/j K/k L/l M/m
N/n O/o P/p Q/q R/r S/s T/t U/v V/v W/w X/x Y/y Z/z
Write a program to accept a plain text of length L, where L must be greater than 3 and less
than 100.
Encrypt the text if valid as per the Caesar Cipher.
Test your program with the sample data and some random data:
Example 1
INPUT: Hello! How are you?
OUTPUT: The cipher text is:
Uryyb? Ubj ner lbh?

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 () ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


157

42 if ( L . length () > 3 && L . length () < 100) {


43 System . out . println ( " OUTPUT :\ tThe ␣ cipher ␣ text ␣ is :
");
44 CaesarCipher cipher = new CaesarCipher ( L ) ;
45 cipher . encode () ;
46 System . out . println ( cipher ) ;
47 } else {
48 System . out . println ( " OUTPUT :\ tINVALID ␣ LENGTH " ) ;
49
50 }
51 in . close () ;
52 }
53 }

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
19. ISC-2018

ISC-2018, Question 1
Goldbach numbers

Explanation Video: https://www.youtube.com/watch?v=hOqmjmVf2uY

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 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


161

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

Explanation Video: https://www.youtube.com/watch?v=ifq75Zs8XIw

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:

1. Display the original matrix.


2. Sort each row of the matrix in ascending order using any standard sorting technique.
3. Display the changed matrix after sorting each row.

Test your program for the following data:

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

MATRIX AFTER SORTING


-2 3 11
5 7 16
0 4 0
1 3 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

MATRIX AFTER SORTING ROWS


5 19 22
7 12 36
6 9 13

Example 3:
INPUT: M=11
N=5

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


163

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

38 System . out . println ( header ) ;


39 for ( int row = 0; row < A . length ; row ++) {
40 for ( int col = 0; col < A [ row ]. length ; col ++) {
41 System . out . print ( " \ t " + A [ row ][ col ]) ;
42 }
43 System . out . println () ;
44 }
45 }
46
47 // public static void sort ( int A [][]) {
48 // int temp ;
49 // boolean swapped ;
50 // for ( int row =0; row < A . length ; row ++) {
51 // for ( int pass =0; pass < A [ row ]. length -1; pass ++) {
52 // swapped = false ;
53 // for ( int x =0; x < A [ row ]. length -1 - pass ; x ++) {
54 // if ( A [ row ][ x +1] < A [ row ][ x ]) {
55 // temp = A [ row ][ x +1];
56 // A [ row ][ x +1]= A [ row ][ x ];
57 // A [ row ][ x ]= temp ;
58 // swapped = true ;
59 // }
60 // }
61 // if (! swapped ) break ;
62 // }
63 // }
64 // }
65 public static void sort ( int A []) {
66 int temp ;
67 boolean swapped ;
68
69 for ( int pass = 0; pass < A . length - 1; pass ++) {
70 swapped = false ;
71 for ( int x = 0; x < A . length - 1 - pass ; x ++) {
72 if ( A [ x + 1] < A [ x ]) {
73 temp = A [ x + 1];
74 A [ x + 1] = A [ x ];
75 A [ x ] = temp ;
76 swapped = true ;
77 }
78 }
79 if (! swapped )
80 break ;
81 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


165

82
83 }
84 }

ISC-2018, Question 3
Vertical banner

Explanation Video: https://www.youtube.com/watch?v=0ClseEk89sw

The names of teams participating in a competition should be displayed on a banner vertically,


to accomodate as many teams as possible in a single banner. Design a program to accept the
name of N teams, where 2 < N < 9 and display them in a vertical order, side by side with a
horizontal tab (i.e. eight spaces). Test your program for the following data and some random
data:

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 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


167

27 System . out . println () ;


28 }
29 } else {
30 System . out . println ( " OUTPUT :\ nINVALID ␣ INPUT " ) ;
31 }
32 in . close () ;
33 }
34 }

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
20. ISC-2019

ISC-2019, Question 1
Convert day number into date

Explanation Video: https://www.youtube.com/watch?v=2-5PdukkbnI

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 ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


171

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

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 ,
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 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


173

ISC-2019, Question 2
Sort and transfer 1D array into a 2D array

Explanation Video: https://www.youtube.com/watch?v=ddOg_9vl2UY

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 {

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


175

22 System . out . println ( " OUTPUT \ nMATRIX ␣ SIZE ␣ OUT ␣ OF ␣


RANGE " ) ;
23 }
24 in . close () ;
25 }
26
27 public static int [][] createAndFillMatrix ( int a []) {
28 int b [][] = new int [ a . length ][ a . length ];
29 int offset = 0;
30 for ( int row = 0; row < b . length ; row ++) {
31 offset = 0;
32 for ( int col = 0; col < b [ row ]. length ; col ++) {
33 if ( col < b . length - row ) {
34 b [ row ][ col ] = a [ col ];
35 } else {
36 b [ row ][ col ] = a [ offset ++];
37 }
38 }
39 }
40 return b ;
41 }
42
43 public static void bubbleSort ( int a []) {
44 int temp ;
45 for ( int pass = 0; pass < a . length - 1; pass ++) {
46 for ( int x = 0; x < a . length - 1 - pass ; x ++) {
47 if ( a [ x + 1] < a [ x ]) {
48 temp = a [ x + 1];
49 a [ x + 1] = a [ x ];
50 a [ x ] = temp ;
51 }
52 }
53 }
54 }
55
56 public static void display ( int a []) {
57 for ( int n : a ) {
58 System . out . print ( n + " ␣ " ) ;
59 }
60 System . out . println ( " \ b " ) ;
61 }
62
63 public static void display ( int b [][]) {
64 for ( int row = 0; row < b . length ; row ++) {

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
176 Chapter 20. ISC-2019

65 for ( int col = 0; col < b [ row ]. length ; col ++) {


66 System . out . print ( b [ row ][ col ] + " \ t " ) ;
67 }
68 System . out . println ( " \ b " ) ;
69 }
70 }
71 }

ISC-2019, Question 3
Make palindromic words

Explanation Video: https://www.youtube.com/watch?v=gHaq_-xvn3E

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?

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


177

ISI THEHT WATERETAW LEVEL RISINGNISIR

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

28 public boolean isValid () {


29 return " .?! " . indexOf ( sentence . charAt ( sentence . length
() - 1) ) >= 0;
30 }
31
32 public void convertToPalindrome () {
33 String reverse = " " ;
34 int delta ;
35 sentence = sentence . substring (0 , sentence . length () -
1) ;
36 words = sentence . split ( " ␣ " ) ;
37 for ( int i = 0; i < words . length ; i ++) {
38 if (! isPalindrome ( words [ i ]) ) {
39 reverse = " " ;
40 if ( words [ i ]. length () > 2
41 && words [ i ]. charAt ( words [ i ]. length ()
- 1) == words [ i ]. charAt ( words [ i
]. length () - 2) ) {
42 delta = 1;
43 } else {
44 delta = 0;
45 }
46 for ( int position = 0; position < words [ i ].
length () - 1 - delta ; position ++) {
47 reverse = words [ i ]. charAt ( position ) +
reverse ;
48 }
49 words [ i ] = words [ i ] + reverse ;
50 }
51 }
52 }
53
54 public static boolean isPalindrome ( String source ) {
55 int half = source . length () / 2;
56 for ( int position = 0; position <= half ; position ++)
{
57 if ( source . charAt ( position ) != source . charAt (
source . length () - 1 - position ) )
58 return false ;
59 }
60 return true ;
61 }
62
63 public String toString () {

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


179

64 String output = " " ;


65 for ( String word : words ) {
66 output += word + " ␣ " ;
67 }
68 return output ;
69 }
70 }

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
21. ISC-2020

ISC-2020, Question 1
Prime Adam integers

Explanation Video: https://www.youtube.com/watch?v=Gur681KgdSM

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 // }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


183

9 // return count ==2;


10 // }
11 public static boolean isPrimeNumber ( int n ) {
12 if ( n < 2)
13 return false ;
14 else if ( n == 2)
15 return true ;
16 else if ( n % 2 == 0)
17 return false ;
18 else {
19 int limit = ( int ) Math . sqrt ( n ) ;
20 for ( int divisor = 3; divisor <= limit ; divisor
+= 2) {
21 if ( n % divisor == 0)
22 return false ;
23 }
24 }
25 return true ;
26 }
27
28 public static int getReverse ( int n ) {
29 int reverse = 0;
30 for ( int temp = n ; temp > 0; temp /= 10) {
31 reverse = reverse * 10 + temp % 10;
32 }
33 return reverse ;
34 }
35
36 public static boolean isAdamNumber ( int n ) {
37 int reverse = getReverse ( n ) ;
38 return n * n == getReverse ( reverse * reverse ) ;
39 }
40
41 public static boolean isPrimeAdamNumber ( int n ) {
42 return isAdamNumber ( n ) && isPrimeNumber ( n ) ;
43 }
44
45 public static void main ( String [] args ) {
46 int m , n ;
47 Scanner input = new Scanner ( System . in ) ;
48 System . out . println ( " INPUT : " ) ;
49 System . out . print ( " m ␣ = ␣ " ) ;
50 m = input . nextInt () ;
51

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
184 Chapter 21. ISC-2020

52 System . out . print ( " n ␣ = ␣ " ) ;


53 n = input . nextInt () ;
54
55 System . out . println ( " OUTPUT : " ) ;
56 if ( m <= n ) {
57 int frequency = 0;
58 System . out . println ( " THE ␣ PRIME - ADAM ␣ INTEGERS ␣ ARE :
");
59 for ( int i = m ; i <= n ; i ++) {
60 if ( isPrimeAdamNumber ( i ) ) {
61 frequency ++;
62 System . out . print ( i + " ␣ " ) ;
63 }
64 }
65 if ( frequency == 0) {
66 System . out . println ( " \ bNIL " ) ;
67 } else {
68 System . out . println ( " \ b \ nFREQUENCY ␣ OF ␣ PRIME -
ADAM ␣ INTEGERS ␣ IS : ␣ " + frequency ) ;
69 }
70 } else {
71 System . out . println ( " INVALID ␣ INPUT " ) ;
72 }
73 input . close () ;
74 }
75 }

ISC-2020, Question 2
Rows of 2D array as octal representation

Explanation Video: https://www.youtube.com/watch?v=3tL4BCoKwoM

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)

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


185

1 5 6 (decimal equivalent of 1st row = 110 i.e. 1*8^2 + 5X8^1 + 6*8^0)

Perform the following tasks on the matrix:

1. Display the original matrix


2. Calculate the decimal equivalent for each row and display as per the format given below.

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

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


187

- 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

Explanation Video: https://www.youtube.com/watch?v=0YJ2HjyQMnE

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 () {

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


189

11 return " . ,?! " . indexOf ( sentence . charAt ( sentence .


length () - 1) ) >= 0;
12 }
13
14 public void arrange () {
15 String temp ;
16 sentence = sentence . substring (0 , sentence . length () -
1) ; // take everything except the last character
17 words = sentence . split ( " ␣ " ) ;
18 for ( int i = 0; i < words . length - 1; i ++) {
19 for ( int x = 0; x < words . length - 1 - i ; x ++) {
20 if ( words [ x + 1]. length () < words [ x ]. length
() ) {
21 temp = words [ x + 1];
22 words [ x + 1] = words [ x ];
23 words [ x ] = temp ;
24 } else if ( words [ x + 1]. length () == words [ x
]. length () ) {
25 if ( words [ x + 1]. compareTo ( words [ x ]) <
0) {
26 temp = words [ x + 1];
27 words [ x + 1] = words [ x ];
28 words [ x ] = temp ;
29 }
30 }
31 }
32 }
33 }
34
35 public String toString () {
36 String output = " " ;
37 // for ( int i =0; i < words . length ; i ++) {
38 // output += words [ i ]+" ";
39 // }
40 for ( String word : words ) {
41 output += word + " ␣ " ;
42 }
43 return output . trim () ; // return output +"\ b "
44 }
45 }
46
47 public class ISC2020Q3 {
48 public static void main ( String [] args ) {
49 Scanner in = new Scanner ( System . in ) ;

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 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


22. ISC-2021

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 ) ) {

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


193

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.

COMMOM WORDS FREQUENCY


IS 2

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


195

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.

OUTPUT: INDIA IS MY MOTHERLAND AND I AM PROUD OF MY MOTHERLAND.


ALL INDIANS ARE MY BROTHERS AND SISTERS.

COMMOM WORDS FREQUENCY


MY 3
AND 2

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

17 if ( delimiters . indexOf ( ch ) >= 0) { // delimiter


found
18 wordCount ++;
19 if ( position == paragraph . length () - 1) {
20 terminatorAtEnd = true ;
21 }
22 }
23 }
24 return wordCount == 2 && terminatorAtEnd ;
25 }
26
27 public void seperate () {
28 /*
29 * The method split () will return an array
30 * of strings ( that is the two sentences )
31 * and the split will take place wherever one or
32 * more of the terminating characters are occuring .
33 * Check https :// youtu . be / i00OWGPu6uQ for video .
34 */
35 sentence = paragraph . split ( " [.!? ,]+ " ) ;
36 System . out . println ( sentence [0]. trim () ) ;
37 System . out . println ( sentence [1]. trim () ) ;
38 }
39
40 public static String [] getCommonWords ( String wordList1
[] , String wordList2 []) {
41 String commonWords = " " ;
42 for ( int i = 0; i < wordList1 . length ; i ++) {
43 for ( int j = 0; j < wordList2 . length ; j ++) {
44 if ( wordList1 [ i ]. equals ( wordList2 [ j ]) ) {
45 if ( commonWords . indexOf ( wordList1 [ i ]) ==
-1) {
46 commonWords = commonWords + (
commonWords . length () > 0 ? " ," :
" " ) + wordList1 [ i ];
47 }
48 }
49 }
50 }
51 return commonWords . split ( " ," ) ;
52 }
53
54 public void computeFrequency () {
55 String wordList1 [] = sentence [0]. split ( " [ ␣ ,]+ " ) ;

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


197

56 String wordList2 [] = sentence [1]. split ( " [ ␣ ,]+ " ) ;


57 String commonWord [] = getCommonWords ( wordList1 ,
wordList2 ) ;
58 String word [] = paragraph . split ( " [ ,.!? ␣ ]+ " ) ;
59 int frequency [] = new int [ word . length ];
60 // Populating the arrays
61 int limit = word . length ;
62 for ( int i = 0; i < limit ; i ++) {
63 frequency [ i ] = 1;
64 for ( int j = i + 1; j < limit ; j ++) {
65 if ( word [ i ]. equals ( word [ j ]) ) {
66 word [ j ] = word [ limit - 1];
67 frequency [ i ]++;
68 limit - -;
69 j - -;
70 }
71 }
72 }
73
74 output = " \ tCOMMON ␣ WORD \ t \ tFREQUENCY \ n " ;
75 for ( int i = 0; i < limit ; i ++) {
76 if ( Arrays . asList ( commonWord ) . contains ( word [ i ]) )
{
77 output += " \ t " + word [ i ] + " \ t \ t \ t " +
frequency [ i ] + " \ n " ;
78 }
79 }
80 }
81
82 public void displayFrequencyTable () {
83 System . out . println ( output ) ;
84 }
85 }
86
87 public class ISC2021Q2 {
88 public static void main ( String [] args ) {
89 Scanner in = new Scanner ( System . in ) ;
90 System . out . print ( " INPUT : ␣ " ) ;
91 String text = in . nextLine () ;
92 Paragraph para = new Paragraph ( text ) ;
93 if ( para . isValid () ) {
94 System . out . println ( " OUTPUT : " ) ;
95 para . seperate () ;
96 para . computeFrequency () ;

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

SUM OF BOUNDARY ELEMENTS(UNSORTED) = 43


SORTED matrix
13 11 9 8
7 6 5 4
2 1 0 -3
SUM OF BOUNDARY ELEMENTS(SORTED) = 52

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


199

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

SUM OF BOUNDARY ELEMENTS(UNSORTED) = 65


SORTED matrix
17 14 9
8 6 5
4 2 1

SUM OF BOUNDARY ELEMENTS(SORTED) = 60

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 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh


201

48 output += " \ n " ;


49 }
50 return output ;
51 }
52
53 public void sortDescending () {
54 int temp ;
55 computeMaxiumPosition (0 , 0) ;
56 for ( int row = 0; row < M ; row ++) {
57 for ( int col = 0; col < N ; col ++) {
58 computeMaxiumPosition ( row , col ) ;
59 temp = A [ maxRow ][ maxCol ];
60 A [ maxRow ][ maxCol ] = A [ row ][ col ];
61 A [ row ][ col ] = temp ;
62 }
63 }
64 }
65
66 public int sumOfBoundaryElements () {
67 int sum = 0;
68 for ( int row = 0; row < M ; row ++) {
69
70 for ( int col = 0; col < N ; col ++) {
71 if ( row == 0 || col == 0 || row == M - 1 ||
col == N - 1)
72 sum += A [ row ][ col ];
73 }
74 }
75 return sum ;
76 }
77 }
78
79 public class ISC2021Q3 {
80 public static void main ( String [] args ) {
81 Scanner in = new Scanner ( System . in ) ;
82 System . out . print ( " INPUT :\ tM ␣ = ␣ " ) ;
83 int M = in . nextInt () ;
84 System . out . print ( " \ tN ␣ = ␣ " ) ;
85 int N = in . nextInt () ;
86 if ( M > 2 && N > 2 && M < 8 && N < 8) {
87 Matrix obj = new Matrix (M , N ) ;
88 obj . getInput () ;
89 System . out . println ( " OUPUT : " ) ;
90 System . out . println ( " ORIGINAL ␣ MATRIX " ) ;

https://www.youtube.com/@CodeWithVinay www.codewithvinay.com
202 Chapter 22. ISC-2021

91 System . out . println ( obj ) ;


92 System . out . println ( " SUM ␣ OF ␣ BOUNDARY ␣ ELEMENETS (
UNSORTED ) ␣ = ␣ " + obj . sumOfBoundaryElements () ) ;
93 obj . sortDescending () ;
94 System . out . println ( " REARRANGED ␣ MATRIX " ) ;
95 System . out . println ( obj ) ;
96 System . out . println ( " SUM ␣ OF ␣ BOUNDARY ␣ ELEMENETS (
SORTED ) ␣ = ␣ " + obj . sumOfBoundaryElements () ) ;
97 } else {
98 System . out . println ( " OUTPUT :\ n \ tOUT ␣ OF ␣ RANGE " ) ;
99 }
100
101 in . close () ;
102 }
103 }

Pragmatic Solutions To ISC Computer Science Practical Problems Vinay Singh

You might also like