w3resource

C++ Exercises: Check if two given non-negative integers have the same last digit

C++ Basic Algorithm: Exercise-23 with Solution

Same Last Digit for Integers

Write a C++ program to check if two given non-negative integers have the same last digit.

Sample Solution:

C++ Code :

#include <iostream>
using namespace std;

// Function to check if the last digits of two numbers have the same absolute value
bool test(int x, int y)
{
    // Return true if the absolute value of the last digit of both x and y are equal
    return abs(x % 10) == abs(y % 10);
}

// Main function
int main() 
{
    // Output the result of the test function with different input numbers
    cout << test(123, 456) << endl;
    cout << test(12, 512) << endl;
    cout << test(7, 87) << endl;
    cout << test(12, 45) << endl;

    return 0;    // Return 0 to indicate successful execution of the program
}

Sample Output:

0
1
1
0

Visual Presentation:

C++ Basic Algorithm Exercises: Check if two given non-negative integers have the same last digit.

Flowchart:

Flowchart: Check if two given non-negative integers have the same last digit.

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C++ program to check if a given string contains between 2 and 4 'z' character.
Next: Write a C++ program to create a new string which is n (non-negative integer) copies of a given string.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/cpp-exercises/basic-algorithm/cpp-basic-algorithm-exercise-23.php