- 1

Write a program to check given number even or odd.

14th Dec 2017, 3:25 PM
Devendra
7 Answers
+ 7
Go to the Code Playground, type in "even or odd" in the search bar at the top and you'll get a bunch of C++ codes created by the community users! :)
14th Dec 2017, 3:48 PM
Dev
Dev - avatar
+ 5
print("{}".format("odd" if int(input()) % 2 else "even")) Sorry - it's not C++ 😎
14th Dec 2017, 3:53 PM
David Ashton
David Ashton - avatar
+ 5
int main() {int n; cin>>n; if(n&1==1) cout<<"odd"; else cout<<"even"; return 0; }
14th Dec 2017, 3:55 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 5
If a given number can be divided by 2 without remainder, it's an even number, otherwise it's an odd number. So, you need to get the remainder of a division. The operator, which returns the remainder is called the modulo operator - %. Example: 5 % 2 = 1, because the closest number between 5 and 2, divisible by 2 without remainder is 4 (4 / 2 = 2, rem. = 0). 7 % 5 = {2}, because: 7 / 5 = 1 (rem. 2) 6 / 5 = 1 (rem. 1) 5 / 5 = 1 (rem. 0) 7 - 5 = {2} 7 % 2 = {1}, because: 7 / 2 = 3 (rem. 1) 6 / 2 = 3 (rem. 0) 7 - 6 = {1} 17 % 2 = 1 16 % 2 = 0 15 % 2 = 1 14 % 2 = 0 As you can see, division by 2, gives remainder of 0 or 1. And like I mentioned above, if the number is divisible by 2 without rem. it's even. The main problem for your program is solved. The second part is to print whether it's even or odd by using conditional construction. // Declare and input integer int n; cin >> n; // Get the remainder int remainder = n % 2; // Condition if (remainder == 0) { cout << "even"; } else { cout << "odd"; }
14th Dec 2017, 4:32 PM
Boris Batinkov
Boris Batinkov - avatar
14th Dec 2017, 4:16 PM
Hrishikesh Kulkarni
Hrishikesh Kulkarni - avatar
0
#include<iostream> using namespace std; int number; int main(){ cin>>number; if(number%2 == 0){ cout<<"even"; } else{ cout<<"odd"; } return 0; }
23rd Dec 2021, 2:01 AM
Blas Belliard
0
#include<iostream> using namespace std; int number; int main(){ cin>>number; if(number%2 == 0){ cout<<"even"; } else{ cout<<"odd"; } return 0; } Good Luck
25th Jan 2022, 11:28 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar