+ 1
The âelseâ Statement (C++).
Write a program to check if the number is even or odd. Take an integer input from the user, if it is even print "even", otherwise print "odd". /* I havenât learned how to tell the program to determine odd and even numbers, yet the module gives me this challenge. Please help!*/
8 RĂ©ponses
+ 6
Hint:
An even number leaves no remainder when the number is divided by 2. You can use modulo binary operator % to check the remainder of division;
<remainder> = <num1> % <num2>;
(Edit)
Modulo binary operator is meant for integer operands e.g. `short`, `int`, `long`, `long long`, and their unsigned variants.
+ 4
No problem đ
And good luck! đ
+ 3
Although a BIT advanced.
You can also go for bit manipulation to check for parity of a number much faster by making use of the fact that LSB(least significant bit) of an even number will always be zero and that to of odd numbers is one, so the parity check is as simple as doing
if(number&1)
// Number is odd
else
// Number is even
+ 2
đTahitiđ· in you *if* statment, you are using assignment operator (=) instead of equality operator (==)
Here's the fix
https://code.sololearn.com/c19Qw4XhelfT/?ref=app
+ 2
Arsenic Youâve saved me from ripping out my hair. I remember learning that, but hadnât thought of it while coding my answer.
Thank you. đđŒ
+ 1
Ipang
Thank you! That makes sense!
+ 1
//đ©đ©đ©
#include <iostream>
using namespace std;
int main() {
int number;
cin>> number;
// your code goes here
if (number % 2= 0)
cout << "even"
else
cout << "odd"
return 0;
/* âlvalue required as left operand of assignment.â So it wants me to put some value in , instead of the word ânumberâ? or what does this error mean? */
+ 1
Tahitiđ· add another =between 2 & 0