0% found this document useful (0 votes)
10 views12 pages

ISC13

AI

Uploaded by

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

ISC13

AI

Uploaded by

xagopen600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question

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.

Design a program to accept a ten digits code from the user. For an invalid input, display an
appropriate message.

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.

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
Program
import java.util.*;
class ISC13Q1 {
String s;
int t;

ISC13Q1(String str) {
s = str;
t = 0;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.print("INPUT CODE: ");
String a = sc.nextLine();
ISC13Q1 obj = new ISC13Q1(a);
obj.check();
obj.process();
obj.display();
}

void check() {
if (s.length() != 10) {
System.out.println("INVALID INPUT");
System.exit(0);
}
}

void process() {
for (int i = 0; i < 9; i++) {
char ch = s.charAt(i);
t += Character.getNumericValue(ch) * (10 - i);
}
char ch = s.charAt(9);
if (ch == 'X' || ch == 'x') {
t += 10;
} else {
t += Character.getNumericValue(ch);
}
}

void display() {
System.out.println("SUM = " + t);
if (t % 11 == 0) {
System.out.println("LEAVES NO REMAINDER - VALID ISBN CODE");
} else {
System.out.println("LEAVES REMAINDER - INVALID ISBN CODE");
}
}
}

Algorithm
START
Step 1 : In main method, READ the code as a string.
Step 2 : CREATE an object of class ISC13Q1 and pass the string to the constructor.
Step 3 : The constructor INITIALIZES the class variable 's' with the input code.
Step 4 : CALL the process method.
Step 5 : CALL the display method.
Step 6 : In the process method, CHECK IF the length of string 's' is not equal to 10.
Step 7 : IF true, THEN RETURN to caller.
Step 8 : IF false, THEN INITIALIZE sum 't' to 0.
Step 9 : START a FOR loop from i = 0 TO 8.
Step 10: GET the character at index 'i'.
Step 11: CALCULATE the product of the character's numeric value and its weight (10-i).
Step 12: ADD the result to sum 't'.
Step 13: ENDFOR.
Step 14: GET the last character at index 9.
Step 15: IF the last character is 'X' or 'x', THEN ADD 10 to sum 't'.
Step 16: ELSE, ADD the numeric value of the last character to sum 't'.
Step 17: ENDIF.
Step 18: In the display method, CHECK IF the length of string 's' is not equal to 10.
Step 19: IF true, THEN DISPLAY 'INVALID INPUT'.
Step 20: ELSE, DISPLAY the sum 't'.
Step 21: CHECK IF sum 't' modulo 11 is 0.
Step 22: IF true, THEN DISPLAY 'LEAVES NO REMAINDER - VALID ISBN CODE'.
Step 23: ELSE, DISPLAY 'LEAVES REMAINDER - INVALID ISBN CODE'.
Step 24: ENDIF.
Step 25: ENDIF.
STOP
Variable Description
Data Type Variable Name Description
String s Stores the user-input ISBN code.
int t Stores the calculated sum of the ISBN digits.
int i Loop control variable.
char ch Stores each character of the ISBN string for processing.
char lastChar Stores the last character of the ISBN string.
ISC13Q1 obj Object of the class ISC13Q1.
Scanner sc Scanner object to take input.
String code Stores the input code read in the main method.

Input/Output
Test 1
INPUT :
CODE: 0201530821
OUTPUT:
SUM = 99
LEAVES NO REMAINDER - VALID ISBN CODE
Test 2
INPUT
CODE: 035680324
OUTPUT:
INVALID INPUT
Test 3
INPUT:
CODE: 0231428031
OUTPUT:
SUM = 122
LEAVES REMAINDER - INVALID ISBN CODE
Question
Write a program to declare a square matrix A[][] of order (M x M) where 'M' is the number of rows
and the number of columns such that M must be greater than 2 and less than 20. Allow the user to
input integers into this matrix.

Display appropriate error message for an invalid input. Perform the following tasks:
(a) Display the input matrix.
(b) Create a mirror image matrix.
(c) Display the mirror image matrix.

