JavaScript Program to Check if Two Numbers have Same Last Digit
Last Updated :
13 Jun, 2024
In this article, we will discuss how to check if two numbers have the same last digit in JavaScript. Checking the last digit of a number is a common requirement in programming tasks, and it can be useful in various scenarios. We will explore an approach using JavaScript to accomplish this task.
Methods to Check Both Numbers have the Same Last Digit
We will explore every approach for checking if the Numbers Have the Same Last Digit, along with understanding their basic implementations.
Approach 1: Using the Modulus Operator (%)
- Take the modulus (%) of both numbers with 10 to extract their last digits.
- Compare the last digits using the ===.
- Return
true
if the last digits are the same, otherwise return false
.
Example: In this example we are following above mentioned approach.
JavaScript
// Define function to check last digit of number
function haveSameLastDigit(num1, num2) {
// Getting last digit using % operator
const lastDigit1 = num1 % 10;
const lastDigit2 = num2 % 10;
// Return if last digits are same or not
return lastDigit1 === lastDigit2;
}
const number1 = 123;
const number2 = 456;
// Calling function with arguements
const result = haveSameLastDigit(number1, number2);
// Giving output
console.log(result);
Approach 2: Converting Numbers to Strings
- Convert both numbers to strings using the
toString()
method. - Get the last character (digit) of each string using the
slice()
method. - Compare the last digits using the ===.
- Return
true
if the last digits are the same, otherwise return false
.
Syntax:
string.slice(startIndex, endIndex);
Example:
JavaScript
// Define function to check if two numbers have the same last digit
function haveSameLastDigit(num1, num2) {
// Getting last digit using "toString" and "slice" methods
const lastDigit1 = num1.toString().slice(-1);
const lastDigit2 = num2.toString().slice(-1);
// Return if last digits are same or not
return lastDigit1 === lastDigit2;
}
// Define two numbers
const number1 = 123;
const number2 = 453;
// Calling function with arguments
const result = haveSameLastDigit(number1, number2);
// Output the result
console.log(result);
Approach 3: Using the Array of Digits
- Create an array of digits for each number using the spread operator.
- Get the last digit of each array using the array index.
- Compare the last digits using the ===.
- Return
true
if the last digits are the same, otherwise return false
.
Syntax:
// Spread operator
[...iterable]
Example:
JavaScript
// Define function to check last digit of number
function haveSameLastDigit(num1, num2) {
// Converting number to string
const digits1 = [...num1.toString()];
const digits2 = [...num2.toString()];
// Getting last character of string
const lastDigit1 = digits1[digits1.length - 1];
const lastDigit2 = digits2[digits2.length - 1];
// Return if last digits are same or not
return lastDigit1 === lastDigit2;
}
const number1 = 123;
const number2 = 453;
// Calling function with arguements
const result = haveSameLastDigit(number1, number2);
// Giving output
console.log(result);
Approach 4: Using the Bitwise AND Operator
- The Bitwise AND operator
&
to perform a bitwise AND operation between the numbers and 1 (num1 & 1
and num2 & 1
). - The result will be 1 if the last bit (last digit) of both numbers is the same, and 0 otherwise.
- Then, we compare the results using the strict equality operator
===
to check if they are equal. - If they are equal, it means the numbers have the same last digit.
Syntax:
firstNum=num&1
JavaScript
function hasSameLastDigit(num1, num2) {
// Get the last bit of num1
const lastDigit1 = num1 & 1;
// Get the last bit of num2
const lastDigit2 = num2 & 1;
return lastDigit1 === lastDigit2;
}
// Example usage:
const number1 = 123;
const number2 = 453;
const result = hasSameLastDigit(number1, number2);
console.log(result); // Output: false
Approach 5: Using Math.floor() Method
This method involves a combination of floor division and multiplication to isolate the last digit of each number. The steps are as follows:
- Divide each number by 10 and take the integer part (floor division) to remove the last digit.
- Multiply the result of the division by 10 to get a number ending in zero.
- Subtract this number from the original number to isolate the last digit.
- Compare the isolated last digits using the strict equality operator (===).
- Return true if the last digits are the same, otherwise return false.
Example:
JavaScript
// Define function to check if two numbers have the same last digit
function haveSameLastDigit(num1, num2) {
// Isolate the last digit using floor division and multiplication
const lastDigit1 = num1 - Math.floor(num1 / 10) * 10;
const lastDigit2 = num2 - Math.floor(num2 / 10) * 10;
// Return if last digits are same or not
return lastDigit1 === lastDigit2;
}
// Define two numbers
const number1 = 123;
const number2 = 453;
// Calling function with arguments
const result = haveSameLastDigit(number1, number2);
// Output the result
console.log(result);
Similar Reads
JavaScript Program to Check if Two Strings are Same or Not In this article, we are going to implement a JavaScript program to check whether two strings are the same or not. If they are the same then we will return true else we will return false.Examples: Input: str1 = Geeks, str2 = GeeksOutput: True. Strings are the SameInput: str1 = Geeks, str2 = GeekOutpu
4 min read
JavaScript Program to Check Whether a Number is Harshad Number A Harshad number (also called Niven number) is a number that is divisible by the sum of its digits. In other words, if you take a number, sum up its digits, and if the original number is divisible by that sum, then it's a Harshad number. For example, 18 is a Harshad number because the sum of its dig
2 min read
JavaScript Program to Check for Palindrome Number We are going to learn about Palindrome Numbers in JavaScript. A palindrome number is a numerical sequence that reads the same forwards and backward, It remains unchanged even when reversed, retaining its original identity. Example: Input : Number = 121Output : PalindromeInput : Number = 1331Output :
4 min read
Java Program to Count Number of Digits in a String The string is a sequence of characters. In java, objects of String are immutable. Immutable means that once an object is created, it's content can't change. Complete traversal in the string is required to find the total number of digits in a string. Examples: Input : string = "GeeksforGeeks password
2 min read
How to Check if All Digits of Given Number are Same in PHP ? Given a number, our task is to check if all the digits of a given number are the same. This can be useful in various scenarios, such as validating user input or processing numerical data. In this article, we will explore different approaches to check if a given number's digits are the same in PHP.Ex
5 min read
Java Program to Check For a Valid Mobile Number In Java, validating mobile numbers is usually done by using Regular Expressions (Regex) that define patterns for matching strings. Mobile number validation can be implemented for both local (Indian) and global phone numbers.Example: Now, let's take a basic example, to check if a mobile number is val
3 min read