Java program to extract the last two digits from a given year

Given a year, we have to extract the last two digits from a given year.
Submitted by Nidhi, on March 01, 2022

Problem Solution:

In this program, we will read a year in 4 digits and extract the last two digits from the input year.

Program/Source Code:

The source code to extract the last two digits from a given year is given below. The given program is compiled and executed successfully.

// Java program to extract the last two digits 
// from a given year

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner SC = new Scanner(System.in);

    int year = 0;
    int res = 0;

    System.out.printf("Enter year: ");
    year = SC.nextInt();

    res = year % 100;

    System.out.printf("Result is: %02d", res);
  }
}

Output:

Enter year: 2022
Result is: 22

Explanation:

In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contains a static method main().

The main() method is an entry point for the program. Here, we read a year from the user using Scanner class. Then we extracted the last two digits from the input year and printed the result.

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.