Answer to Question #231683 in C++ for Yasas Sri Silva

Question #231683

Write statements to validate the number given below is a 4-digit integer and then to print the digits divisible by 2.

int number=8459;


1
Expert's answer
2021-08-31T23:51:37-0400
//A C++ program Write statements to validate the number given below is a 4-digit integer and then to print the digits divisible by 2.
//int number=8459;

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int num = 8459;
    string digits = to_string(num);
 //Write statements to validate the number given below is a 4-digit integer
    if (num >= 1000 && num <= 9999)
        cout<<num<<" is a 4 digit number";
    else
        cout << num << "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;

}

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
APPROVED BY CLIENTS