Example 1
INPUT:
M=3
4 16 12
8 2 14
4 1 3
OUTPUT:
ORIGINAL MATRIX
4 16 12
8 2 14
4 1 3
MIRROR IMAGE MATRIX
12 16 4
14 2 8
3 1 4
Example 2
INPUT:
M = 22
OUTPUT:
SIZE OUT OF RANGE
Program
import java.util.*;
class ISC13Q2 {
int m;
int[][] a, b;
Scanner sc;

ISC13Q2(int m) {
this.m = m;
if (m > 2 && m < 20) {
a = new int[m][m];
b = new int[m][m];
}
}

public static void main(String[] args) {


ISC13Q2 obj;
Scanner scMain = new Scanner(System.in);
System.out.print("ENTER SIZE OF MATRIX: ");
int x = scMain.nextInt();
System.out.println("INPUT: M = " + x);
obj = new ISC13Q2(x);
obj.sc = scMain;
obj.check();
obj.input();
obj.process();
obj.display();
scMain.close();
}

void check() {
if (m <= 2 || m >= 20) {
System.out.println("OUTPUT:");
System.out.println("SIZE OUT OF RANGE");
System.exit(0);
}
}

void input() {
System.out.println("ENTER ELEMENTS OF MATRIX");
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = sc.nextInt();
}
}
}

void process() {
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
b[i][j] = a[i][m - 1 - j];
}
}
}

void display() {
System.out.println("OUTPUT:");
System.out.println("ORIGINAL MATRIX");
print(a);
System.out.println("MIRROR IMAGE MATRIX");
print(b);
}

void print(int[][] x) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
System.out.print(x[i][j] + "\t");
}
System.out.println();
}
}
}Algorithm

START
Step 1 : In main method, READ the size of the matrix 'M'.
Step 2 : CREATE an object of class ISC13Q2, passing 'M' to the constructor.
Step 3 : The constructor INITIALIZES class variable 'm' and initializes matrices 'a' and 'b' IF 'm' is within
range.
Step 4 : CALL the accept method.
Step 5 : CALL the createMirror method.
Step 6 : CALL the display method.
Step 7 : In the accept method, CHECK IF 'm' is out of range (<=2 or >=20).
Step 8 : IF true, THEN RETURN to caller.
Step 9 : ELSE, START a FOR loop for rows 'r' from 0 TO m-1.
Step 10: START a nested FOR loop for columns 'c' from 0 TO m-1.
Step 11: READ an integer and STORE it in matrix 'a' at [r][c].
Step 12: ENDFOR for columns.
Step 13: ENDFOR for rows.
Step 14: In createMirror method, CHECK IF 'm' is out of range. IF true, THEN RETURN.
Step 15: START a FOR loop for rows 'r' from 0 TO m-1.
Step 16: START a nested FOR loop for columns 'c' from 0 TO m-1.
Step 17: ASSIGN the element from a[r][m-1-c] to b[r][c].
Step 18: ENDFOR for columns.
Step 19: ENDFOR for rows.
Step 20: In the display method, CHECK IF 'm' is out of range.
Step 21: IF true, THEN DISPLAY 'SIZE OUT OF RANGE'.
Step 22: ELSE, DISPLAY 'ORIGINAL MATRIX' heading.
Step 23: CALL printMatrix for matrix 'a'.
Step 24: DISPLAY 'MIRROR IMAGE MATRIX' heading.
Step 25: CALL printMatrix for matrix 'b'.
Step 26: ENDIF.
Step 27: In printMatrix method, USE nested loops to DISPLAY each element of the given matrix.
STOP
Variable Description
Data Type Variable Name Description
int m Stores the size (order) of the square matrix.
int[][] a Stores the elements of the original matrix.
int[][] b Stores the elements of the mirror image matrix.
int r Loop control variable for rows.
int c Loop control variable for columns.
ISC13Q2 obj Object of the class ISC13Q2.
Scanner sc Scanner object to take input.
int size Stores the input size read in the main method.

Input/Output
Test 1
INPUT:
M=3
4 16 12
8 2 14
4 1 3
OUTPUT:
ORIGINAL MATRIX
4 16 12
8 2 14
4 1 3
MIRROR IMAGE MATRIX
12 16 4
14 2 8
3 1 4
Test 2
INPUT:
M = 22
OUTPUT:
SIZE OUT OF RANGE
Question
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:


(a) Display the count of palindromic words in the sentence.
(b) Display the Palindromic words in the sentence.

