C program to check even or odd





How to check a number is odd or even using c

#include<stdio.h>

int main(){

    int number;
  
    printf("Enter any integer: ");
    scanf("%d",&number);

    if(number % 2 ==0)
         printf("%d is even number.",number);
    else
         printf("%d is odd number.",number);
  
    return 0;

}

Sample output:
Enter any integer: 5
5 is odd number.



C program to check a given number is odd or even without using any conditional operators?

#include <stdio.h>
int main(){         
int num;
scanf("%d",&num);

if(num&1)
         printf("odd");
    else
         printf("even");

    return 0;
}


Algorithm:

An even number is defined as a number divisible by two, while an odd number is one that is not divisible by two.

Example of even numbers: 0,2,4,8,9,10 etc.
Example of odd numbers: 1, 3,5,7,9 etc.



No comments: