write a function which ask a user to type a number, it then tells whether the number input is Even Or Odd?
This is a discussion on Program Of Even Or Odd within the C Programming forums, part of the General Programming Boards category; write a function which ask a user to type a number, it then tells whether the number input is Even ...
write a function which ask a user to type a number, it then tells whether the number input is Even Or Odd?
Ok, easy stuff... what have you got so far... Post your code and lets see what we can do...
Use the modulo % operator.
Code:#include<stdio.h> #include<conio.h> void even(void); void main(void) { clrscr(); even(); even(); getche(); } void even(void) { int x; printf("\n\nType A Number :"); scanf("%d",&x); if(x%2==0) {printf("\n\nYou Typed Even Number");} else { printf("\n\nYou Typed Odd Number"); } }
Hmmm... try it like this....
Code:#include <stdio.h> #include <conio.h> // warning, not standard C! int IsOdd(int Num) { return Num & 1; } // odd integers have 1 in bit 0 int main ( void ) { int uNum = 0; clrscr(); // warning, not standard C! printf("Enter a number : "); scanf("%d", &uNum); if ( IsOdd( uNum ) ) printf(" %d is an odd number", uNum); else printf(" %d is an even number", uNum); return 0; }
hmmmmmmmmmm