Practical PapersISCClass 12Computer Science

ISC Practical 2027: River Basin Rainfall and Flood Warning – Algorithm and Java Program

Fully solved ISC Computer Science Practical 2027 question: validate a basin code (RB followed by 4 digits), find the digit sum of the basin number and analyse N days of rainfall to decide whether to raise a flood warning.

7 min read
ISC Practical 2027: River Basin Rainfall and Flood Warning – Algorithm and Java Program

This is Question 2 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 Blended concepts: string + number + array. 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 river basin monitoring system assigns each basin a unique code in the format RB followed by a 4-digit numeric identifier (for example, RB4319). Write a program to accept a basin code and rainfall readings for N days, where 5 ≤ N ≤ 20. If the code is not in the correct format, or N is outside the range, display an appropriate error message.

  1. Extract the basin number from the basin code.
  2. Calculate the sum of all digits in the basin number.
  3. Store the rainfall readings and display the highest, lowest and average rainfall.
  4. Count the number of days on which the rainfall exceeds 150 mm.
  5. If the average rainfall is greater than 150 mm, display FLOOD WARNING GENERATED; otherwise display NORMAL CONDITIONS.

CISCE gives 5 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

This question blends a string check, number logic and a one-dimensional array. Each part is simple. The marks come from doing them in the right order and formatting the output exactly.

  • Validate the code before asking for N. Example 4 stops immediately after an invalid code. The code must be exactly 6 characters: "RB" + 4 digits. "rb4319" and "RB43190" are invalid.
  • Extract the number with substring(2), then convert it with Integer.parseInt() to find the digit sum using % 10 and / 10.
  • One pass over the array can find the maximum, minimum, sum and the count of readings above 150.
  • Two decimal places: the average is printed as 161.67 and 140.00, so use String.format("%.2f", avg).
  • "Greater than 150" is strict. An average of exactly 150 means NORMAL CONDITIONS.
Bar chart of rainfall readings with the 150 mm threshold and the average line
Question 5: 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 the basin code. If its length is not 6, it does not start with "RB", or characters 3 to 6 are not all digits, print "Invalid Basin Code" and stop.
  3. Input N. If N < 5 or N > 20, print "Invalid Number of Days" and stop.
  4. Input N readings into rain[].
  5. Set basinNo = integer value of code.substring(2). Find digitSum by repeatedly adding basinNo % 10 and dividing basinNo by 10.
  6. Set max = min = rain[0], sum = 0, heavy = 0. For each reading: update max and min, add it to sum, and if it is greater than 150 increase heavy.
  7. avg = sum / N (real division).
  8. Print the basin number, digit sum, max, min, avg to 2 decimal places and heavy.
  9. If avg > 150, print "FLOOD WARNING GENERATED", otherwise print "NORMAL CONDITIONS".
  10. 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.

RiverBasin.java
import java.util.Scanner;

/**
 * ISC Practical 2027 - Sample Paper 2, Question 2
 * Validates a river basin code (RB + 4 digits) and analyses rainfall
 * readings for N days (5 to 20).
 */
public class RiverBasin
{
    String code;         // basin code, for example RB4319
    int rain[];          // rainfall readings in mm
    int n;               // number of days

    RiverBasin(String c)
    {
        code = c.trim();
    }

    // "RB" followed by exactly 4 digits
    boolean isValidCode()
    {
        if (code.length() != 6 || !code.startsWith("RB"))
            return false;
        for (int i = 2; i < 6; i++)
            if (!Character.isDigit(code.charAt(i)))
                return false;
        return true;
    }

    int digitSum(int num)
    {
        int s = 0;
        while (num > 0)
        {
            s += num % 10;
            num /= 10;
        }
        return s;
    }

    void acceptReadings(Scanner sc, int days)
    {
        n = days;
        rain = new int[n];
        System.out.println("Enter " + n + " rainfall readings:");
        for (int i = 0; i < n; i++)
            rain[i] = sc.nextInt();
    }

