Home >Backend Development >C++ >Check if a number ends with another number

Check if a number ends with another number

WBOY
WBOYforward
2023-08-26 13:25:031271browse

Check if a number ends with another number

A typical programming challenge is determining whether a number ends with another number. To solve this problem, you have to identify the last digits of a given number and check if they match another number. Many applications, including data processing, string manipulation, and numerical analysis, frequently involve such operations. Programming methods including converting numbers to strings, modulo arithmetic, and the use of logical operators are used to solve this challenge. This topic should be of interest to beginners and intermediate programmers who want to get better at manipulating numbers and solving algorithmic problems.

method

There are multiple ways to check if a number ends with another number. Here are two common methods -

  • Use the modulo operator (%)

  • Use string conversion

Method 1: Use the modulo operator (%)

If two numbers are divided, the modulo operator returns the remainder. Using the modulo operator, we can determine whether one number ends with another number, using the second number as the divisor. If the result is equal to the second number, the first number ends with the second number.

The commonly used mathematical operator is called the modulo operator, which returns the remainder of the division operation and is represented by the symbol %. A useful application is to use the modulo operator to verify whether one number ends with another number.

We can use the modulo operator to get the remaining value after dividing a number "n" by another number "m" to determine whether the two numbers are continuous. If the remainder is equal to m, then n ends in m. If it is not equal to m, then it is not.

grammar

The syntax of 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 divisor

int remainder = number % divisor; 
  • Calculate remainder

return remainder == ending; }
  • Returns 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 expression floor(log10(ending)) 1 calculates the number of digits in ending, while pow(10, digits) calculates the divisor. The remainder of number divided by divisor is then calculated using the modulo operator %. If the remainder is equal to ending, 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 ends with another number −

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

Step 2 - Use the modulo operator (%) to calculate the remainder of num divided by 10.

Step 3 − Return true if the number ends with the end digit and the remainder is equal to the end digit.

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

Step 5 - If the loop ends without a match, the number does not end with end Num, so false is returned.

Example 1

Example of using the modulo operator to check if a number ends with another number −

In this example, the endsWith function accepts two integer parameters number and ending. Then use the modulo operator % to determine whether the last digit of the number is equal to ending. If it is, the function returns true, otherwise it returns false.

Before calling the endsWith method, we first define number and ending in the main function. If the method returns true, we print a message stating that the number ends with ending. If not, print a message stating that the number does not end with ending.

It is important to note that this is just a very simple example, there are many different methods (such as string manipulation methods) to determine whether a number ends with another number. The modulo operator is a popular and effective technique.

#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: Use string conversion

Using string manipulation functions, this technique checks whether the ends of two integers match by converting them to strings. A common approach is to use the to_string() function to convert a number to a string and then find if the last few characters of the first string match the last few characters of the second string.

grammar

The following is the syntax of the string conversion method in C to check whether a number ends with another number, without the actual code:

  • Convert number to string

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

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

algorithm

Here is a C algorithm for determining if a number ends with another number −

Step 1 - Start by creating strings for these two integers.

Step 2 - Assuming the second integer is called n, determine its length.

步骤 3 - 如果第一个数字的长度小于 n,则返回 false。

步骤 4 − 将 substr 方法应用于第一个数字,以提取最后 n 位数字。

第五步 - 使用stoi方法将第二个数字和检索到的子字符串转换为整数。比较这两个整数

步骤6 - 如果它们相等,则返回true。如果不相等,则返回false。

Example 2

使用 to_string() 函数将这两个数字转换为字符串。然后使用 substr() 函数提取第一个字符串的最后几个字符,使其与第二个字符串的长度相匹配。然后使用 == 运算符将这些提取的字符与第二个字符串进行比较。

#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;
}

输出

Number 1 ends with number 2

结论

总之,使用substr()函数比较每个字符串的最后几个字符,或者使用模运算来分离每个数字的最后几位数并直接比较它们,这两种方法都可以判断一个数字是否以另一个数字结尾。这两种策略都很有效,并且可以使用基本的C++编程结构来实践。许多需要模式匹配的数值和计算应用都可以从这个任务中受益。

The above is the detailed content of Check if a number ends with another number. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete