Algorithm


  1. Read the first number, let's call it num1.
  2. Read the second number, let's call it num2.
  3. Extract the last digit of num1 using the modulo operator % with 10, and store it in a variable, let's call it lastDigit1.
  4. Extract the last digit of num2 using the modulo operator % with 10, and store it in a variable, let's call it lastDigit2.
  5. Compare lastDigit1 with lastDigit2.
  6. If they are equal, print or return "The numbers have the same last digit."
  7. If they are not equal, print or return "The numbers do not have the same last digit."

 

Code Examples

#1 Code Example- Check the Last Digit

Code - Javascript Programming

/* program to check whether the last digit of three
numbers is same */

// take input
const a = prompt('Enter a first integer: ');
const b = prompt('Enter a second integer: ');
const c = prompt('Enter a third integer: ');

// find the last digit
const result1 = a % 10;
const result2 = b % 10;
const result3 = c % 10;

// compare the last digits
if(result1 == result2 && result1 == result3) {
    console.log(`${a}, ${b} and ${c} have the same last digit.`);
}
else {
    console.log(`${a}, ${b} and ${c} have different last digit.`);
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter a first integer: 8
Enter a second integer: 38
Enter a third integer: 88
8, 38 and 88 have the same last digit.
Advertisements

Demonstration


JavaScript Programing Example to Check if the Numbers Have Same Last Digit-DevsEnv

Previous
JavaScript Practice Example #3 - Assign 3 Variables and Print Good Way