+ 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.
6 Réponses
0
show us your attempt, and we will help you to make it work ;)
0
that's not your attempt, that the task link... save your code in a sololearn code playground and provide the link to it ^^
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
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
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;