Practical PapersISCClass 12Computer Science

ISC Practical 2027: Digital Lock Security Code Analyzer – Algorithm and Java Program

Fully solved ISC Computer Science Practical 2027 number-logic question: for a 5 to 10 digit security code, find the first and last digits, odd and even position sums, product of non-zero digits, digit frequency and classify the code as STRONG, MODERATE or WEAK.

7 min read
ISC Practical 2027: Digital Lock Security Code Analyzer – Algorithm and Java Program

This is Question 3 of Sample Paper 2 from the specimen questions CISCE has released for the ISC Year 2027 Computer Science Practical (Paper 2). The concept area is Number logic. Below you will find the question, a plain-language explanation of the logic, an exam-ready algorithm, a fully documented Java program, the real output for every sample input, and tips for scoring the full 15 marks.

Every program on this page was compiled with JDK 21 and run on CISCE’s sample inputs, plus extra boundary inputs of the kind a visiting examiner will try.

The question

A digital lock generates a security code consisting of a positive integer with 5 to 10 digits. Write a program to accept the code and display INVALID INPUT if it has fewer than 5 or more than 10 digits. Otherwise:

  1. Extract and display the first digit and the last digit.
  2. Calculate the sum of the digits at odd positions, the sum of the digits at even positions, and the product of all non-zero digits.
  3. Display the frequency of each digit (0–9) that occurs, and the most frequently occurring digit. If there is a tie, display the smallest digit.
  4. Classify the code: STRONG CODE if all digits are distinct, MODERATE CODE if only one digit is repeated, and WEAK CODE if two or more digits are repeated.

CISCE gives 4 examples with this question. Each one is shown with its output in the Output section below, so you can check your own program against it.

Understanding the problem

A number logic question about extracting digits. The sample outputs show that positions are counted from the left, starting at 1: for 5834271, the odd positions are 5, 3, 2, 1 (sum 11).

  • Use long. A 10-digit code (up to 9999999999) does not fit in an int.
  • Store the digits left to right in an array. Converting with Long.toString() and charAt(i) - '0' is the simplest way to get them in reading order.
  • A frequency array freq[10] answers three questions at once: the frequency table, the most frequent digit (scan 0 to 9 with a strict >, so the smallest digit wins a tie) and how many digits are repeated (freq > 1).
  • Product of non-zero digits can exceed the int range: ten 9s give 9^10 = 3,486,784,401. Keep the product in a long.
Digits of 75523755 with odd and even positions coloured, and a frequency bar chart
Question 6: worked example

Algorithm [3 marks]

Write the algorithm in the planning session, in numbered steps like these. It must match your program exactly.

  1. Start.
  2. Input code as a long. If code ≤ 0 or its number of digits is less than 5 or more than 10, print "INVALID INPUT" and stop.
  3. Convert code to a String s and store each digit in dig[], left to right. Let len = number of digits.
  4. Set oddSum = evenSum = 0, product = 1 and freq[0..9] = 0.
  5. For i = 0 to len − 1: if (i + 1) is odd add dig[i] to oddSum, else add it to evenSum. If dig[i] ≠ 0, multiply product by dig[i]. Increase freq[dig[i]].
  6. Print dig[0], dig[len − 1], oddSum, evenSum and product.
  7. For d = 0 to 9: if freq[d] > 0 print d and freq[d]. Track most = the digit with the largest frequency (strictly larger replaces it). Count repeated = number of digits with freq > 1.
  8. Print most. If repeated == 0 print "STRONG CODE", if repeated == 1 print "MODERATE CODE", otherwise print "WEAK CODE".
  9. Stop.

Java program [7 marks]

The program uses a class with separate methods for each task and comments that explain every data member and the special logic. That is what the 2 documentation marks are for.

SecurityCode.java
import java.util.Scanner;

/**
 * ISC Practical 2027 - Sample Paper 2, Question 3
 * Analyses the digits of a 5 to 10 digit security code of a digital lock.
 */
public class SecurityCode
{
    long code;           // the security code (long, because 10 digits exceed the int range)
    int dig[];           // digits from left to right
    int len;             // number of digits

    SecurityCode(long c)
    {
        code = c;
    }

    // stores the digits in dig[] from left to right
    void extractDigits()
    {
        String s = Long.toString(code);
        len = s.length();
        dig = new int[len];
        for (int i = 0; i < len; i++)
            dig[i] = s.charAt(i) - '0';
    }

