Question 1
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.
Example 1:
INPUT :
DAY NUMBER : 233
YEAR : 2008
DATE AFTER (N) : 17
OUTPUT :
20TH AUGUST 2008
DATE AFTER 17 DAYS :
6TH SEPTEMBER 2008
Example 2:
INPUT :
DAY NUMBER : 360
YEAR : 2008
DATE AFTER (N) : 45
OUTPUT :
25TH DECEMBER 2008
DATE AFTER 45 DAYS :
8TH FEBRUARY 2009
Program
import [Link];
class ISC09Q1 {
int d, y, n;
int m[];
String s[];
ISC09Q1(int a, int b, int c) {
d = a;
y = b;
n = c;
m = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
s = new String[]{"", "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY",
"JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"};
}
boolean isLeap() {
return (y % 400 == 0) || ((y % 100 != 0) && (y % 4 == 0));
}
String getSuffix(int day) {
if (day >= 11 && day <= 13) return "TH";
switch (day % 10) {
case 1: return "ST";
case 2: return "ND";
case 3: return "RD";
default: return "TH";
}
}
void process() {
if (isLeap()) {
m[2] = 29;
}
int month = 1;
int day = d;
while (day > m[month]) {
day -= m[month];
month++;
}
display(day, month, y, "");
int futureDay = day + n;
int futureMonth = month;
int futureYear = y;
while (futureDay > m[futureMonth]) {
futureDay -= m[futureMonth];
futureMonth++;
if (futureMonth > 12) {
futureMonth = 1;
futureYear++;
if ((futureYear % 400 == 0) || ((futureYear % 100 != 0)
&& (futureYear % 4 == 0))) {
m[2] = 29;
} else {
m[2] = 28;
}
}
}
display(futureDay, futureMonth, futureYear, "DATE AFTER " + n + "
DAYS :");
}
void display(int day, int month, int year, String prefix) {
[Link](prefix + day + getSuffix(day) + " " + s[month] +
" " + year);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("DAY NUMBER : ");
int dayNum = [Link]();
[Link]("YEAR : ");
int year = [Link]();
[Link]("DATE AFTER (N) : ");
int nVal = [Link]();
if (dayNum < 1 || dayNum > 366) {
[Link]("DAY NUMBER OUT OF RANGE.");
} else if (year < 1000 || year > 9999) {
[Link]("YEAR OUT OF RANGE.");
} else if (nVal < 1 || nVal > 100) {
[Link]("DATE AFTER (N) OUT OF RANGE.");
} else {
ISC09Q1 obj = new ISC09Q1(dayNum, year, nVal);
[Link]();
}
}
}
Algorithm
Step 1: START
Step 2: READ day number 'd', year 'y', and future days 'n'.
Step 3: IF d < 1 OR d > 366 THEN DISPLAY error and STOP.
Step 4: IF y < 1000 OR y > 9999 THEN DISPLAY error and STOP.
Step 5: IF n < 1 OR n > 100 THEN DISPLAY error and STOP.
Step 6: DECLARE integer array 'm' with days in each month.
Step 7: CALL isLeap function to check if 'y' is a leap year.
Step 8: IF (y % 400 == 0) OR ((y % 100 != 0) AND (y % 4 == 0)) THEN RETURN true ELSE RETURN
false.
Step 9: IF isLeap is true THEN SET m[2] = 29.
Step 10: SET month = 1, day = d.
Step 11: WHILE day > m[month]
Step 12: SET day = day - m[month].
Step 13: INCREMENT month.
Step 14: ENDWHILE
Step 15: CALL display function with day, month, y to print the date.
Step 16: SET futureDay = day + n.
Step 17: SET futureMonth = month, futureYear = y.
Step 18: WHILE futureDay > m[futureMonth]
Step 19: SET futureDay = futureDay - m[futureMonth].
Step 20: INCREMENT futureMonth.
Step 21: IF futureMonth > 12 THEN
Step 22: SET futureMonth = 1.
Step 23: INCREMENT futureYear.
Step 24: CALL isLeap for futureYear and update m[2].
Step 25: ENDIF
Step 26: ENDWHILE
Step 27: CALL display function with futureDay, futureMonth, futureYear.
Step 28: STOP
Variable Description
Data Type Variable Name Description
int d Stores the input day number.
int y Stores the input year.
int n Stores the number of days after which the
int[] m An array to store the number of days in ea
String[] s An array to store the names of the months
int day Stores the calculated day of the month.
int month Stores the calculated month.
int futureDay Stores the day of the month for the future d
int futureMonth Stores the month for the future date.
int futureYear Stores the year for the future date.
Input/Output
Test 1:
INPUT :
DAY NUMBER : 233
YEAR : 2008
DATE AFTER (N) : 17
OUTPUT :
20TH AUGUST 2008
DATE AFTER 17 DAYS :
6TH SEPTEMBER 2008
Test 2:
INPUT :
DAY NUMBER : 360
YEAR : 2008
DATE AFTER (N) : 45
OUTPUT :
25TH DECEMBER 2008
DATE AFTER 45 DAYS :
8TH FEBRUARY 2009
Question 2
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 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 tasks on the matrix:
(a) Sort the elements of the outer row and column elements in ascending order using any standard
sorting technique and arrange them in an array.
(b) Calculate the sum of the outer row and column elements.
(c) Output the original matrix, rearranged matrix and only the boundary elements of the rearranged
array with their sum.
Example 1:
INPUT :
M=3
N=3
174
825
639
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
Program
import [Link];
class ISC09Q2 {
int m, n;
int a[][];
ISC09Q2(int r, int c) {
m = r;
n = c;
a = new int[m][n];
}
void readMatrix(Scanner sc) {
[Link]("Enter elements row wise:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = [Link]();
}
}
}
void process() {
int size = 2 * m + 2 * n - 4;
int b[] = new int[size];
int k = 0;
for (int j = 0; j < n; j++) b[k++] = a[0][j];
for (int i = 1; i < m - 1; i++) b[k++] = a[i][n-1];
for (int j = n - 1; j >= 0; j--) b[k++] = a[m-1][j];
for (int i = m - 2; i > 0; i--) b[k++] = a[i][0];
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (b[j] > b[j+1]) {
int t = b[j]; b[j] = b[j+1]; b[j+1] = t;
}
}
}
k = 0;
for (int j = 0; j < n; j++) a[0][j] = b[k++];
for (int i = 1; i < m - 1; i++) a[i][n-1] = b[k++];
for (int j = n - 1; j >= 0; j--) a[m-1][j] = b[k++];
for (int i = m - 2; i > 0; i--) a[i][0] = b[k++];
}
void display(String title, boolean boundaryOnly) {
[Link](title);
int sum = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || i == m - 1 || j == 0 || j == n - 1) {
[Link](a[i][j] + "\t");
sum += a[i][j];
} else {
[Link](boundaryOnly ? "\t" : a[i][j] + "\
t");
}
}
[Link]();
}
if (boundaryOnly) [Link]("SUM OF THE OUTER ROW AND
COLUMN ELEMENTS = " + sum);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter M = ");
int m_in = [Link]();
[Link]("Enter N = ");
int n_in = [Link]();
if (m_in <= 2 || m_in >= 20 || n_in <= 2 || n_in >= 20) {
[Link]("SIZE IS OUT OF RANGE");
} else {
ISC09Q2 obj = new ISC09Q2(m_in, n_in);
[Link](sc);
[Link]("ORIGINAL MATRIX", false);
[Link]();
[Link]("REARRANGED MATRIX", false);
[Link]("BOUNDARY ELEMENTS", true);
}
}
}
Algorithm
Step 1: START
Step 2: READ matrix dimensions 'm' and 'n'.
Step 3: IF m <= 2 OR m >= 20 OR n <= 2 OR n >= 20 THEN DISPLAY error and STOP.
Step 4: CREATE a 2D integer array 'a' of size m x n.
Step 5: CALL readMatrix function to fill the matrix.
Step 6: FOR i = 0 TO m-1
Step 7: FOR j = 0 TO n-1
Step 8: READ element into a[i][j].
Step 9: ENDFOR
Step 10: ENDFOR
Step 11: CALL display function to print the original matrix.
Step 12: CALL process function.
Step 13: CALCULATE size of boundary array 'b'.
Step 14: EXTRACT boundary elements from 'a' into 'b' in clockwise order.
Step 15: SORT array 'b' in ascending order (e.g., using bubble sort).
Step 16: PLACE sorted elements from 'b' back into the boundary of 'a'.
Step 17: CALL display function to print the rearranged matrix.
Step 18: CALL display function to print only boundary elements and their sum.
Step 19: STOP
Variable Description
Data Type Variable Name Description
int m Stores the number of rows in the matrix.
int n Stores the number of columns in the matrix
int[][] a The 2D array to store the matrix elements.
int[] b A 1D array to store and sort the boundary
int k An index variable for the boundary array 'b
int t A temporary variable used for swapping du
int sum Stores the sum of the boundary elements.
Input/Output
Test 1:
INPUT :
M=3
N=3
174
825
639
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
Question 3
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.
Example 1:
INPUT :
The lines are printed in reverse order.
OUTPUT :
In the are lines order printed reverse.
Example 2:
INPUT :
I love my country.
OUTPUT :
I my love country.
Program
import [Link].*;
class ISC09Q3 {
String s;
String w[];
int p[];
ISC09Q3(String str) {
s = [Link]().substring(0, [Link]() - 1);
}
void process() {
w = [Link](" ");
p = new int[[Link]];
for(int i=0; i < [Link]; i++) {
p[i] = i;
}
for (int i = 0; i < [Link] - 1; i++) {
for (int j = 0; j < [Link] - i - 1; j++) {
if (w[j].length() > w[j+1].length()) {
String tempWord = w[j]; w[j] = w[j+1]; w[j+1] =
tempWord;
int tempPos = p[j]; p[j] = p[j+1]; p[j+1] = tempPos;
} else if (w[j].length() == w[j+1].length()) {
if (p[j] > p[j+1]) {
String tempWord = w[j]; w[j] = w[j+1]; w[j+1]
= tempWord;
int tempPos = p[j]; p[j] = p[j+1]; p[j+1] =
tempPos;
}
}
}
}
}
void display() {
if ([Link] > 0) {
String first = w[0];
w[0] = [Link]([Link](0)) +
[Link](1);
}
[Link]("OUTPUT: ");
for (int i=0; i < [Link]; i++) {
[Link](w[i] + (i == [Link] - 1 ? "" : " "));
}
[Link](".");
}
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new
InputStreamReader([Link]));
[Link]("INPUT: ");
String input = [Link]();
ISC09Q3 obj = new ISC09Q3(input);
[Link]();
[Link]();
}
}
Algorithm
Step 1: START
Step 2: READ a sentence 's' that ends with a full stop.
Step 3: REMOVE the full stop and convert the sentence to lower case.
Step 4: SPLIT the sentence into words and store in a string array 'w'.
Step 5: CREATE an integer array 'p' to store the original positions of the words.
Step 6: INITIALIZE 'p' with indices from 0 to length-1.
Step 7: FOR i = 0 TO length-2
Step 8: FOR j = 0 TO length-i-2
Step 9: IF length of w[j] > length of w[j+1] THEN
Step 10: SWAP w[j] with w[j+1].
Step 11: SWAP p[j] with p[j+1].
Step 12: ELSE IF length of w[j] == length of w[j+1] THEN
Step 13: IF p[j] > p[j+1] THEN
Step 14: SWAP w[j] with w[j+1].
Step 15: SWAP p[j] with p[j+1].
Step 16: ENDIF
Step 17: ENDIF
Step 18: ENDFOR
Step 19: ENDFOR
Step 20: CAPITALIZE the first letter of the first word in 'w'.
Step 21: DISPLAY the words in array 'w' separated by spaces, ending with a full stop.
Step 22: STOP
Variable Description
Data Type Variable Name Description
String s Stores the input sentence.
String[] w An array to store the words of the sentence
int[] p An array to store the original position of ea
String tempWord A temporary variable for swapping words.
int tempPos A temporary variable for swapping position
Input/Output
Test 1:
INPUT :
The lines are printed in reverse order.
OUTPUT :
In the are lines order printed reverse.
Test 2:
INPUT :
I love my country.
OUTPUT :
I my love country.