Answer to Question #320400 in C++ for Oneeeee

Question #320400

Input two integers in one single line. The first inputted integer must be within 0-9 only.


Using loops, identify if the first inputted integer (0-9) is present in the second inputted integer. If it is found, print "Yes"; otherwise, print "No". Tip: If the number is already found even without reaching the end of the loop, use the break keyword to exit the loop early for a more efficient code.

1
Expert's answer
2022-03-30T17:46:24-0400
#include <iostream>
#include <string>
using namespace std;


int main()
{
    int firstNumber, secondNumber;
    cout << "Enter two numbers(first must be within 0 - 9): ";
    cin >> firstNumber >> secondNumber;
    while (firstNumber < 0 || firstNumber > 9) {
        cout << "Please enter first number within 0 - 9." << endl;
        cout << "Enter two numbers: ";
        cin >> firstNumber >> secondNumber;
    }


    string firstIntToString = to_string(firstNumber);
    string secondIntToString = to_string(secondNumber);


    for (int i = 0; i < secondIntToString.length();i++) {
        string toSplit = secondIntToString.substr(i,1);
        if (toSplit==firstIntToString) {
            cout<< "Yes" << endl;
            break;
        }
        else {
            cout << "No" << endl;
        }
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog