0% found this document useful (0 votes)
1K views

Isc - Practical - 2024 Solved

The document describes a program to perform operations on matrices. It accepts the dimensions of a matrix from the user and inputs the elements. It displays the original matrix, rotates the matrix by 270 degrees anti-clockwise, and calculates the sum of odd elements, displaying the results. It provides sample code and test cases.
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)
1K views

Isc - Practical - 2024 Solved

The document describes a program to perform operations on matrices. It accepts the dimensions of a matrix from the user and inputs the elements. It displays the original matrix, rotates the matrix by 270 degrees anti-clockwise, and calculates the sum of odd elements, displaying the results. It provides sample code and test cases.
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
You are on page 1/ 10

Question 1

A Vampire number is a composite natural number with an even number of digits that can
be factored into two natural numbers each with half as many digits as the original
number and not both with trailing zeros, where the two factors contain precisely all the
digits of the original number, in any order of counting multiplicity.
Example: 1260 = 21 × 60 (where, 21 and 60 contain precisely all the digits of the number)
Thus, 1260 is a Vampire number.
Accept two positive integers m and n, where m < n and values of both ‘m’ and ‘n’ must be
>= 1000 and <= 9999 as user input. Display all Vampire numbers that are in the range
between m and n (both inclusive) and output them along with the frequency, in the format
specified below:
Test your program for the following data and some random data:
Example 1
INPUT: m = 1002
n = 1640

OUTPUT:
THE VAMPIRE NUMBERS ARE:
1260 1395 1435 1530
FREQUENCY OF VAMPIRE NUMBER IS: 4
Example 2
INPUT:
m = 1810
n = 7800

OUTPUT:
THE VAMPIRE NUMBERS ARE:
1827 2187 6880
FREQUENCY OF VAMPIRE NUMBER IS: 3

Example 3
INPUT:
m = 8105
n= 9999

OUTPUT:
THE VAMPIRE NUMBERS ARE:
NIL
FREQUENCY OF VAMPIRE NUMBER IS: 0

Example 4
INPUT:
m = 174
n = 4500
OUTPUT:
INVALID INPUT

ALGORITHM:

1. **Import Necessary Libraries:**


- Import the `Scanner` class for user input.
2. **Prompt for Input:**
- Use `Scanner` to prompt the user to enter two integers, `m` and `n`.
- Ensure that `m` and `n` are both greater than or equal to 1000 and less than or
equal to 9999. Also, ensure that `m` is less than `n`.

3. **Input Validation:**
- If the input is invalid, display "INVALID INPUT" and exit the program.

4. **Vampire Number Finding:**


- Create a loop that iterates over the range of numbers from `m` to `n` (inclusive).
- For each number in the range:
- Call the `isVampireNumber` method to check if it is a Vampire number.
- If it is a Vampire number, print the number and increment the count.

5. **Display Results:**
- Print "THE VAMPIRE NUMBERS ARE:" followed by the Vampire numbers found.
- Print "FREQUENCY OF VAMPIRE NUMBER IS:" followed by the count of Vampire
numbers.

6. **isVampireNumber Method:**
- Convert the current number to a string to get its length.
- Check if the length of the number is even. If not, return false.
- Iterate through possible pairs of factors from 10 to 99.
- For each pair of factors:
- Check if the number is divisible by the factor.
- If divisible, calculate the other factor and form a string with both factors.
- Convert the number and the factor string to character arrays.
- Check if the digit counts match using the `areEqualDigits` method.
- If they match, return true (Vampire number found).
- If no Vampire number is found, return false.

7. **areEqualDigits Method:**
- Initialize an array `count` to count the occurrences of each digit (0 to 9).
- Iterate through the characters in the first set of digits and increment the
corresponding count.
- Iterate through the characters in the second set of digits and decrement the
corresponding count.
- Check if all counts are zero. If yes, return true (digits match), else return false.

