0
How to create a even odd detecting program in c
42 Respuestas
+ 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.
+ 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...
+ 2
if (c%2==0)
{
//even
}else
{
//odd
}
+ 2
Use simple if else as n%2==0 even...else odd
+ 1
Hint:Check if given number divided by 2 results in remainder being 0 ,if yes then it is even otherwise odd
+ 1
Mas Has "if you see closely" you wrote char for char the exact same things than Amit singh ^^
+ 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....
+ 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
+ 1
Kunsh-Tyagi can you edit your question to solved please!!!
0
@Abhay i wanted to know how to give integers as an input
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?)
0
Kunsh-Tyagi read your question
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)
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 ....
0
Aryan Raj that's the first answer given by Alexander Thiem ^^
0
he didn't noticed that visph
0
visph i didn't noticed sorry..
next time i will take care of that
0
Answers still given must be read before posting, then answer only if you have something new to give...
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 ^^