Check if a number ends with another number or not - GeeksforGeeks

22 Nov, 2021
Given two numbers A and B where (A > B), the task is to check if B is a suffix of A or not. Print “Yes” if it is a suffix Else print “No”.Examples:  
Input: A = 12345, B = 45 Output: YesInput: A = 12345, B = 123 Output: No  
 
Method 1:  
Convert the given numbers A and B to strings str1 and str2 respectively.Traverse both the strings from the end of the strings.While traversing the strings, if at any index characters from str1 and str2 are unequal then print “No”.Else print “Yes”.
Convert the given numbers A and B to strings str1 and str2 respectively.
Traverse both the strings from the end of the strings.
While traversing the strings, if at any index characters from str1 and str2 are unequal then print “No”.
Else print “Yes”.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include "bits/stdc++.h"using namespace std; // Function to check if B is a// suffix of A or notbool checkSuffix(int A, int B){     // Convert numbers into strings    string s1 = to_string(A);    string s2 = to_string(B);     // Find the lengths of strings    // s1 and s2    int n1 = s1.length();    int n2 = s2.length();     // Base Case    if (n1 < n2) {        return false;    }     // Traverse the strings s1 & s2    for (int i = 0; i < n2; i++) {         // If at any index characters        // are unequals then return false        if (s1[n1 - i - 1]            != s2[n2 - i - 1]) {            return false;        }    }     // Return true    return true;} // Driver Codeint main(){    // Given numbers    int A = 12345, B = 45;     // Function Call    bool result = checkSuffix(A, B);     // If B is a suffix of A, then    // print "Yes"    if (result) {        cout << "Yes";    }    else {        cout << "No";    }    return 0;}
// Java program for the above approachclass GFG{     // Function to check if B  // is a suffix of A or notpublic static boolean checkSuffix(int A,                                  int B){         // Convert numbers into strings    String s1 = String.valueOf(A);    String s2 = String.valueOf(B);         // Find the lengths of strings    // s1 and s2    int n1 = s1.length();    int n2 = s2.length();         // Base case    if (n1 < n2)    {        return false;    }         // Traverse the strings s1 & s2    for(int i = 0; i < n2; i++)    {                // If at any index characters       // are unequals then return false       if (s1.charAt(n1 - i - 1) !=           s2.charAt(n2 - i - 1))       {           return false;       }    }         // Return true    return true;} // Driver codepublic static void main(String[] args){             // Given numbers    int A = 12345, B = 45;         // Function Call    boolean result = checkSuffix(A, B);         // If B is a suffix of A,     // then print "Yes"    if (result)    {        System.out.print("Yes");    }    else    {        System.out.println("No");    }}} // This code is contributed by divyeshrabadiya07
# Python3 program for the above approach # Function to check if B is a# suffix of A or notdef checkSuffix(A, B):     # Convert numbers into strings    s1 = str(A);    s2 = str(B);     # Find the lengths of strings    # s1 and s2    n1 = len(s1)    n2 = len(s2)     # Base Case    if (n1 < n2):        return False;         # Traverse the strings s1 & s2    for i in range(n2):         # If at any index characters        # are unequals then return false        if (s1[n1 - i - 1] != s2[n2 - i - 1]):            return False;                 # Return true    return True;     # Driver Code # Given numbersA = 12345B = 45; # Function Callresult = checkSuffix(A, B); # If B is a suffix of A, then# print "Yes"if (result):    print("Yes")else:    print("No") # This code is contributed by grand_master   
// C# program for the above approachusing System;class GFG{     // Function to check if B// is a suffix of A or notpublic static bool checkSuffix(int A,                               int B){         // Convert numbers into strings    string s1 = A.ToString();    string s2 = B.ToString();         // Find the lengths of strings    // s1 and s2    int n1 = s1.Length;    int n2 = s2.Length;         // Base case    if (n1 < n2)    {        return false;    }         // Traverse the strings s1 & s2    for(int i = 0; i < n2; i++)    {                 // If at any index characters        // are unequals then return false        if (s1[n1 - i - 1] !=  s2[n2 - i - 1])        {            return false;        }    }         // Return true    return true;} // Driver codepublic static void Main(string[] args){             // Given numbers    int A = 12345, B = 45;         // Function Call    bool result = checkSuffix(A, B);         // If B is a suffix of A,    // then print "Yes"    if (result)    {        Console.Write("Yes");    }    else    {        Console.Write("No");    }}} // This code is contributed by rutvik_56
<script> // javascript program for the above approach        // Function to check if B// is a suffix of A or not     function checkSuffix( A, B){           // Convert numbers into strings    var s1 = A.toString();    var s2 = B.toString();           // Find the lengths of strings    // s1 and s2         var n1 = s1.length;    var n2 = s2.length;           // Base case    if (n1 < n2)    {        return false;    }           // Traverse the strings s1 & s2    for(var i = 0; i < n2; i++)    {                   // If at any index characters        // are unequals then return false        if (s1[n1 - i - 1] !=  s2[n2 - i - 1])        {            return false;        }    }           // Return true    return true;}   // Driver code                // Given numbers    var A = 12345, B = 45;           // Function Call    var result = checkSuffix(A, B);           // If B is a suffix of A,    // then print "Yes"    if (result)    {        document.write("Yes");    }    else    {        document.write("No");    }     </script>    
Yes
 