    void analyse()
    {
        int basinNo = Integer.parseInt(code.substring(2));     // numeric part of the code
        int max = rain[0], min = rain[0], sum = 0, heavy = 0;
        for (int i = 0; i < n; i++)
        {
            if (rain[i] > max) max = rain[i];
            if (rain[i] < min) min = rain[i];
            sum += rain[i];
            if (rain[i] > 150) heavy++;
        }
        double avg = (double) sum / n;

        System.out.println("Basin Number: " + code.substring(2));
        System.out.println("Digit Sum: " + digitSum(basinNo));
        System.out.println("Highest Rainfall: " + max);
        System.out.println("Lowest Rainfall: " + min);
        System.out.println("Average Rainfall: " + String.format("%.2f", avg));
        System.out.println("Days exceeding 150 mm: " + heavy);
        if (avg > 150)
            System.out.println("FLOOD WARNING GENERATED");
        else
            System.out.println("NORMAL CONDITIONS");
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Basin Code: ");
        RiverBasin ob = new RiverBasin(sc.next());
        if (!ob.isValidCode())
        {
            System.out.println("Invalid Basin Code");
            return;
        }
        System.out.print("N: ");
        int days = sc.nextInt();
        if (days < 5 || days > 20)
        {
            System.out.println("Invalid Number of Days");
            return;
        }
        ob.acceptReadings(sc, days);
        ob.analyse();
    }
}

Variable description

Variable description
Name           Type       Purpose
-------------  ---------  -------
code           String     basin code, for example RB4319
rain[]         int[]      rainfall readings in mm
n              int        number of days
basinNo        int        numeric part of the code
max, min       int        highest and lowest rainfall
sum            int        total rainfall
heavy          int        days with rainfall above 150 mm
avg            double     average rainfall

Output [3 marks]

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

Example 1
INPUT:
Basin Code: RB4319
N: 6
180 145 170 155 190 130

OUTPUT:
Basin Number: 4319
Digit Sum: 17
Highest Rainfall: 190
Lowest Rainfall: 130
Average Rainfall: 161.67
Days exceeding 150 mm: 4
FLOOD WARNING GENERATED
Example 2
INPUT:
Basin Code: RB5824
N: 5
120 135 140 160 145

OUTPUT:
Basin Number: 5824
Digit Sum: 19
Highest Rainfall: 160
Lowest Rainfall: 120
Average Rainfall: 140.00
Days exceeding 150 mm: 1
NORMAL CONDITIONS
Example 3
INPUT:
Basin Code: RB7608
N: 7
210 185 160 175 220 195 180

OUTPUT:
Basin Number: 7608
Digit Sum: 21
Highest Rainfall: 220
Lowest Rainfall: 160
Average Rainfall: 189.29
Days exceeding 150 mm: 7
FLOOD WARNING GENERATED
Example 4
INPUT:
Basin Code: R4319

OUTPUT:
Invalid Basin Code
Example 5
INPUT:
Basin Code: RB4319
N: 3

OUTPUT:
Invalid Number of Days

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:

Average exactly 150 (not greater)
INPUT:
Basin Code: RB0150
N: 5
150 150 150 150 150

OUTPUT:
Basin Number: 0150
Digit Sum: 6
Highest Rainfall: 150
Lowest Rainfall: 150
Average Rainfall: 150.00
Days exceeding 150 mm: 0
NORMAL CONDITIONS
Lowercase prefix
INPUT:
Basin Code: rb4319

OUTPUT:
Invalid Basin Code
Five digits after RB
INPUT:
Basin Code: RB43190

OUTPUT:
Invalid Basin Code

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
  • Print the basin number as the substring ("0150"), not the parsed int (150), so leading zeros are not lost.
  • Use the exact messages from the question ("Invalid Basin Code", "Invalid Number of Days"). They are part of the expected output.
  • Show a boundary test with an average of exactly 150. It proves you used > and not >=.
  • 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

  • Printing the average without formatting (161.66666666666666).
  • Initialising max to 0 and min to 0. min would then never change. Start both with rain[0].
  • Accepting "RB43" or "RB12345" because only startsWith("RB") was checked.

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.