“JavaScript prüft, ob eine Nummer mit einer anderen Nummer beginnt” Code-Antworten

JavaScript prüft, ob eine Nummer mit einer anderen Nummer beginnt

<script>
 
// javascript program for the above approach
   
 
// Function to check if B is a
// prefix of A or not
 
function checkprefix( 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[i] != s2[i])
        {
            return false;
        }
    }
   
    // Return true
    return true;
}
   
// Driver Code
 
       
    // Given numbers
    var A = 12345, B = 12;
   
    // Function call
    var result = checkprefix(A, B);
   
    // If B is a prefix of A, then
    // print "Yes"
    if (result)
    {
        document.write("Yes");
    }
    else
    {
        document.write("No");
    }
 
 
</script>
Crazy Caiman

JavaScript prüft, ob eine Nummer mit einer anderen Nummer beginnt

// 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;
}
Crazy Caiman

Ähnliche Antworten wie “JavaScript prüft, ob eine Nummer mit einer anderen Nummer beginnt”

Fragen ähnlich wie “JavaScript prüft, ob eine Nummer mit einer anderen Nummer beginnt”

Weitere verwandte Antworten zu “JavaScript prüft, ob eine Nummer mit einer anderen Nummer beginnt” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen