Page 55 - CTS - CSA TP - Volume 2
P. 55

COMPUTER SOFTWARE APPLICATION - CITS


































           TASK 4: Check if a year is a century year

           import java.util.Scanner;
           public class CenturyYearCheck {
               public static void main(String[] args) {
                   // Create a Scanner object to read input from the console

                   Scanner scanner = new Scanner(System.in);
                   // Prompt the user to enter a year
                   System.out.print(“Enter a year: “);
                   // Read the integer input provided by the user
                   int year = scanner.nextInt();
                   // Check if the year is divisible by 100 and print the result

                   if (year % 100 == 0) {
                       System.out.println(year + “ is a century year.”);
                   } else {
                       System.out.println(year + “ is not a century year.”);

                   }
                   // Close the scanner object to release system resources
                   scanner.close();
               }
           }

           Explanation:
           1  The program begins by importing the Scanner class from the java.util package to allow user input from the
              console.
           2  The CenturyYearCheck class contains the main method, which serves as the entry point of the program.

           3  Inside the main method:
              •  It creates a new Scanner object named scanner to read input from the console.


                                                           40
                                CITS : IT & ITES - Computer Software Application - Exercise 85
   50   51   52   53   54   55   56   57   58   59   60