This program will compute whether or not there is an odd or even number of ones. User can only input integer numbers. Set up for loop for bit comparison.
Solution:
//how to determine if a number is odd or even.
//C Program to check whether the given Number is Even or Odd.
//Write a C Program to check if number is odd or even number.
//C Program to print whether or not there is an odd or even number.
#include <stdio.h>
int initial;
int counter = 0;
int mod;
int result;
int comparator = 1;
int main () {
/* Read in value of which to compute factorial */
printf("This program will compute whether or not there is an odd or even number of ones.\nPlease enter a number: ");
scanf("%d", &initial);
printf("\n");
/*Check for invalid input*/
while (initial < 0) {
printf("This input is not valid. Only positive integers up to 2^31 are accepted\nEnter N: ");
scanf("%d", &initial);
printf("\n");
}
/* set up for loop for bit comparison */
for(; initial > 0; initial = initial >> 1) {
counter += (initial&1);
}
/* Check if odd number or even number of 1s and print answer*/
if ((counter%2) == 1) {
printf("Your value is odd\n");
}
else {
printf("Your value is even\n");
}
return 0;
}
This C Program to Check whether the given Number is Even or Odd Ones.
Check whether the given Number is Even or Odd |
//how to determine if a number is odd or even.
//C Program to check whether the given Number is Even or Odd.
//Write a C Program to check if number is odd or even number.
//C Program to print whether or not there is an odd or even number.
#include <stdio.h>
int initial;
int counter = 0;
int mod;
int result;
int comparator = 1;
int main () {
/* Read in value of which to compute factorial */
printf("This program will compute whether or not there is an odd or even number of ones.\nPlease enter a number: ");
scanf("%d", &initial);
printf("\n");
/*Check for invalid input*/
while (initial < 0) {
printf("This input is not valid. Only positive integers up to 2^31 are accepted\nEnter N: ");
scanf("%d", &initial);
printf("\n");
}
/* set up for loop for bit comparison */
for(; initial > 0; initial = initial >> 1) {
counter += (initial&1);
}
/* Check if odd number or even number of 1s and print answer*/
if ((counter%2) == 1) {
printf("Your value is odd\n");
}
else {
printf("Your value is even\n");
}
return 0;
}
This C Program to Check whether the given Number is Even or Odd Ones.