#include <iostream>
int main()
{
int number = 3875 ; // for example (non-negative)
std::cout << "the number " << number << " contains the digits " ;
while( number != 0 ) // repeat till we have nothing left
{
constint last_digit = number % 10 ; // 3875%10 yields 5
std::cout << last_digit << ' ' ; // print out the digit
number /= 10 ; // dividing by 10 throws away the last digit
// 3875/10 == 387; last digit of 387 is 387%10 == 7
// 387/10 == 38; last digit of 38 is 38%10 == 8 etc.
}
std::cout << " (in reverse order)\n" ;
}
#include <iostream>
int main()
{
int num;
do
{
std::cout << "Enter number (0 to 9999): ";
std::cin >> num;
} while (num < 0 || num > 9999);
int o, t, h, th;
//to get all the digits separately
o = num / 1 % 10; //ones
t = num / 10 % 10; //tens
h = num / 100 % 10; //hundreds
th = num / 1000 % 10; //thousands
if (o == 1 || o == 2 || o == 3 || t == 1 || t == 2 || t == 3 ||
h == 1 || h == 2 || h == 3 || th == 1 || th == 2 || th == 3)
{
std::cout << "Entered number, " << num << ", contains the digit 1, 2 or 3." << std::endl;
}
else
{
std::cout << "Entered number, " << num << ", does not contain the digit 1, 2 or 3." << std::endl;
}
return 0;
}