+ 1

Write program to check if number is even or odd. Take an int input from user, if it's even print "even", otherwise print "Odd"

Guys please Help me with this. Use IF-Else Statement.

8th Jul 2021, 10:45 PM
Elvis Noko Kgomo
Elvis Noko Kgomo - avatar
6 Réponses
0
show us your attempt, and we will help you to make it work ;)
8th Jul 2021, 10:46 PM
visph
visph - avatar
0
that's not your attempt, that the task link... save your code in a sololearn code playground and provide the link to it ^^
8th Jul 2021, 10:49 PM
visph
visph - avatar
8th Jul 2021, 10:56 PM
Elvis Noko Kgomo
Elvis Noko Kgomo - avatar
0
even or odd means divisible by 2 or not... your code check if input is equal to 10 or -10... you need to use modulo operator (%) to check divisibility by 2: n%2 == 0 => divisible by 2 n%2 != 0 => not divisible by 2
8th Jul 2021, 10:59 PM
visph
visph - avatar
0
Pallavi Bhardwaj your code blindly replace Elvis Noko Kgomo number equals 10 and -10 by number % 2 equals 0 and not equals 0... but in addition of: if (number%2!=0){ cout<<"Even"<<endl; } being logical mistake, what is its use as inside: if (number%2 == 0){ } rather than providing pointless code without explanation, try to help OP by explaining what he do wrong and what to do to correct himself his code ^^ if n == 10 is true, then n == -10 is necessarly false if n%2 == 0 is true, then n%2 != 0 is false... and else (n%2 == 0 is false) then n%2 != 0 is necessarly true
9th Jul 2021, 11:20 AM
visph
visph - avatar
0
#include <iostream> using namespace std; int main() { int number; cin>>number; // your code goes here if (number%2 == 0){ cout << "even" << endl; } else { cout << "odd" << endl; } return 0;
29th Dec 2021, 5:06 PM
Pedro Alvarez