Digital Lock | ISC Computer Science 2027 Practical

A digital lock generates a security code consisting of a positive integer having 5 to 10 digits.

Write a program to accept the security code and perform the following operations.
Display INVALID INPUT if the number contains fewer than 5 digits or more than 10 digits.

  1. Extract and display:
    i. First digit
    ii. Last digit
  2. Calculate:
    i. Sum of digits at even positions
    ii. Sum of digits at odd positions
    iii. Product of all non-zero digits
  3. Determine and display:
    i. Frequency of each digit (0 – 9)
    ii. Most frequently occurring digit
    (If there is a tie, display the smallest digit)
    Classify the code as:
    STRONG CODE → if all digits distinct
    MODERATE CODE → if only one digit repeated
    WEAK CODE → if two or more digits repeated

Test your program with the following data and some random data:

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

import java.util.Scanner;
class DigitalLock{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Security code: ");
        long code = in.nextLong();
        String s = String.valueOf(code);
        int len = s.length();
        if(len < 5 || len > 10){
            System.out.println("INVALID INPUT");
            return;
        }
        char first = s.charAt(0);
        System.out.println("FIRST DIGIT: " + first);
        char last = s.charAt(len - 1);
        System.out.println("LAST DIGIT: " + last);
        int odd = 0;
        int even = 0;
        int prod = 1;
        int a[] = new int[10];
        for(int i = 0; i < len; i++){
            char ch = s.charAt(i);
            String d = String.valueOf(ch);
            int digit = Integer.parseInt(d);
            if(i % 2 == 0)
                odd += digit;
            else
                even += digit;
            if(digit != 0)
                prod *= digit;
            a[digit]++;
        }
        System.out.println("SUM OF ODD POSITION DIGITS: " + odd);
        System.out.println("SUM OF EVEN POSITION DIGITS: " + even);
        System.out.println("PRODUCT OF NON-ZERO DIGITS: " + prod);
        System.out.println("DIGIT FREQUENCY:");
        int freq = 0;
        int repeat = 0;
        for(int i = 0; i < 10; i++){
            if(a[i] > 0){
                System.out.println(i + ": " + a[i]);
                if(a[i] > freq)
                    freq = a[i];
            }
            if(a[i] > 1)
                repeat++;
        }
        int digit = 9;
        for(int i = 0; i < 10; i++){
            if(a[i] == freq && digit > i)
                digit = i;
        }
        System.out.println("MOST FREQUENT DIGIT: " + digit);
        System.out.print("CLASSIFICATION: ");
        if(repeat == 0)
            System.out.println("STRONG CODE");
        else if(repeat == 1)
            System.out.println("MODERATE CODE");
        else
            System.out.println("WEAK CODE");
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *