C/C++ program to find a number is odd or even

In this tutorial, you will learn how we can find a number is even or odd. Understanding this topic u should have some basic knowledge of C/C++ language. We will see some different code of C/C++ program to find a number is odd or even.

What is an Even Number?

Any integer (never a fraction) that can be divided exactly by 2. The last digit is 0, 2, 4, 6 or 8. Example: -128, 20, 2, 8 and 0 are all even numbers.

What is an Odd Number?

Any integer that cannot be divided exactly by 2 is an odd number. The last digit is 1, 3, 5, 7 or 9. Example: – 121,21, 37, 5 and 29 are all odd numbers.

Even Odd Number line

Program to check Even and odd Number using if-else condition

CC++
#include <stdio.h>
#include <conio.h>
int main()
{
    int number;                 //Variable Declaration
    
    printf("Enter a number: ");
    scanf("%d",&number);
    
    if(number%2==0)
    {
        printf("\n\tNumber %d is Even",number);
    }
    else
    {
        printf("\n\tNumber %d is Odd",number);
    }

    return 0;
}
#include <iostream>
using namespace std;

int main()
{
    int number;                 //Variable Declaration
    
    cout<<"Enter a number: ";
    cin>>number;
    
    if(number%2==0)
    {
        cout<<"\n\tNumber is Even";
    }
    else
    {
        cout<<"\n\tNumber is Odd";
    }

    return 0;
}

Read more : star half pyramid pattern code in c

Explanation for this code:

Number is entered by the user that is stored into the variable number. After than number is checked wheather it is exactly divisible by 2 or not.

If the number is divisible by 2 then it is even number otherwise it is an odd number.

C/C++ program for an odd-even number using AND operator

CC++
#include <stdio.h>
#include <conio.h>
using namespace std;

int main() 
{
    
    int res, number;
    
    printf("Enter a number: ");
    scanf("%d",&number);
    
    
    res = number & 1;
       
    if(res == 0)
    {
        printf("%d is Even Number",number);
    }
    else
    {
       printf("%d is odd Number",number);
    }
    
    return 0;
}
#include<iostream>
using namespace std;

int main() 
{
    
    int res, number;
    
    cout<<"Enter a number: ";
    cin>>number;
    
    
    res = number & 1;
       
    if(res == 0)
    {
        cout<<number<<" is Even Number";
    }
    else
    {
       cout<<number<<" is odd Number";
    }
    
    return 0;
}

Even or Odd number program using the ternary operator in C/C++

CC++
#include <stdio.h>
#include <conio.h>

int main() 
{
    
    int number;
    
    printf("Enter an number: ");
    scanf("%d", &number);
    
    (number % 2 == 0) ? printf("%d is even.", number) : printf("%d is odd.", number);
    
    return 0;
}
#include <iostream>
using namespace std;

int main() 
{
    
    int number;
    
    cout<<"Enter an number: ";
    cin>>number;
    
    (number % 2 == 0) ? cout<<"Number is even." : cout<<"Number is odd.";
    
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *