Answer to Question #185301 in C++ for nethu

Question #185301

Write statements to validate the number given below is a 4-digit integer and then to print the digit divisable by 2,

int number = 8459


1
Expert's answer
2021-04-26T07:58:23-0400
#include <iostream>
#include <string>
using namespace std;


int main()
{
    int number = 8459;


    string digits = to_string(number);


    if(digits.size() == 4)
        cout << number << " is a 4-digit number.";
    else
        cout << number << "is not a 4-digit number.";


    cout << endl << endl;


    cout << "Digits, divisable by 2: ";


    for(int i = 0; i < digits.size(); i++)
    {
        int digit = digits[i] - '0';


        if(digit%2==0)
            cout << digit << " ";
    }
    cout << endl << endl;


    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