0

How to create a even odd detecting program in c

27th Apr 2020, 11:00 AM
Kunsh-Tyagi
Kunsh-Tyagi - avatar
42 ответов
+ 4
#include<stdio> int main() { int n; scanf("%d",&n); if(n&1) printf("it's even"); else printf("it's odd"); return 0; } Just see this if condition it ands with last bit with 1 if it gives '1' then it's odd else it would be even.
28th Apr 2020, 5:38 AM
Nikhil Maroju
Nikhil Maroju - avatar
+ 3
1 is odd, then 1/3 is odd too (modulo calculating).... More general: if a is kongruent (===) to 1 modulo 2 and b===1 (mod) 2 then a/b===1 (mod) 2 too, so odd/odd is odd (1/3,5/7) if a is kongruent (===) to 0 modulo 2 and b===1 (mod) 2 then a/b===0 (mod) 2 too, so even/odd is even (2/3,6/7) the last case odd/even is undefined, since even===0 mod 2 and Division by 0 is undefined.... Conclusion: even/odd=even odd/odd=odd odd/even=undefined even/even=can be reduced So this is for all fractions...
28th Apr 2020, 8:15 AM
Alexander Thiem
Alexander Thiem - avatar
+ 2
if (c%2==0) { //even }else { //odd }
27th Apr 2020, 11:03 AM
Alexander Thiem
Alexander Thiem - avatar
+ 2
Use simple if else as n%2==0 even...else odd
28th Apr 2020, 6:40 AM
Aryan Raj
Aryan Raj - avatar
+ 1
Hint:Check if given number divided by 2 results in remainder being 0 ,if yes then it is even otherwise odd
27th Apr 2020, 11:03 AM
Abhay
Abhay - avatar
+ 1
Mas Has "if you see closely" you wrote char for char the exact same things than Amit singh ^^
29th Apr 2020, 6:31 AM
visph
visph - avatar
+ 1
visph Martin Taylor U Hariharan Not only integers can be odd or even: 1 is odd, then 1/3 is odd too (modulo calculating).... More general: if a is kongruent (===) to 1 modulo 2 (which means a is odd) and b===1 (mod) 2 then a/b===1 (mod) 2 too, so odd/odd is odd (1/3,5/7) if a is kongruent (===) to 0 modulo 2 (which means even) and b===1 (mod) 2 then a/b===0 (mod) 2 too, so even/odd is even (2/3,6/7) the last case odd/even is undefined, since even===0 mod 2 and Division by 0 is undefined.... Conclusion: even/odd=even odd/odd=odd odd/even=undefined even/even=can be reduced So this is how even and odd is defined for all fractions... Dame arguments can be tried for sqrt....
29th Apr 2020, 8:52 AM
Alexander Thiem
Alexander Thiem - avatar
+ 1
visph Yeah you r right, but the result of my last post was just ignorant posting of the same simple solution 4 times, so I reposted it
29th Apr 2020, 9:08 AM
Alexander Thiem
Alexander Thiem - avatar
+ 1
Kunsh-Tyagi can you edit your question to solved please!!!
29th Apr 2020, 9:48 AM
Alexander Thiem
Alexander Thiem - avatar
0
@Abhay i wanted to know how to give integers as an input
27th Apr 2020, 3:09 PM
Kunsh-Tyagi
Kunsh-Tyagi - avatar
0
Martin Taylor wrote: "for integers" Does that means that a real number could be odd or even? (serious question, even if my first reaction was to laugh ^^ But maybe do I miss something in mathematics field?)
27th Apr 2020, 5:52 PM
visph
visph - avatar
0
Kunsh-Tyagi read your question
27th Apr 2020, 6:00 PM
Abhay
Abhay - avatar
0
Nikhil Maroju Why not just upvotes Martin Taylor rather than implements his solution? All the more than if you do all OP works, Kunsh-Tyagi will not learn a lot ^^ (as the first example of this idea, it would be acceptable, but to repeat something already stated is stupid ;P)
28th Apr 2020, 6:11 AM
visph
visph - avatar
0
Before we think about Odd numbers we have to think about basic mathematics we can only classify integers into odd or even .... we can't take any real number not an integer for this argument Trying to Classifying a real number not an integer into odd or even is a useless conversation ....
28th Apr 2020, 6:39 AM
U Hariharan
U Hariharan - avatar
0
Aryan Raj that's the first answer given by Alexander Thiem ^^
28th Apr 2020, 6:41 AM
visph
visph - avatar
0
he didn't noticed that visph
28th Apr 2020, 6:42 AM
U Hariharan
U Hariharan - avatar
0
visph i didn't noticed sorry.. next time i will take care of that
28th Apr 2020, 6:43 AM
Aryan Raj
Aryan Raj - avatar
0
Answers still given must be read before posting, then answer only if you have something new to give...
28th Apr 2020, 6:50 AM
visph
visph - avatar
0
U Hariharan Aryan Raj Don't you be the same person with two accounts? You're posting at same time, each one upvote the other, and former answer for later ^^
28th Apr 2020, 6:55 AM
visph
visph - avatar
0
nope visph we aren't the same person I don't know the Aryan Raj
28th Apr 2020, 6:57 AM
U Hariharan
U Hariharan - avatar