How to get last digit of a number in java – Java Program to Find the Last Digit of a Number

How to get last digit of a number in java: In the previous article, we have seen Java Program to Find the First Digit of a Number

In this article we will see how to find the last digit of a number using Java programming language.

Java Program to Find the Last Digit of a Number

The digit at the end of a number is called as last digit.

For example:

A number is 7912

So, the last digit of a number is 2.

Let’s see different ways to find the last digit of the number.

Method-1: Java Program to Find the Last Digit of a Number By Using Static Input Value

Approach:

• Declare an integer variable say ‘num‘ and initialize a value.
• Then find the number’s modulo by 10 which is the last digit of number.
• Print the result.

Program:

public class Main
{
public static void main(String[] args)
{
//a number declared
int num=591;

//find last digit of the number
int lastDigit = num % 10;

System.out.println("Lasa Digit: "+ lastDigit);

}
}
Output:

Lasa Digit: 1

Method-2: Java Program to Find the Last Digit of a Number By Using User Input Value

Approach:

• Declare an integer variable say ‘num‘ and take theĀ  value as user input.
• Then find the number’s modulo by 10 which is the last digit of number.
• Print the result.

Program:

import java.util.Scanner;

public class Main
{
public static void main(String[] args)
{
//Scanner class object created
Scanner sc=new Scanner(System.in);
//taking a number input from user
int num=sc.nextInt();

//find last digit of the number
int lastDigit = num % 10;

System.out.println("Lasa Digit: "+ lastDigit);
}
}
Output:

785
Lasa Digit: 5



Method-3: Java Program to Find the First Digit of a Number By Using User Defined Method

Approach:

• Declare an integer variable say ‘num‘ and take theĀ  value as user input.
• Call a user defined method findLast() and pass ‘num‘ as parameter.
• Inside method find the number’s modulo by 10 which is the last digit of number.
• Print the result.

Program:

import java.util.Scanner;

public class Main
{
public static void main(String[] args)
{
//Scanner class object created
Scanner sc=new Scanner(System.in);
//taking a number input from user
int num=sc.nextInt();
//calling a user defined method findLast()
findLast(num);
}

//user defined method findLast() to find last digit of number
public static void findLast(int num)
{
//find last digit of the number
int lastDigit = num % 10;

System.out.println("Lasa Digit: "+ lastDigit);
}
}
Output: