0% found this document useful (0 votes)
5 views16 pages

ISC12

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)
5 views16 pages

ISC12

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

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 >= 100 and n <= 3000. Display the
number of prime palindrome integers in the specified range along with their values in the format
specified below:
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:
INPUT:
M = 100
N = 5000
OUTPUT:
OUT OF RANGE
Program
import java.util.*;
class ISC12Q1 {
int m, n;

ISC12Q1(int mm, int nn) {


m = mm;
n = nn;
}

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);
System.out.print("INPUT:\tM = ");
int x = sc.nextInt();
System.out.print("\tN = ");
int y = sc.nextInt();
ISC12Q1 obj = new ISC12Q1(x, y);
obj.check();
sc.close();
}

void check() {
if (m < 100 || n > 3000 || m > n) {
System.out.println("OUTPUT: OUT OF RANGE");
System.exit(0);
}
process();
}

void process() {
String s = "";
int c = 0;
for (int i = m; i <= n; i++) {
if (prime(i) && pali(i)) {
if (c > 0) {
s += ", ";
}
s += i;
c++;
}
}
display(s, c);
}

boolean prime(int k) {
int d = 0;
for (int i = 1; i <= k; i++) {
if (k % i == 0) {
d++;
}
}
return d == 2;
}

boolean pali(int k) {
int r = 0, t = k;
while (t > 0) {
r = r * 10 + t % 10;
t /= 10;
}
return k == r;
}

void display(String s, int c) {


System.out.println("OUTPUT: THE PRIME PALINDROME INTEGERS ARE:");
System.out.println(s);
System.out.println("FREQUENCY OF PRIME PALINDROME INTEGERS: " + c);
}
}
Algorithm
Step 1: START
Step 2: DECLARE integer variables m, n.
Step 3: READ m and n from the user.
Step 4: IF m > 3000 OR n > 3000 OR m < 100 OR m > n THEN
Step 5: DISPLAY 'OUTPUT: OUT OF RANGE'.
Step 6: GOTO Step 20.
Step 7: ENDIF.
Step 8: DECLARE integer c = 0 and string result = "".
Step 9: DISPLAY 'OUTPUT: THE PRIME PALINDROME INTEGERS ARE:'.
Step 10: FOR w from m TO n.
Step 11: SET t = w, s = 0.
Step 12: WHILE t > 0 DO.
Step 13: SET d = t % 10.
Step 14: SET s = s * 10 + d.
Step 15: SET t = t / 10.
Step 16: ENDWHILE.
Step 17: IF w is equal to s THEN.
Step 18: SET prime_flag = 1, factor_count = 0.
Step 19: FOR a from 1 TO w.
Step 20: IF w % a == 0 THEN.
Step 21: INCREMENT factor_count.
Step 22: ENDIF.
Step 23: ENDFOR.
Step 24: IF factor_count is equal to 2 THEN.
Step 25: IF c > 0 THEN.
Step 26: APPEND ", " to result.
Step 27: ENDIF.
Step 28: APPEND w to result.
Step 29: INCREMENT c.
Step 30: ENDIF.
Step 31: ENDIF.
Step 32: ENDFOR.
Step 33: DISPLAY result.
Step 34: DISPLAY 'FREQUENCY OF PRIME PALINDROME INTEGERS: ' + c.
Step 35: STOP
Variable Description
Data Type Variable Name Description
int m To store the lower bound of the range.
int n To store the upper bound of the range.
int k Formal parameter in isPrime() and isPalindrome() to hold the number.
int c To count factors in isPrime() or to count prime palindromes in process().
int a Loop control variable in isPrime().
int t To store a temporary copy of the number in isPalindrome().
int d To store a digit of the number.
int s To store the reversed number.
int w Loop control variable to iterate from m to n.
String result To store the string of prime palindrome numbers.
Scanner sc To create a scanner object for input.
ISC12Q1 obj Object of the class ISC12Q1.

Input/Output
Test 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

Test 2:
INPUT:
M = 300
N = 2500
OUTPUT:
THE PRIME PALINDROME INTEGERS ARE:
313, 353, 373, 383, 727, 757, 787, 797, 919, 929
FREQUENCY OF PRIME PALINDROME INTEGERS: 10

