Check if a number ends with another number or not


A typical programming challenge is determining whether a number terminates with another number. To solve this problem, you must identify the last few digits of a given number and check to see if they match another number. Numerous applications, including data processing, string manipulation, and numerical analysis, frequently include this kind of operation. Programming approaches including converting numbers to strings, modular arithmetic, and the use of logical operators are used to solve this challenge. Beginner and intermediate programmers who want to get better at manipulating numbers and solving algorithmic issues should be interested in this topic.

Methods

There are various ways to check if a number ends with another number or not. Here are two common methods −

  • Using modulo operator (%)

  • Using string conversion

Method 1: Using modulo operator (%)

If two numbers are split, the modulo operator returns the remainder. Utilising the modulo operator with the second number as the divisor will allow us to determine if a number ends with another number. First number ends with second number if outcome equals second number.

The commonly used mathematical operator known as the modulo returns the remainder of a division operation, and it is represented by the % symbol. Verifying if several finishes with another number or not is a useful application of the modulo operator.

We can use the modulo operator to get the remaining value after dividing a number "n" by another number "m" to determine if the two numbers are consecutive. N ends with m if the residual equals m. It doesn't if not.

Syntax

The syntax for this method is as follows −

  • function to check if a number ends with another number

bool endsWith(int number, int ending) {
   int digits = floor(log10(ending)) + 1; 
  • get the number of digits in the ending number

int divisor = pow(10, digits); 
  • calculate the divisor

int remainder = number % divisor; 
  • calculate the remainder

return remainder == ending; }
  • return true if the remainder is equal to the ending number

Here, number is the original number and ending is the number to check if it is at the end of number. The floor(log10(ending)) + 1 expression calculates the number of digits in ending, and pow(10, digits) calculates the divisor. The remainder of the division of number by divisor is then calculated using the modulo operator %. If the remainder is equal to ending, then the function returns true, indicating that number ends with ending.

Algorithm

The modulo operator is used in the following C++ procedure to determine whether an integer finishes with another number −

Step 1 − Enter num and end Num, the two numbers to be compared.

Step 2 − Utilise the modulo operator (%) to calculate remaining part of division of num by 10.

Step 3 − If number ends with end Num and the remainder equals end Num, then return true.

Step 4 − If not, divide number by 10 and repeat steps 1-2 till number equals 0.

Step 5 − The number does not end with end Num if the loop ends without a match, thus return false.

Example 1

An example of the modulo operator to check if a number ends with another number −

The endsWith function in this illustration accepts the two integer arguments number and ending. The modulo operator% is then used to determine if the number's last digit equals ending. The function returns true if it is, and false if it is not.

We first define number and ending in the main function before calling the endsWith method. We print a message stating that the number ends with ending if the method returns true. If not, a message stating that the number does not conclude with ending is printed.

It should be noted that this is a very simple example and that there are numerous different approaches (such as string manipulation methods) to determining whether a number ends with another number. The modulo operator is a well-liked and effective technique, though.

#include <iostream>
using namespace std;

bool endsWith(int number, int ending) {
   return (number % 10) == ending; // Check if last digit is equal to ending
}

int main() {
   int number = 12345;
   int ending = 5;

   if (endsWith(number, ending)) {
      cout << number << " ends with " << ending << endl;
   }
   else {
      cout << number << " does not end with " << ending << endl;
   }

   return 0;
}

Output

12345 ends with 5

Method 2: Using string conversion

Using string manipulation functions, this technique checks to see if the ends of the two integers match by turning both numbers to strings. One typical method is to use the to_string() function to convert the numbers to strings, and then find whether final few characters of the first string match those of the second string.

Syntax

Here is the syntax without actual code of string conversion methods to check if a number ends with another number or not in C++ −

  • Convert the numbers to strings

string num1_str = to_string(num1);
string num2_str = to_string(num2);
  • Check if the last characters of the strings are equal

bool ends_with = num1_str.substr(num1_str.size() - num2_str.size()) == num2_str;

Algorithm

Here is a C++ algorithm to determine whether a number of finishes with another number −

Step 1 − Start by creating strings for the two integers.

Step 2 − Assuming that the second integer is called n, ascertain its length.

Step 3 − Return false if the first number's length is smaller than n.

Step 4 − Apply the substr method to the first number to extract the last n digits.

Step 5 − Apply the stoi method to convert the second number and the retrieved substring to integers.Comparing the two integers at

Step 6 − Give true if they are equal. If not, return false.

Example 2

The two numbers are converted to strings using to_string () function. The final few characters of first string are then extracted using substr () function so that they match length of second string. Using == operator, these extracted characters are then compared to second string.

#include <iostream>
#include <string>

using namespace std;

int main() {
   int num1 = 123456;
   int num2 = 56;

   string str1 = to_string(num1);
   string str2 = to_string(num2);

   if (str1.substr(str1.length() - str2.length()) == str2) {
      cout << "Number 1 ends with number 2" << endl;
   } else {
      cout << "Number 1 does not end with number 2" << endl;
   }

   return 0;
}

Output

Number 1 ends with number 2

Conclusion

In conclusion, comparing the last few characters of each string using the substr() function or performing modular arithmetic to isolate the last few digits of each number and directly comparing them are two ways to tell if a number ends with another number in C++. Both strategies work well and may be put into practise using basic C++ programming structures. Numerous numerical and computational applications that call for pattern matching can benefit from this task.

Updated on: 31-Jul-2023

543 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements