Java Program to Extract Last Two Digits of a Given Year

This is a Java Program to Extract Last two Digits of a given Year.

Enter any integer as input. After that we perform modulus and division operations to extract last two digits of the given integer as output.

Here is the source code of the Java Program to Extract Last two Digits of a given Year. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Extract_Digit 
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         int x, y, i = 0;
  7.         String z = "";
  8.         Scanner s = new Scanner(System.in);
  9.         System.out.print("Enter Year:");
  10.         x = s.nextInt();
  11.         while(i < 2)
  12.         {
  13.             y = x % 10;
  14.             z = y + "" +z;
  15.             x = x / 10;
  16.             i++;
  17.         }
  18.         System.out.println("Last two digits:"+z);
  19.     }
  20. }

Output:

$ javac Extract_Digit .java
$ java Extract_Digit
 
Enter Year:2014
Last two digits:14

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.