The Programming Project: ISC
Showing posts with label ISC. Show all posts
Showing posts with label ISC. Show all posts

Tuesday, January 6, 2026

ISC Computer Science Theory Paper 2023 Sorting of Words in a String


 




import java.io.*;

public class String2023 {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        SortAlpha obj = new SortAlpha(br);
        obj.acceptsent();
        obj.sort();
        br.close();
    }
}

class SortAlpha {

    private String sent;
    private int n;
    private BufferedReader br;
    private String[] words;

    SortAlpha(BufferedReader br) {
        this.br = br;
        this.sent = "";
        this.n = 0;
    }

    public void sort() {
        words = new String[n];
        int k = 0;
        // extracting the words
        for (int i = 0; i < sent.length(); i++) {
            words[k] = "";
            while (sent.charAt(i) != ' ' && i < sent.length()) {
                words[k] += sent.charAt(i);
                i++;
                if (i == sent.length()) {
                    break;
                }

            }
            if (i < sent.length()) {
                while (sent.charAt(i) == ' ' && i < sent.length()) {
                    i++;
                }
            }
            i -= 1;
            k++;
            if (k == n) {
                break;
            }
        }
        // sorting the array of words
        String temp = "";
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (words[j].compareTo(words[j + 1]) > 0) {
                    temp = words[j];
                    words[j] = words[j + 1];
                    words[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            System.out.print(words[i] + " ");
        }
    }

    // Added 'throws IOException' here to fix the error
    public void acceptsent() throws IOException {
        System.out.println("Enter the sentence in uppercase:");
        sent = br.readLine();

        if (sent == null || sent.trim().isEmpty()) {
            System.out.println("Word count: 0");
            return;
        }

        // Counting words by checking for spaces
        for (int i = 0; i < sent.length() - 1; i++) {
            if (sent.charAt(i) == ' ' && sent.charAt(i + 1) != ' ') {
                n++;
            }
        }

        n += 1;
        System.out.println("Number of words: " + n);
    }
}


USING LIBRARY FUNCTIONS

import java.io.*;
import java.util.Arrays;

public class String2023 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        SortAlpha obj = new SortAlpha(br);
        obj.acceptsent();
        obj.sort();
        br.close();
    }
}

class SortAlpha {
    private String sent;
    private int n;
    private BufferedReader br;
    private String[] words;

    SortAlpha(BufferedReader br) {
        this.br = br;
        this.sent = "";
        this.n = 0;
    }

    public void sort() {
        if (n == 0) return;
       
        // Use split() to extract words
        words = sent.trim().split("\\s+");
       
        // Use Arrays.sort() for sorting
        Arrays.sort(words);
       
        // Print sorted words
        System.out.print("Sorted words: ");
        for (int i = 0; i < n; i++) {
            System.out.print(words[i] + " ");
        }
        System.out.println();
    }

    public void acceptsent() throws IOException {
        System.out.println("Enter the sentence in uppercase:");
        sent = br.readLine();

        if (sent == null || sent.trim().isEmpty()) {
            System.out.println("Word count: 0");
            return;
        }

        // Validate uppercase
        if (!sent.matches("[A-Z\\s]+")) {
            System.out.println("Error: Only uppercase letters and spaces allowed!");
            sent = "";
            n = 0;
            return;
        }

        // Use split() and length to count words
        words = sent.trim().split("\\s+");
        n = words.length;
       
        System.out.println("Number of words: " + n);
    }
}

Monday, December 15, 2025

ISC Computer Science Theory Paper 2023 Program Transpose of a Matrix

 





import java.util.*;

public class Matrix {

    public static void main(String[] args) {

        // Create a single Scanner object for System.in
        Scanner scanner = new Scanner(System.in);

        // Pass the single Scanner object to the Trans constructor
        Trans obj = new Trans(scanner);

        obj.fillarray();
        obj.display();

        // Close the Scanner
        scanner.close();
    }
}

class Trans {

    private int m;
    private int[][] arr;
    private int[][] arrT;
    private Scanner in;

    Trans(Scanner scanner) {
        this.in = scanner;
        System.out.println("Enter the order (size) of the square matrix:");
        this.m = in.nextInt();
        this.arr = new int[m][m];
        this.arrT = new int[m][m];
    }

    public void fillarray() {
        System.out.println("\n--- Entering elements for the " + m + "x" + m + " matrix ---");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
               
                System.out.println("Input element at [" + i + "][" + j + "]:");
                arr[i][j] = in.nextInt();
            }
        }
    }

    public void display() {
        System.out.println("\nORIGINAL MATRIX:");
        printMatrix(arr);

        transpose();
    }

    private void transpose() {
        System.out.println("\nTRANSPOSE OF THE MATRIX:");
        // The core logic for transpose: ArrT[i][j] = Arr[j][i]
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                arrT[i][j] = arr[j][i];
            }
        }
        printMatrix(arrT);
    }

    // Helper method for cleaner printing and better formatting
    private void printMatrix(int[][] matrix) {
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                // Use format for aligned columns, which improves readability
                System.out.format("%5d", matrix[i][j]);
            }
            System.out.println();
        }
    }
}

