Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript check if a number starts with another number

// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
 
// Function to check if B is a
// prefix of A or not
bool checkprefix(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[i]
            != s2[i]) {
            return false;
        }
    }
 
    // Return true
    return true;
}
 
// Driver Code
int main()
{
    // Given numbers
    int A = 12345, B = 12;
 
    // Function Call
    bool result = checkprefix(A, B);
 
    // If B is a prefix of A, then
    // print "Yes"
    if (result) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #javascript #check #number #starts #number
ADD COMMENT
Topic
Name
2+7 =