The “else” Statement (C++). | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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!*/

23rd Feb 2021, 6:01 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
8 Answers
+ 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.
23rd Feb 2021, 6:10 AM
Ipang
+ 4
No problem 👌 And good luck! 👍
23rd Feb 2021, 6:18 AM
Ipang
+ 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
23rd Feb 2021, 7:24 AM
Arsenic
Arsenic - avatar
+ 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
24th Feb 2021, 5:15 AM
Arsenic
Arsenic - avatar
+ 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. 🙏🏼
24th Feb 2021, 5:18 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 1
Ipang Thank you! That makes sense!
23rd Feb 2021, 6:17 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 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? */
24th Feb 2021, 4:45 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 1
Tahiti🍷 add another =between 2 & 0
9th Apr 2021, 3:37 PM
Mario
Mario - avatar