Test 3:
INPUT:
M = 100
N = 5000
OUTPUT:
OUT OF RANGE
Question
Write a program to accept a sentence as input. The words in the string are to be separated by a
blank. Each word must be in upper case.
The sentence is terminated by either '.', '!' or '?'.
Perform the following tasks:
(i) Obtain the length of the sentence (measured in words).
(ii) Arrange the sentence in alphabetical order of the words.

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
Program
import java.util.*;
class ISC12Q2 {
String s;
String w[];
int l;

ISC12Q2(String st) {
s = st;
}

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);
System.out.println("INPUT:");
String str = sc.nextLine();
ISC12Q2 obj = new ISC12Q2(str);
obj.check();
obj.process();
obj.display();
}

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

void process() {
s = s.toUpperCase();
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ' || s.charAt(i) == '.' || s.charAt(i) == '?' || s.charAt(i) == '!') {
count++;
}
}
w = new String[count];
int idx = 0;
String temp = "";
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch != ' ' && ch != '.' && ch != '?' && ch != '!') {
temp += ch;
} else {
if (!temp.equals("")) {
w[idx++] = temp;
temp = "";
}
}
}
l = idx;
sortWords();
}

void sortWords() {
for (int a = 0; a < l - 1; a++) {
for (int b = 0; b < l - 1 - a; b++) {
if (w[b].compareTo(w[b + 1]) > 0) {
String t = w[b];
w[b] = w[b + 1];
w[b + 1] = t;
}
}
}
}

void display() {
System.out.println("OUTPUT:");
System.out.println("LENGTH: " + l);
System.out.println("REARRANGED SENTENCE:");
for (int i = 0; i < l; i++) {
System.out.print(w[i] + " ");
}
System.out.println();
}
}
Algorithm
Step 1: START
Step 2: DECLARE a string s.
Step 3: READ the sentence s from user.
Step 4: GET the last character of s and store it in lch.
Step 5: IF lch is not '.', '?' or '!' THEN
Step 6: DISPLAY 'INVALID INPUT'.
Step 7: GOTO Step 22.
Step 8: ENDIF.
Step 9: CONVERT s to uppercase.
Step 10: SPLIT s by spaces or punctuation marks into a string array w.
Step 11: GET the length of array w and store in l.
Step 12: CALL sortWords procedure.
Step 13: FOR a from 0 to l-2.
Step 14: FOR b from 0 to l-2-a.
Step 15: IF word at w[b] is alphabetically greater than word at w[b+1] THEN
Step 16: SWAP w[b] and w[b+1].
Step 17: ENDIF.
Step 18: ENDFOR.
Step 19: ENDFOR.
Step 20: CALL display procedure.
Step 21: DISPLAY 'LENGTH: ' + l.
Step 22: DISPLAY 'REARRANGED SENTENCE:'.
Step 23: FOR a from 0 to l-1.
Step 24: DISPLAY w[a] + ' '.
Step 25: ENDFOR.
Step 26: STOP
Variable Description
Data Type Variable Name Description
String s To store the input sentence.
String[] w To store the words of the sentence.
int l To store the number of words.
char lch To store the last character of the sentence.
String t Temporary variable for swapping strings.
int a Outer loop control variable.
int b Inner loop control variable.
Scanner sc To create a scanner object for input.
ISC12Q2 obj Object of the class ISC12Q2.

Input/Output
Test 1:
INPUT:
NECESSITY IS THE MOTHER OF INVENTION.
OUTPUT:
LENGTH: 6
REARRANGED SENTENCE:
INVENTION IS MOTHER NECESSITY OF THE
Test 2:
INPUT:
BE GOOD TO OTHERS.
OUTPUT:
LENGTH: 4
REARRANGED SENTENCE:
BE GOOD OTHERS TO
Test 3:
INPUT:
ARE YOU A STUDENT,
OUTPUT:
INVALID INPUT
Question 3
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 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.
Example 1:
INPUT:
M=3
N=4
Entered values: 8, 7, 9, 3
-2, 0, 4, 5,
1, 3, 6, -4
OUTPUT:
Original matrix:
8 7 9 3
-2 0 4 5
1 3 6 -4
Largest Number: 9
Row: 0
Column: 2
Smallest Number: -4
Row: 2
Column: 3
Rearranged matrix:
-4 -2 0 1
3 3 4 5
6 7 8 9
Program
import java.util.*;
class ISC12Q2 {
String s;
String w[];
int l;

ISC12Q2(String s) {
this.s = s;
}

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);
System.out.println("INPUT:");
String str = sc.nextLine();
ISC12Q2 obj = new ISC12Q2(str);
obj.check();
obj.process();
obj.display();
}

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

