w3resource logo
C Programming Exercises

C Exercises: Check the given number is even or odd

C Function : Exercise-4 with Solution

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

Sample Solution:

C Code:

#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("\n\n Function : check the number is even or odd:\n");
	printf("------------------------------------------------\n");	     
    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.\n\n");
    }
    else
    {
        printf("The entered number is even.\n\n");
    }
    return 0;
}

Sample Output:

 Function : check the number is even or odd:
------------------------------------------------
Input any number : 5
The entered number is odd.

C Programming Code Editor:

#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("\n\n Function : check the number is even or odd:\n");
	printf("------------------------------------------------\n");	     
    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.\n\n");
    }
    else
    {
        printf("The entered number is even.\n\n");
    }
    return 0;
}

Improve this sample solution and post your code through Disqus.