Method 2: Using inbuilt function std::boost::algorithm::ends_with() which is included in Boost Library of C++ which is used to check whether any string contains suffix of another string or not.Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>#include <boost/algorithm/string.hpp>using namespace std; // Function to check if B is a// suffix of A or notvoid checkSuffix(int A, int B){     // Convert numbers into strings    string s1 = to_string(A);    string s2 = to_string(B);     bool result;     // Check if s2 is a suffix of s1    // or not using ends_with() function    result = boost::algorithm::ends_with(s1,                                         s2);     // If result is true, print "Yes"    if (result) {        cout << "Yes";    }    else {        cout << "No";    }} // Driver Codeint main(){    // Given numbers    int A = 12345, B = 45;     // Function Call    checkSuffix(A, B);    return 0;}
// Java program for the above approachimport java.util.*; class GFG{ // Function to check if B is a// suffix of A or notstatic void checkSuffix(int A, int B){     // Convert numbers into Strings    String s1 = String.valueOf(A);    String s2 = String.valueOf(B);     boolean result;     // Check if s2 is a suffix of s1    // or not    result = s1.endsWith(s2);     // If result is true, print "Yes"    if (result)    {        System.out.print("Yes");    }    else    {        System.out.print("No");    }} // Driver Codepublic static void main(String[] args){    // Given numbers    int A = 12345, B = 45;     // Function Call    checkSuffix(A, B);}} // This code is contributed by 29AjayKumar
# Python3 program for the above approach # Function to check if B is# a suffix of A or notdef checkSuffix(A, B):     # Convert numbers into strings    s1 = str(A)    s2 = str(B)     # Check if s2 is a suffix of s1    # or not    result = s1.endswith(s2)     # If result is true print "Yes"    if (result):        print("Yes")    else:        print("No") # Driver codeif __name__ == '__main__':     # Given numbers    A = 12345    B = 45     # Function call    checkSuffix(A, B) # This code is contributed by himanshu77
// C# program for the above approachusing System; class GFG{ // Function to check if B is a// suffix of A or notstatic void checkSuffix(int A, int B){     // Convert numbers into Strings    String s1 = String.Join("", A);    String s2 = String.Join("", B);     bool result;     // Check if s2 is a suffix of s1    // or not    result = s1.EndsWith(s2);     // If result is true, print "Yes"    if (result)    {        Console.Write("Yes");    }    else    {        Console.Write("No");    }} // Driver Codepublic static void Main(String[] args){    // Given numbers    int A = 12345, B = 45;     // Function Call    checkSuffix(A, B);}} // This code is contributed by 29AjayKumar
<script> // JavaScript program for the above approach // Function to check if B is a// suffix of A or notfunction checkSuffix( A, B){     // Convert numbers into Strings    let s1 = A.toString();    let s2 = B.toString();     let result;     // Check if s2 is a suffix of s1    // or not    result = s1.endsWith(s2);     // If result is true, print "Yes"    if (result)    {        document.write("Yes");    }    else    {        document.write("No");    }} // Driver Code     // Given numbers    let A = 12345, B = 45;     // Function Call    checkSuffix(A, B);             </script>
Yes
 
