Last Digit Of Given Numbers Are Equal
This tutorial shows a snippet to find if the last digit of numbers are equal.
Logic:
- When we find the modulus of 10 of a given number we get the last digit of the number,
e.g. 100 % 10 = 0
. Here,modulus
is represented by%
- If we find the 10 modulus of given number and compare the values with each other we can find if the last digit of numbers are equal or not.
Pseudo-code
number1 = 10
number2 = 150
lastDigitNumber1 = number1 % 10
lastDigitNumber2 = number2 % 10
if( lastDigitNumber1 equals lastDigitNumber2 ) {
print " The last digit of given numbers are equal."
} else {
print "The last digits are not equal"
}
JavaScript Syntax
const number1 = 10;
const number2 = 150;
const lastDigitNumber1 = number1 % 10;
const lastDigitNumber2 = number2 % 10;
if( lastDigitNumber1 === lastDigitNumber2 ) {
console.log(`The last digit of given numbers: ${number1} and ${number2} are equal.`);
} else {
console.log(`The last digit of given numbers: ${number1} and ${number2} are not equal.`);
}
/*
* Output
* prints: The last digit of given numbers: 10 and 150 are equal
*/
Now, the same code wrapped inside a function looks like
// function that checks if the last digit of given numbers are equal
function lastDigitalComparator(number1, number2) {
const lastDigitNumber1 = number1 % 10;
const lastDigitNumber2 = number2 % 10;
if( lastDigitNumber1 === lastDigitNumber2 ) {
console.log(`The last digit of given numbers: ${number1} and ${number2} are equal.`);
}
else {
console.log(`The last digit of given numbers: ${number1} and ${number2} are not equal.`);
}
}
lastDigitalComparator(11, 41);
/*
* Output
* prints: The last digit of given numbers: 11 and 41 are equal
*/
lastDigitalComparator(12, 56);
/*
* Output
* prints: The last digit of given numbers: 12 and 56 are not equal
*/