SAMPLE CODE:
import java.util.Scanner;
public class VampireNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input
System.out.print("Enter the value of m: ");
int m = scanner.nextInt();
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
// Validate input
if (m < 1000 || m > 9999 || n < 1000 || n > 9999 || m >= n) {
System.out.println("INVALID INPUT");
return;
}
// Find and display Vampire numbers
System.out.println("THE VAMPIRE NUMBERS ARE:");
int count = 0;
for (int num = m; num <= n; num++) {
if (isVampireNumber(num)) {
System.out.print(num + " ");
count++;
}
}
// Display frequency
System.out.println("\nFREQUENCY OF VAMPIRE NUMBER IS: " + count);
}
// Check if a number is a Vampire number
private static boolean isVampireNumber(int num) {
String strNum = Integer.toString(num);
int len = strNum.length();
// Ensure the number is even-digit
if (len % 2 != 0) {
return false; // Not an even-digit number
}
// Iterate through possible pairs of factors
for (int i = 10; i <= 99; i++) {
if (num % i == 0) {
int factor1 = i;
int factor2 = num / i;
String strFactors = Integer.toString(factor1) + Integer.toString(factor2);
char[] numDigits = strNum.toCharArray();
char[] factorDigits = strFactors.toCharArray();
// Check if the digits match
if (areEqualDigits(numDigits, factorDigits)) {
return true; // Found a Vampire number
}
}
}
return false; // No matching factors found
}
// Helper method to check if two sets of digits are equal
private static boolean areEqualDigits(char[] digits1, char[] digits2) {
int[] count = new int[10];
// Count the occurrences of each digit in the first set
for (char digit : digits1) {
count[digit - '0']++;
}
// Subtract the occurrences of each digit in the second set
for (char digit : digits2) {
count[digit - '0']--;
}
// If all counts are zero, the sets are equal
for (int value : count) {
if (value != 0) {
return false; // Digits do not match
}
}
return true; // Digits match
}
}

OUTPUT:

Question 2
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 10.
Allow the user to input integers into this matrix. Display appropriate error message for an
invalid input.
Perform the following tasks on the matrix.
(a) Display the input matrix
(b) Rotate the matrix by 270° degrees anti clock wise and display the resultant matrix
(c) Calculate the sum of the odd elements of the matrix and display
Test your program for the following data and some random data:
Example 1
INPUT: M=3
N=4
ENTER ELEMENTS: 8, 7, 9, 3,-2, 0, 4, 5, 1, 3, 6,-4
OUTPUT: ORIGINALMATRIX
8 7 0 3
-2 0 4 5
1 3 6 -4
ROTATED MATRIX (270° ANTI CLOCK WISE)
1 -2 8
3 0 7
6 4 0
-4 5 3
SUM OF THE ODD ELEMENTS = 28
Example 2
INPUT: M=3
N=2
ENTER ELEMENTS: 9, 13, 41, 5, 6, -5
OUTPUT: ORIGINALMATRIX
9 13 41
5 6 -5

ROTATED MATRIX (270° ANTI CLOCK WISE)


5 9
6 13
-5 41
SUM OF THE ODD ELEMENTS = 63
Example 3
INPUT: M=2
N=10
OUTPUT: INVALID INPUT
ALGORITHM:

1. **Import Necessary Libraries:**


- Import the `Scanner` class for user input.

2. **Prompt for Matrix Dimensions:**


- Prompt the user to enter the number of rows (`M`) and columns (`N`).
- Validate that both `M` and `N` are greater than 2 and less than 10. If not, display "INVALID
INPUT" and exit.

3. **Input Matrix Elements:**


- Create a 2D array (`matrix`) of size `M x N`.
- Prompt the user to enter the elements of the matrix.
- Store the user input in the matrix.

4. **Display Original Matrix:**


- Print "ORIGINAL MATRIX" followed by the elements of the input matrix using a nested
loop.

5. **Rotate Matrix by 270° Anti-Clockwise:**


- Create a method (`rotateMatrix`) to rotate the input matrix by 270° anti-clockwise.
- Inside the method, create a new matrix (`rotatedMatrix`) of size `N x M`.
- Use nested loops to copy and rotate each element from the input matrix to the rotated
matrix.

6. **Display Rotated Matrix:**


- Print "ROTATED MATRIX (270° ANTI-CLOCKWISE)" followed by the elements of the rotated
matrix using a nested loop.

7. **Calculate Sum of Odd Elements:**


- Create a method (`sumOfOddElements`) to calculate the sum of odd elements in a matrix.
- Iterate through the matrix, and for each odd element, add it to a running sum.

8. **Display Sum of Odd Elements:**


- Print "SUM OF THE ODD ELEMENTS =" followed by the sum calculated in the previous step.

9. **End of Program:**
- End the program.

