ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.