- 1
Write a program to check given number even or odd.
7 Réponses
+ 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! :)
+ 5
print("{}".format("odd" if int(input()) % 2 else "even"))
Sorry - it's not C++ 😎
+ 5
int main() {int n;
cin>>n;
if(n&1==1)
cout<<"odd";
else
cout<<"even";
return 0;
}
+ 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";
}
0
Check this one 👍
https://code.sololearn.com/cIOQrA3K8o63/?ref=app
0
#include<iostream>
using namespace std;
int number;
int main(){
cin>>number;
if(number%2 == 0){
cout<<"even";
}
else{
cout<<"odd";
}
return 0;
}
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