English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

you are to prompt the user to enter an integer value, and then accept the value from the user. The program should then determine and display an appropriate message as to whether the entered integer's value was EVEN or ODD. HINT: this is a situation where the modulo (remaindering) operation may come in handy; that is, what do you get as a result of taking an integer modulo 2 (divide the number by 2 and take the remainder)?

This is the last part of the HW that I am suppose to do, but I can't seem to figure out the modulo.

Can anybody help me with this?

I think if-else statement is needed for this, since that's what we are currently learning.

Thank you

2007-02-21 13:32:34 · 6 answers · asked by Anonymous in Computers & Internet Programming & Design

6 answers

#include

int num1;


main()
{
cout<<"Enter a number:";
cin>>num1;

if(num1%2==0)
{
cout<<"EVEN";
}
else
{
cout<<"ODD";
}
}

2007-02-21 13:45:37 · answer #1 · answered by peperoni 2 · 0 0

The modulus operator in c++ is %. So, you can solve the question using a= x%2 and if a=0 then the integer number entered is Even otherwise it is odd.

You can use conditional operator ? and : to check this condition like this :

(a%2==0)?cout<<"Even Number ":cout<<"Odd Number." ;

2007-02-21 13:51:43 · answer #2 · answered by Neeta Suryvansh 2 · 0 0

modulo arithmetic is the remainder after a division.
x / y = z + remainder
x % y = remainder

% is the modulo operator in C and C++

2007-02-21 13:41:03 · answer #3 · answered by ccGuru 1 · 0 0

#include
using namespace std;

int main(){
long number;
while(1){
cout << "\n\nEnter an integer number : ";
cin >> number;
(number % 2 == 0) ? cout << "Even Number " : cout << "Odd Number." ;
}
}

2007-02-21 18:31:32 · answer #4 · answered by iyiogrenci 6 · 0 0

#include

int main()
{
int i;
cout<<"Enter an integer : ";
cin>>i;
if(i%2==0)
cout<<"The integer is even.";
else
cout<<"The integer is odd";
return 0;
}

2007-02-21 14:00:35 · answer #5 · answered by sentinels_of_evil 1 · 0 0

do no longer enable a grade in a class verify no count in case you drop it or no longer. determine base off no count in case you desire to or no longer, and what you get from taking the class or yet another... which ever is extra effectual for you i assume.

2016-10-16 05:18:05 · answer #6 · answered by ? 4 · 0 0

fedest.com, questions and answers