    void analyse()
    {
        if (code <= 0 || Long.toString(code).length() < 5 || Long.toString(code).length() > 10)
        {
            System.out.println("INVALID INPUT");
            return;
        }
        extractDigits();
        int oddSum = 0, evenSum = 0;
        long product = 1;
        int freq[] = new int[10];               // freq[d] = how many times digit d occurs
        for (int i = 0; i < len; i++)
        {
            if ((i + 1) % 2 == 1)               // positions are counted from 1, left to right
                oddSum += dig[i];
            else
                evenSum += dig[i];
            if (dig[i] != 0)
                product *= dig[i];
            freq[dig[i]]++;
        }

        System.out.println("FIRST DIGIT: " + dig[0]);
        System.out.println("LAST DIGIT: " + dig[len - 1]);
        System.out.println("SUM OF ODD POSITION DIGITS: " + oddSum);
        System.out.println("SUM OF EVEN POSITION DIGITS: " + evenSum);
        System.out.println("PRODUCT OF NON-ZERO DIGITS: " + product);
        System.out.println("DIGIT FREQUENCY:");
        int most = 0, repeated = 0;
        for (int d = 0; d <= 9; d++)
        {
            if (freq[d] > 0)
                System.out.println("    " + d + ": " + freq[d]);
            if (freq[d] > freq[most])           // strict > keeps the smallest digit on a tie
                most = d;
            if (freq[d] > 1)
                repeated++;
        }
        System.out.println("MOST FREQUENT DIGIT: " + most);
        String type;
        if (repeated == 0)
            type = "STRONG CODE";
        else if (repeated == 1)
            type = "MODERATE CODE";
        else
            type = "WEAK CODE";
        System.out.println("CLASSIFICATION: " + type);
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the security code: ");
        SecurityCode ob = new SecurityCode(sc.nextLong());
        ob.analyse();
    }
}

Variable description

Variable description
Name             Type       Purpose
---------------  ---------  -------
code             long       security code entered
dig[]            int[]      digits from left to right
len              int        number of digits
oddSum, evenSum  int        sums of odd and even position digits
product          long       product of non-zero digits
freq[]           int[]      freq[d] = occurrences of digit d
most             int        most frequent digit
repeated         int        how many digits occur more than once

Output [3 marks]

These are the actual outputs of the program above for every example in the question:

Example 1
INPUT:
5834271

OUTPUT:
FIRST DIGIT: 5
LAST DIGIT: 1
SUM OF ODD POSITION DIGITS: 11
SUM OF EVEN POSITION DIGITS: 19
PRODUCT OF NON-ZERO DIGITS: 6720
DIGIT FREQUENCY:
    1: 1
    2: 1
    3: 1
    4: 1
    5: 1
    7: 1
    8: 1
MOST FREQUENT DIGIT: 1
CLASSIFICATION: STRONG CODE
Example 2
INPUT:
123455

OUTPUT:
FIRST DIGIT: 1
LAST DIGIT: 5
SUM OF ODD POSITION DIGITS: 9
SUM OF EVEN POSITION DIGITS: 11
PRODUCT OF NON-ZERO DIGITS: 600
DIGIT FREQUENCY:
    1: 1
    2: 1
    3: 1
    4: 1
    5: 2
MOST FREQUENT DIGIT: 5
CLASSIFICATION: MODERATE CODE
Example 3
INPUT:
75523755

OUTPUT:
FIRST DIGIT: 7
LAST DIGIT: 5
SUM OF ODD POSITION DIGITS: 20
SUM OF EVEN POSITION DIGITS: 19
PRODUCT OF NON-ZERO DIGITS: 183750
DIGIT FREQUENCY:
    2: 1
    3: 1
    5: 4
    7: 2
MOST FREQUENT DIGIT: 5
CLASSIFICATION: WEAK CODE
Example 4
INPUT:
542

OUTPUT:
INVALID INPUT

Test it on unknown inputs

One of the 3 output marks is for correct output on inputs the examiner chooses, usually boundary cases. Our program handles these correctly:

10 digits with a zero
INPUT:
9876543210

OUTPUT:
FIRST DIGIT: 9
LAST DIGIT: 0
SUM OF ODD POSITION DIGITS: 25
SUM OF EVEN POSITION DIGITS: 20
PRODUCT OF NON-ZERO DIGITS: 362880
DIGIT FREQUENCY:
    0: 1
    1: 1
    2: 1
    3: 1
    4: 1
    5: 1
    6: 1
    7: 1
    8: 1
    9: 1
MOST FREQUENT DIGIT: 0
CLASSIFICATION: STRONG CODE
Tie for the most frequent digit
INPUT:
9988776655

OUTPUT:
FIRST DIGIT: 9
LAST DIGIT: 5
SUM OF ODD POSITION DIGITS: 35
SUM OF EVEN POSITION DIGITS: 35
PRODUCT OF NON-ZERO DIGITS: 228614400
DIGIT FREQUENCY:
    5: 2
    6: 2
    7: 2
    8: 2
    9: 2
MOST FREQUENT DIGIT: 5
CLASSIFICATION: WEAK CODE
11 digits
INPUT:
12345678901

OUTPUT:
INVALID INPUT

How to score full marks on this question

Marks distribution for the ISC Computer Science practical: algorithm 3, program 7, hard copy 2, output 3
ISC Paper 2: how the 15 exam marks are divided
  • Write in your algorithm that positions are counted from the left starting at 1. Examiners check that your logic matches the sample output.
  • Print only digits that occur in the frequency table, as the sample output does.
  • Try 9988776655. It tests the tie rule (answer 5) and the WEAK classification together.
  • Keep the typed program identical to the planned one. Large differences between your planning sheet and the printout cost the 2 hard-copy marks.

Common mistakes to avoid

  • Extracting digits with % 10, which gives them right to left, and then counting positions from the wrong end.
  • Using >= when finding the most frequent digit, which picks the largest digit in a tie instead of the smallest.
  • Multiplying by 0 when the code contains a zero (the question says non-zero digits).

This post is part of our series that solves all 12 specimen questions for the ISC 2027 Computer Science practical, one question a day. Practise writing the algorithm and program on paper in 90 minutes before you type it. That is exactly how the exam works.