Write statements to validate the number given below is a 4-digit integer and then to print the digits divisible by 2.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int number = 8459;
string digits = to_string(number);
//Write statements to validate the number given below is a 4-digit integer
if(digits.size() == 4)
cout << number << " is a 4-digit number.";
else
cout << number << "is not a 4-digit number.";
//Print out the digits divisible by 2.
cout << "\n\nDigits of 8459 divisable by 2 are: ";
for(int i = 0; i < digits.size(); i++)
{
int digit = digits[i] - '0';
if(digit%2==0)
cout << digit << " ";
}
return 0;
}
Comments
Leave a comment