Home c++ Checking a number for evenness

# Checking a number for evenness

Author

Date

Category

There is the code below. You need to redo it using a function to check the parity of the entered number. After checking for parity, the program should ask for more numbers to enter.

``````# include & lt; iostream & gt;
using namespace std;
void main () {
setlocale (LC_ALL, "ukr");
int a;
cout & lt; & lt; ("-Enter the number:");
cin & gt; & gt; a;
if (a% 2 == 0)
{
cout & lt; & lt; ("-This is an even number \ n");
}
else
{
cout & lt; & lt; ("-This is an Odd number \ n");
}
system ("pause");
}
``````

Try it like this

``````std :: string str;
while (true)
{
std :: cout & lt; & lt; ("-Enter the number:");
std :: cin & gt; & gt; str;
if (str == "quit")
break;
int value = :: strtol (str.c_str (), 0, 10);
if (value% 2 == 0)
std :: cout & lt; & lt; ("-number \ n");
else
std :: cout & lt; & lt; ("-Odd \ n");
}
``````

Only here the situation is not handled when an invalid string is transmitted

``````for (;;)
{
int N;
cout & lt; & lt; "Enter a number; 0 to complete:";
if (! (cin & gt; & gt; N) || (N == 0)) break;
cout & lt; & lt; N & lt; & lt; "-" & lt; & lt; ((N & amp; 1)? "Not": "") & lt; & lt; "even number \ n";
}
``````

``````for (i = 1; i & lt; = 1000; i ++)
{
cout & lt; & lt; ("-Enter the number:");
cin & gt; & gt; a;
if (a% 2 == 0)
{
cout & lt; & lt; ("-This is an even number \ n");
}
else
{
cout & lt; & lt; ("-This is an Odd number \ n");
}
}
``````

then the loop will check 1000 times, it all depends on how much and under what conditions to check for parity, otherwise another loop may be needed

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.