Someone please guide me how to check whether a specific digit exists in an integer
or not. for the sake of code optimization I am trying to avoid the use of strings
or any kind of loops
to iterate over all the digits
of the integer
.
If I need to find out whether 4
exists the in an integer or not, input and the output samples are given below:
Sample Input:
154
Sample Output:
true
Desired Code:
bool ifExists(int digit, int number)
{
if()
{
return true;
}
else
{
return false;
}
}
Possible Logic:
I believe there must be a mathematical approach which will do the job in the if
condition, however I am unable to find such method in cmath
library.
Convert integer to string, do a string search for digit.
The "mathematical" method would have to do the same, compute the digit sequence by division/remainder by 10 and compare with the given digit.