Sunday, December 14, 2025

ISC Computer Science Theory Paper 2023 Program Dudeney Number

 




import java.util.Scanner;

public class Dudeney {

    public static void main(String[] args) {

        NumDude obj = new NumDude();
        obj.input();
        obj.isDude();

    }

}

class NumDude {

    public void isDude() {
        if (numb == (int) Math.pow(sumDigits(numb), 3)) {
            System.out.println(numb + " is a Dudeney Number");
        }else {
            System.out.println(numb + " is not a Dudeney Number");
        }
    }

    private int sumDigits(int x) {
        if (x > 10) {
            return ((x % 10) + sumDigits(x / 10));
        } else {
            return x;
        }

    }

    public void input() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a positive integer: ");
        numb = scanner.nextInt();
        scanner.close();
    }

    public NumDude() {
        this.numb = 0;
    }
    private int numb;

}

🔢 Dudeney Number Checker

A Dudeney number is a positive integer that is a perfect cube such that the sum of its decimal digits equals the cube root of the number.

🎯 Examples:

  • 1 = 1³ and 1 = 1
  • 512 = 8³ and 5+1+2 = 8
  • 4913 = 17³ and 4+9+1+3 = 17
  • 5832 = 18³ and 5+8+3+2 = 18

Try numbers like: 1, 512, 4913, 5832, 17576, 19683

Sunday, January 8, 2023

Leet Code Roman to Integer

Roman numerals are represented by seven different symbols: IVXLCD and M.

Symbol       Value
I                   1
V                  5
X                 10
L                  50
C                 100
D                 500
M               1000

  For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII,       which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

 Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is   not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making   four. The same principle applies to the number nine, which is written as IX. There are six instances where   subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9. 
  • X can be placed before L (50) and C (100) to make 40 and 90. 
  • C can be placed before D (500) and M (1000) to make 400 and 900.

 Given a roman numeral, convert it to an integer.

 

 Example 1:

Input: s = "III"
Output: 3
Explanation: III = 3.

 Example 2:

Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

 Example 3:

Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

 

 Constraints:

  • 1 <= s.length <= 15
  • s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
  • It is guaranteed that s is a valid roman numeral in the range [1, 3999].
 Explanation:
The logic of the program is explained below with the example of MCMXCIV.
Take the value of each of the letters in the sequence:
1000 = M, 100 = C, 1000 = M, 10 = X, 100 = C, 1 = I, 5 = V
Keed adding the values in a variable. But for the second position onwards we
have check the difference of the value of the current position and the previous
position. If the difference is 4 or 9 or 40 or 90 or 400 or 900 then by the exception
rule we have to adjust the value.
For the example above, as 100-1000 = -900 ( i = 1 and i = 0) which is not under
exception rule we add 1000+100 = 1100
But for the next letter ('M') the value is 1000 ( i = 2 ) and 1000 - 100 = 900
( under exception rule), so instead of adding 1000 to 1100 we have to have add 900
to the value of the first letter ( since CM is forming a single value). For this
we need to subtract the value of the current letter (M = 1000) and the previous
letter ( C = 100 ) from the sum of 1000+100+1000 = 2100 and add 900.
Which gives 2100-1000 - 100 + 900 = 1900.


PYTHON CODE

class Solution(object):

    def romanToInt(self, s):
        temp = 0
        for i in range(len(s)):
            self.output += self.value[self.position(s[i])]
            #print ("BEFORE ADJUSTMENT",self.output)
            if i > 0:
                temp = (self.value[self.position(s[i])] -
                        self.value[self.position(s[i - 1])])
            if temp == 4 or temp == 9 or temp == 40 or temp == 90 or temp == 400 or temp == 900:
                self.output = self.output - self.value[self.position(
                    s[i - 1])] - self.value[self.position(s[i])] + temp
            #print ("AFTER ADJUSTMENT",self.output)
        return self.output

    def position(self, symb):
        counter = 0
        while symb != self.symbol[counter]:
            counter += 1
        return (counter)

    symbol = ['I', 'V', 'X', 'L', 'C', 'D', 'M']
    value = [1, 5, 10, 50, 100, 500, 1000]
    output = 0


obj = Solution()
s = str(input("Enter a valid roman numeral in range(1,3999):"))
print(obj.romanToInt(s))