C++ Exercises: Check if a given number is within 2 of a multiple of 10
Within 2 of Multiple of 10
Write a C++ program to check if a given number is within 2 of a multiple of 10.
Sample Solution:
C++ Code :
#include <iostream>
using namespace std;
// Function to check if a number's last digit is less than or equal to 2 OR greater than or equal to 8
bool test(int n)
{
// Returns true if the last digit of n is less than or equal to 2 OR greater than or equal to 8
return n % 10 <= 2 || n % 10 >= 8;
}
int main()
{
// Testing the test function with different input values
cout << test(3) << endl;
cout << test(7) << endl;
cout << test(8) << endl;
cout << test(21) << endl;
return 0; // Return 0 to indicate successful completion
}
Sample Output:
0 0 1 1
Flowchart:

For more Practice: Solve these Related Problems:
- Write a C++ program to check if a given number is within 2 units of any multiple of 10 by calculating its distance from the nearest multiple.
- Write a C++ program that reads an integer and outputs true if the absolute difference between it and the nearest multiple of 10 is less than or equal to 2.
- Write a C++ program to determine whether a number is almost a multiple of 10 (within 2), using modulo operations to measure closeness.
- Write a C++ program that computes the remainder of a number divided by 10 and returns true if this remainder is 0, 1, 2, 8, or 9.
Go to:
PREV : Within 2 of Multiple of 10.
NEXT : Return 18 if Integer in Range 10–20.
C++ Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?