Write a C program that reads one integer at a time from stdi

Write a C program that reads one integer at a time from stdin and uses a function to determine if each integer contains an even or odd number of 1 bits. Stop on EOF. Output to stdout and end output for each integer with a newline. Output: \"even\" or \"odd\" for each input integer.

Solution

#include <stdio.h>
int checkNum(int n){
if(n %2 == 0){
return 1;
}
else{
return 0;
}
}
int main(void) {
   int n;
       printf(\"Enter a number(0 to quit): \");
   scanf(\"%d\", &n);

   while(n != 0){
   if(checkNum(n)){
   printf(\"Even\ \");
   }
   else{
   printf(\"Odd\ \");
   }
       printf(\"Enter a number(0 to quit): \");
   scanf(\"%d\", &n);

   }
   return 0;
}

Output:

Enter a number(0 to quit): 8

Even
Enter a number(0 to quit): 5

Odd
Enter a number(0 to quit): 0

 Write a C program that reads one integer at a time from stdin and uses a function to determine if each integer contains an even or odd number of 1 bits. Stop o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site