SAMPLE CODE:
import java.util.Scanner;
import javax.lang.model.util.ElementScanner6;
public class MatrixOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input matrix dimensions
System.out.print("Enter the number of rows (M): ");
int m = scanner.nextInt();
System.out.print("Enter the number of columns (N): ");
int n = scanner.nextInt();
// Validate input
if (m >2 && m < 10 && n > 2 && n < 10)
{
// Input matrix elements
int[][] matrix = new int[m][n];
System.out.println("Enter matrix elements separated by commas:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = scanner.nextInt();
}
}
// Display original matrix
System.out.println("ORIGINAL MATRIX");
displayMatrix(matrix);
// Rotate the matrix by 270° anti-clockwise
int[][] rotatedMatrix = rotateMatrix(matrix);
// Display rotated matrix
System.out.println("ROTATED MATRIX (270° ANTI-CLOCKWISE)");
displayMatrix(rotatedMatrix);
// Calculate and display sum of odd elements
int oddSum = sumOfOddElements(matrix);
System.out.println("SUM OF THE ODD ELEMENTS = " + oddSum);
}
else
{
System.out.println("INVALID INPUT");
return;
}
}
// Display the elements of a matrix
private static void displayMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int element : row) {
System.out.print(element + "\t");
}
System.out.println();
}
}
// Rotate a matrix by 270° anti-clockwise
private static int[][] rotateMatrix(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int[][] rotatedMatrix = new int[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
rotatedMatrix[j][rows - 1 - i] = matrix[i][j];
}
}
return rotatedMatrix;
}
// Calculate the sum of odd elements in a matrix
private static int sumOfOddElements(int[][] matrix) {
int sum = 0;
int r = matrix.length;
int c = matrix[0].length;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (matrix[i][j] % 2 != 0) {
sum += matrix[i][j];
}
}
}
return sum;
}
}
OUTPUT:
Question 3
A snowball string is a sentence where each word is arranged in ascending order of their
length and is also consecutive.
For example “I am the Lord” is a snowball string as
Length of word ‘I’ is 1
Length of word ‘am’ is 2
Length of word ‘the’ is 3
Length of word ‘Lord’ is 4
The length of each word is one more than the previous word. Hence they are consecutive
and in ascending order.
Write a program to enter any sentence and check if it is a snowball string or not. The
words in the sentence may be separated by one or more spaces and terminated by ‘.’ or
‘?’ only. The program will generate appropriate error message for any other terminating
character.
Test your program for the following data and some random data:
Example 1

INPUT:
He may give bonus.
OUTPUT:
IT IS A SNOWBALL STRING

Example 2

INPUT:
Is the cold water frozen?
OUTPUT:
IT IS A SNOWBALL STRING

Example 3

INPUT:
Look before you leap.
OUTPUT:
IT IS NOT A SNOWBALL STRING

Example 4

INPUT:
The child is father of the man!
OUTPUT:
INCORRECT TERMINATING CHARACTER. INVALID INPUT
ALGORITHM:
1. **Import Necessary Libraries:**
- Import the `java.io.*` package for input/output operations.

2. **Initialize BufferedReader:**
- Create a `BufferedReader` object (`br`) to read input from the user.

3. **Input String:**
- Prompt the user to enter a string terminated by a full stop (.) or question mark (?).
- Read the input string (`s`) using the `readLine()` method.

4. **Check Terminating Character:**


- Check if the last character of the string is either a full stop (.) or a question mark (?).
- If not, print an error message, "INCORRECT TERMINATING CHARACTER. INVALID INPUT,"
and exit the program.

5. **Process the String:**


- Get the length of the input string (`l`).
- Find the index of the first space character in the string (`iofs`).
- Extract the first word (`sw`) from the string up to the first space.
- Get the length of the first word (`lsw`).

6. **Initialize Variables and Flags:**


- Initialize variables `i`, `p`, and a boolean flag (`flag`) to true.
- Set `p` to the index of the first space character plus 1.

7. **Iterate through the String:**


- Use a loop to iterate through the string starting from the index after the first space.
- For each character:
- If the character is a space or a full stop, extract the word from the string (`w`) starting
from `p` up to the current index.
- Check if the length of the first word (`lsw`) is greater than the length of the current word
(`w`).
- If true, set the flag to false and break out of the loop.
- Update the pointer `p` to the next position in the string.

8. **Output Result:**
- If the flag is true, print "Snowball string."
- If the flag is false, print "Not a snowball string."

9. **End of Program:**
- End the program.
SAMPLE CODE:
import java.io.*;
class SnowBallStringChecker {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string terminated by a full stop(.) or question mark (?) ");
String s = br.readLine();
// Check for correct terminating character
char lastChar = s.charAt(s.length() - 1);
if (lastChar != '.' && lastChar != '?') {
System.out.println("INCORRECT TERMINATING CHARACTER. INVALID INPUT");
System.exit(0);
} else {
int l = s.length();
int iofs = s.indexOf(' ');
String sw = s.substring(0, iofs);
int lsw = sw.length();
int i, p;
boolean flag = true;
p = iofs + 1;
for (i = iofs + 1; i < l; i++) {
char ch = s.charAt(i);
if (ch == ' ' || ch == '.') {
String w = s.substring(p, i);
if (lsw > w.length()) {
flag = false;
break;
}
p = i + 1;
}
}
// Output result
if (flag)
System.out.println("Snowball string");
else
System.out.println("Not a snowball string");
}
}//eom
}//eoc
OUTPUT:

You might also like