Example 1
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
Program
import java.util.*;
import java.io.*;

class ISC13Q3 {
String s, out, s1, s2;
int c, l;

ISC13Q3(String a) {
s = a;
out = "";
s1 = "";
s2 = "";
c = 0;
l = 0;
}

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


Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String a = sc.nextLine();
System.out.println("INPUT: " + a);
ISC13Q3 obj = new ISC13Q3(a);
obj.check();
obj.process();
obj.display();
}

void check() {
s = s.trim();
l = s.length();
if (l == 0) {
System.out.println("INVALID INPUT");
System.exit(0);
}
char ch = s.charAt(l - 1);
if (ch != '.' && ch != '?' && ch != '!') {
System.out.println("INVALID INPUT");
System.exit(0);
}
}

void process() {
s = s.toUpperCase();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch == ' ' || ch == '.' || ch == '?' || ch == '!') {
if (s1.equals(s2) && s1.length() > 0) {
c++;
out += s1 + " ";
}
s1 = "";
s2 = "";
} else {
s1 += ch;
s2 = ch + s2;
}
}
}
void display() {
System.out.println("OUTPUT:");
if (c > 0) {
System.out.println(out.trim());
System.out.println("NUMBER OF PALINDROMIC WORDS: " + c);
} else {
System.out.println("NO PALINDROMIC WORDS");
}
}
}Algorithm

START
Step 1 : In main method, READ a sentence as a string.
Step 2 : CREATE an object of class ISC13Q3, passing the sentence to the constructor.
Step 3 : The constructor INITIALIZES the class variable 's' with the input sentence.
Step 4 : CALL the processAndDisplay method.
Step 5 : In processAndDisplay, CONVERT sentence 's' to upper case.
Step 6 : GET the last character of the sentence.
Step 7 : CHECK IF the last character is not '.', '?', or '!'.
Step 8 : IF true, THEN DISPLAY 'INVALID INPUT' and STOP processing.
Step 9 : INITIALIZE palindrome count 'c' to 0.
Step 10: INITIALIZE empty strings for current word (s1), reversed word (s2), and found palindromes.
Step 11: START a FOR loop from i = 0 TO length-1 of the sentence.
Step 12: GET the character 'ch' at index 'i'.
Step 13: IF 'ch' is a delimiter (' ', '.', '?', '!'), THEN perform check.
Step 14: IF current word 's1' has content AND 's1' is equal to its reverse 's2', THEN
Step 15: INCREMENT palindrome count 'c'.
Step 16: APPEND 's1' and a space to the palindromes string.
Step 17: ENDIF.
Step 18: RESET 's1' and 's2' to empty.
Step 19: ELSE (if 'ch' is not a delimiter), THEN build the words.
Step 20: APPEND 'ch' to the end of 's1'.
Step 21: PREPEND 'ch' to the beginning of 's2'.
Step 22: ENDIF.
Step 23: ENDFOR.
Step 24: AFTER the loop, CHECK IF count 'c' is greater than 0.
Step 25: IF true, THEN DISPLAY the found palindromes string and the final count 'c'.
Step 26: ELSE, DISPLAY 'NO PALINDROMIC WORDS'.
Step 27: ENDIF.
STOP
Variable Description
Data Type Variable Name Description
String s Stores the user-input sentence.
int c Counts the number of palindromic words found.
int l Stores the length of the input sentence.
int i Loop control variable.
char ch Stores each character of the sentence for processing.
char lastChar Stores the last character of the sentence for validation.
String s1 Stores the current word being built.
String s2 Stores the reverse of the current word.
String palindromes Stores all found palindromic words for final display.
ISC13Q3 obj Object of the class ISC13Q3.
BufferedRe br Object for buffered character input.
ader
String sentence Stores the sentence read in the main method.

Input/Output
Test 1
INPUT:
MOM AND DAD ARE COMING AT NOON.
OUTPUT:
MOM DAD NOON
NUMBER OF PALINDROMIC WORDS: 3
Test 2
INPUT:
NITIN ARORA USES LIRIL SOAP.
OUTPUT:
NITIN ARORA LIRIL
NUMBER OF PALINDROMIC WORDS: 3
Test 3
INPUT:
HOW ARE YOU?
OUTPUT:
NO PALINDROMIC WORDS

You might also like