Starting Vocational Training From 1-May-2024 Get Detail


Write a program in C to check a given number is even or odd using the function.


#include <stdio.h>
 
//if the least significant bit is 1 the number is odd and 0 the number is even 
int checkOddEven(int n1)
{   
    return (n1 & 1);//The & operator does a bitwise and,
}

int main()
{
    int n1;
    printf(" Function : check the number is even or odd: ");
    printf("------------------------------------------------ ");         
    printf("Input any number : ");
    scanf("%d", &n1);

    // If checkOddEven() function returns 1 then the number is odd 
    if(checkOddEven(n1))
    {
        printf("The entered number is odd. ");
    }
    else
    {
        printf("The entered number is even. ");
    }
    return 0;
}