Answer to Question #186796 in C++ for john

Question #186796

Get an integer from the user and check if it is divisible by 2. Print appropriate message on the screen


1
Expert's answer
2021-04-28T15:49:38-0400
#include <iostream>

int main()
{
    std::cout << "Please input an integer: ";

    int n;
    std::cin >> n;

    if(!std::cin)
    {
        std::cerr << "Bad input\n";
        return 1;
    }

    if(n % 2 == 0)
    {
        std::cout << "The integer " << n << " is divisible by 2 without remainder\n";
    }
    else
    {
        std::cout << "The integer " << n << " is divisible by 2 with remainder\n";
    }
    
    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