Method 3:  
Subtract B from A.Find the number of digits in B(say X) by using the Method 3 discussed in this article.Check whether A ends with atleast X number zeros or not. If yes then print “Yes”.Else print “No”.
Subtract B from A.
Find the number of digits in B(say X) by using the Method 3 discussed in this article.
Check whether A ends with atleast X number zeros or not. If yes then print “Yes”.
Else print “No”.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>#include <boost/algorithm/string.hpp>using namespace std; // Function to check if B is a// suffix of A or notbool checkSuffix(int A, int B){     // Find the number of digit in B    int digit_B = log10(B) + 1;     // Subtract B from A    A -= B;     // Returns true,    // if B is a suffix of A    return (A % int(pow(10, digit_B)));} // Driver Codeint main(){    // Given numbers    int A = 12345, B = 45;     // Function Call    bool result = checkSuffix(A, B);     // If B is a suffix of A, then    // print "Yes"    if (!result) {        cout << "Yes";    }    else {        cout << "No";    }    return 0;}
// Java program for the above approachimport java.util.*; class GFG{ // Function to check if B// is a suffix of A or notstatic boolean checkSuffix(int A, int B){     // Find the number of digit in B    int digit_B = (int) (Math.log10(B) + 1);     // Subtract B from A    A -= B;     // Returns true,    // if B is a suffix of A    return (A % (int)(Math.pow(10, digit_B)) > 0);} // Driver codepublic static void main(String[] args){     // Given numbers    int A = 12345, B = 45;     // Function call    boolean result = checkSuffix(A, B);     // If B is a suffix of A,    // then print "Yes"    if (!result)    {        System.out.print("Yes");    }    else    {        System.out.print("No");    }}} // This code is contributed by 29AjayKumar
# Python3 program for the above approachimport math # Function to check if B is a# suffix of A or notdef checkSuffix(A, B):     # Find the number of digit in B    digit_B = int(math.log10(B)) + 1;     # Subtract B from A    A -= B;     # Returns true,    # if B is a suffix of A    return (A % int(math.pow(10, digit_B))); # Driver Code # Given numbersA = 12345; B = 45; # Function Callresult = checkSuffix(A, B); # If B is a suffix of A, then# print "Yes"if (result == 0):    print("Yes"); else:    print("No"); # This code is contributed by Nidhi_biet
// C# program for the above approachusing System;class GFG{ // Function to check if B// is a suffix of A or notstatic bool checkSuffix(int A, int B){     // Find the number of digit in B    int digit_B = (int)(Math.Log10(B) + 1);     // Subtract B from A    A -= B;     // Returns true,    // if B is a suffix of A    return (A % (int)(Math.Pow(10, digit_B)) > 0);} // Driver codepublic static void Main(){     // Given numbers    int A = 12345, B = 45;     // Function call    bool result = checkSuffix(A, B);     // If B is a suffix of A,    // then print "Yes"    if (!result)    {        Console.Write("Yes");    }    else    {        Console.Write("No");    }}} // This code is contributed by Code_Mech
<script>     // JavaScript program for the above approach         // Function to check if B    // is a suffix of A or not    function checkSuffix(A, B)    {         // Find the number of digit in B        let digit_B = parseInt(Math.log10(B) + 1, 10);         // Subtract B from A        A -= B;         // Returns true,        // if B is a suffix of A        return (A % (Math.pow(10, digit_B)) > 0);    }         // Given numbers    let A = 12345, B = 45;      // Function call    let result = checkSuffix(A, B);      // If B is a suffix of A,    // then print "Yes"    if (!result)    {        document.write("Yes");    }    else    {        document.write("No");    }     </script>
Yes
 
Time Complexity: O(1)Auxiliary Space: O(1)
divyeshrabadiya07
rutvik_56
29AjayKumar
Code_Mech
nidhi_biet
grand_master
Akanksha_Rai
himanshu77
bunnyram19
susmitakundugoaldanga
mukesh07
subham348
Kirti_Mangal
number-digits
Numbers
Mathematical
Strings
Strings
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program to find GCD or HCF of two numbers
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find sum of elements in a given array
Reverse a string in Java
Write a program to reverse an array or string
Longest Common Subsequence | DP-4
Python program to check if a string is palindrome or not
Different methods to reverse a string in C/C++