Dremendo Tag Line

Input number and print its last digit in Java

Input - Question 4

In this question, we will see how to input a number in Java programming using the java input and print its last digit. To know more about java input click on the java input lesson.

Q4) Write a program in Java to input a number and print its last digit.

Number = 245
Last digit = 5

Program

import java.util.Scanner;

public class Q4
{
    public static void main(String args[])
    {
        int n,ld;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a number");
        n=sc.nextInt();
        ld=n%10;
        System.out.print("Last digit=" + ld);
    }
}

Output

Enter a number
483
Last digit=3
video-poster