Im trying to write a program using C that reads an integer and then determines wheather it is odd or even.
I have to use the if-else structure.
Any help will be apprectiated
Im trying to write a program using C that reads an integer and then determines wheather it is odd or even.
I have to use the if-else structure.
Any help will be apprectiated
sounds like homework, i'll give ya a hint . All even numbers are divisible by 2.
-"What we wish, we readily believe, and what we ourselves think, we imagine others think also."PHP Code:
sadf
here's the logic.......
if a number divided by 2 gives remainder 0..it is even else it is odd.....you can make use of % operator to find remainder......try it and if you are stuck then post the code....someone will help
"Service of the poor and destitutes is the service of the God"
Normative Changes to ISO/IEC 9899:1990 in Technical Corrigendum 1
Incompatibilities Between ISO C and ISO C++
It is homework.
Im not looking for someone to post the code.
Just a hint in the right direction.
Ya I think the % operater will work.
Thanks
1. use scanf
2. get the number and use the % operator
3. if it's an even number else print the odd number
here's some psuedocode
prompt user for number
input number
if the number has a remainder of 0 when divided by 2
the number is even
otherwise the number is odd
Just type up the right!
Don't quote me on that... ...seriously
Hot or Not?Code:#include <stdio.h> #include <math.h> int main() { int a, b; printf("print a number:\n"); scanf("%d",&a); /* Enter number here */ b=a%2; if(b!=0) /*if number divided by 2 and there is a remainder then number is odd*/ printf("this number is odd:\n"); else printf("this number is even:\n"); /* prints wheather number is odd or even*/ return 0; }
Just a suggestion, don't put everything inside the main function. Split it up, say getInput and print etc.
Originally Posted by Deadlocked
Well I won't comment on hot or not, but I would say that you don't really need to #include math.h for this version of your program to work. Also, you don't need the extra variable b. You can evalute a % 2 as an argument to printf().
When you get into some more complicated programs, you are likely going to find that you will want a more robust means of getting input from the user than a call to scanf(). If the user does not enter an int (as scanf() is expecting in this case) then the call to scanf() fails. At the very least, if you must use scanf() check the return value as I demonstrated. Later, you can do something like read the user input into a buffer with fgets() and then grab your number with sscanf() or the like.Code:#include <stdio.h> int main(void) { int input; printf("\n\nEnter a number: "); fflush(stdout); if ((scanf("%d", &input)) != 1) { puts("\nError: call to scanf() failed\n"); return 1; } printf("The number %d is %seven.\n", input, ((input % 2) == 0) ? "" : "not "); return 0; }
Anyway it looks not too bad overall.
~/
Last edited by kermit; 10-26-2005 at 04:37 PM.
Thanks for the suggestion but Id to do it my way.
That level of C>>>>>>>>>>>>> my level of C
That level of C isnt hard, he has simply added error checking by checking to see wether or not an input has been received. And the:
Is basicaly an if statement:Code:((input % 2) == 0) ? "" : "not ")
if input%2 == 0 then it would print nothing, if it != 0 then it would print not.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.