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.
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.
Output:
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.
need an explanation for this answer? contact us directly to get an explanation for this answer