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.
#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;
}
Comments
Leave a comment