w3resource logo


C Programming Exercises

C Exercises: Check the given number is even or odd

Secondary Nav

C Function : Exercise-4 with Solution

Write a program in C to check a given number is even or odd using the function.
Test Data :
Input any number : 5
Expected Output :

The entered number is odd. 

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;
}

C Practice online:

#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.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number