void process() {
s = s.toUpperCase();
int c = 0;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch == ' ' || ch == '.' || ch == '?' || ch == '!') {
c++;
}
}
w = new String[c];
int k = 0;
String temp = "";
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch != ' ' && ch != '.' && ch != '?' && ch != '!') {
temp += ch;
} else if (temp.length() > 0) {
w[k] = temp;
k++;
temp = "";
}
}
l = k;
sortWords();
}

void sortWords() {
for (int a = 0; a < l - 1; a++) {
for (int b = 0; b < l - 1 - a; b++) {
if (w[b].compareTo(w[b + 1]) > 0) {
String t = w[b];
w[b] = w[b + 1];
w[b + 1] = t;
}
}
}
}

void display() {
System.out.println("OUTPUT:");
System.out.println("LENGTH: " + l);
System.out.println("REARRANGED SENTENCE:");
for (int a = 0; a < l; a++) {
System.out.print(w[a] + " ");
}
System.out.println();
}
}
Algorithm
Step 1: START
Step 2: READ matrix dimensions M and N.
Step 3: IF M <= 2 OR N <= 2 OR M >= 20 OR N >= 20 THEN
Step 4: DISPLAY 'OUTPUT: SIZE OUT OF RANGE'.
Step 5: GOTO Step 31.
Step 6: ENDIF.
Step 7: CREATE a 2D integer array a of size M x N.
Step 8: DISPLAY 'ENTER M * N MATRIX ELEMENTS'.
Step 9: FOR r from 0 to M-1.
Step 10: FOR c from 0 to N-1.
Step 11: READ element and store in a[r][c].
Step 12: ENDFOR.
Step 13: ENDFOR.
Step 14: DISPLAY 'ORIGINAL MATRIX'.
Step 15: CALL display procedure for matrix a.
Step 16: INITIALIZE max = a[0][0], min = a[0][0], and their row/column indices.
Step 17: FOR r from 0 to M-1.
Step 18: FOR c from 0 to N-1.
Step 19: IF a[r][c] > max THEN update max and its indices.
Step 20: IF a[r][c] < min THEN update min and its indices.
Step 21: ENDFOR.
Step 22: ENDFOR.
Step 23: DISPLAY max, min, and their positions.
Step 24: CREATE a 1D integer array b of size M*N.
Step 25: COPY elements of a into b.
Step 26: SORT array b in ascending order (using bubble sort).
Step 27: COPY elements from sorted array b back into matrix a.
Step 28: DISPLAY 'REARRANGED MATRIX'.
Step 29: CALL display procedure for matrix a.
Step 30: STOP
Variable Description
Data Type Variable Name Description
int m To store the number of rows.
int n To store the number of columns.
int[][] a To store the integer elements of the matrix.
int[] b A 1D array to facilitate sorting of matrix elements.
int r Loop control variable for rows.
int c Loop control variable for columns.
int w Index variable for the 1D array b.
int t Temporary variable for swapping.
int max/min To store the largest/smallest number.
int maxR/maxC To store the row/column index of the largest number.
int minR/minC To store the row/column index of the smallest number.
BufferedRe br To create a buffered reader object for input.
ader
ISC12Q3 obj Object of the class ISC12Q3.

Input/Output
Test 1:
INPUT:
M=3
N=4
8793
-2 0 4 5
1 3 6 -4
OUTPUT:
ORIGINAL MATRIX
8 7 9 3
-2 0 4 5
1 3 6 -4
Largest Number: 9
Row: 0
Column: 2
Smallest Number: -4
Row: 2
Column: 3
REARRANGED MATRIX
-4 -2 0 1
3 3 4 5
6 7 8 9
Test 2:
INPUT:
M=4
N=3
269
5 4 15
2 8 -7
2 -5 14
OUTPUT:
ORIGINAL MATRIX
2 6 9
5 4 15
2 8 -7
2 -5 14
Largest Number: 15
Row: 1
Column: 2
Smallest Number: -7
Row: 2
Column: 2
REARRANGED MATRIX
-7 -5 2 2
2 4 5 6
8 9 14 15

You might also like