Two Digits of Year in Java

Two digits of year in java. A java program to extract the year’s last two digits. As an input, you can use any integer. The last two digits of the provided integer are then extracted as output using modulus and division operations. 

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int x, y, i = 0;
String z = "";
Scanner s = new Scanner(System.in);
System.out.print("Enter the year");
x = s.nextInt();
while(i < 2)
{
y = x % 10;
z = y + "" +z;
x = x / 10;
i++;
}
System.out.println("Last two digits"+z);
}
}

 

Output:

Enter the year 1980
Last two digits 80

Pre Increment Vs Post Increment in Java

After utilizing a variable in an expression, the post increment operator is used to increase its value. The value is used inside the expression and then incremented by one in the post increment. Both increase the value, but I does so before the current expression is evaluated, whereas i++ does so after the expression has been evaluated.

While Loop Program in Java

A while loop in Java is a control flow statement that allows code to be performed several times based on a given Boolean condition. The while loop may be viewed as a looping if statement. A “While” Loop is used to iterate over a certain piece of code until a condition is fulfilled. The condition is assessed first in a while loop, and if it returns true, the statements within the while loop are executed. When the condition returns false, the control exits the loop and moves on to the next line following the while loop.