+ 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 Respuestas
+ 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