Check value

Cpp program to check a number is even or odd using function

Cpp program to check a number is even or odd using function

In this tutorial, we will discuss the Cpp program  to check a number is even or odd using function

In this program, we are going to learn about how to find the odd or even number from given number using function in Cpp language

 

First, we must understand what is even or odd?

What is Even or Odd

When the number is divided by 2 and the balance becomes zero. the above number is called as the even number – eg 2,4,6,8,10

and on the other sides when it is divided by 2 and balance becomes 1 they are called odd numbers or uneven numbers

 

In my previous post, I have explained the various ways to check whether the  number is even or odd in c++ language

However, in this program, we embed the logic of the program to check even or odd numbers in the function

you can embed the logic of the program to check even or odd numbers using any of the following approaches in the function

 

Once This program received the number it will check the given number either odd or even.

After receiving the input from the user, it is stored in the variable of num. then program divides the value of num into 2 and displays the output

Check a number is even or odd using modular operator

Program 1

#include <iostream>
#include <conio.h>

using namespace std;
int find_Oddeven(int);//function prototype

int main()
{
    int num;
    cout << "Enter a number to ceck odd or even" << endl;
    cin>>num;//get input from user
    find_Oddeven(num);//calling the function
    getch();
    return 0;
}

//create function
int find_Oddeven(int num){//function definition
if(num%2==0)
  cout<<num<<"is an even";
else
    cout<<num<<"is an odd";

}

When the above code is executed, it produces the following results

Case 1

Enter a number to check odd or even
44

44 is an even

Case 2

Enter a number to check odd or even
27

27 is an odd

 

Program 2

Check a number is even or odd using the function with return

#include <iostream>
#include <conio.h>

using namespace std;
int find_Oddeven(int);//function prototype

int main()
{
    int num;
    cout << "Enter a number to check odd or even" << endl;
    cin>>num;
    if(num%2==0)
  cout<<num<<"is an even";
else
    cout<<num<<"is an odd";
    find_Oddeven(num);
    getch();
    return 0;
}

//create function
int find_Oddeven(int num){//function definition
return(num%2==0);//return the value

}


When the above code is executed, it produces the following results

Case 1

Enter a number to check odd or even
87

87 is an odd

Case 2

Enter a number to check odd or even
20

20 is an even

 

Check a number is even or odd using the function with bitwise operator

#include <iostream>
#include <conio.h>

using namespace std;
int find_Oddeven(int);//function prototype

int main()
{
    int num;
    cout << "Enter a number to ceck odd or even" << endl;
    cin>>num;//get input from user
    find_Oddeven(num);//calling the function
    getch();
    return 0;
}


//create function
int find_Oddeven(int num){//function definition
if(num&1)
    cout<<num<<"is an odd";
else
    cout<<num<<"is an even";

}


 

Case 1

Enter a number to check odd or even
786

786 is an even

Case 2

Enter a number to check odd or even
89

89 is an odd

 

Check a number is even or odd using the function with conditional operator

#include <iostream>
#include <conio.h>

using namespace std;
int find_Oddeven(int);//function prototype

int main()
{
    int num;
    cout << "Enter a number to check odd or even" << endl;
    cin>>num;//get input from user
    find_Oddeven(num);//calling the function
    getch();
    return 0;
}
int find_Oddeven(int num){//function definition
(num%2==0) ? cout<<num<<"is an even":
    cout<<num<<"is an odd";

}


 

 

When the above code is executed, it produces the following results

Case 1

Enter a number to check odd or even
101

101 is an odd

Case 2

Enter a number to check odd or even
112

112 is an even

 

Check a number is even or odd using the function with division operator

#include <iostream>
#include <conio.h>

using namespace std;
int find_Oddeven(int);//function prototype
int main()
{
    int num; cout << "Enter a number to check odd or even" << endl;
    cin>>num;//get input from user
    find_Oddeven(num);//call the function
    getch();
    return 0;

}
int find_Oddeven(int num){//function definition
    if((num/2)*2==num)
        cout<<num<<" is an even ";
    else
        cout<<num<<" is an odd ";
     }

 

When the above code is executed, it produces the following results

Case 1

Enter a number to check odd or even
789

789 is an odd

Case 2

Enter a number to check odd or even
118

118 is an even

 

Check a number is even or odd using the function with shift operator

#include <iostream>
#include <conio.h>

using namespace std;
int find_Oddeven(int);//function prototype
int main()
{
    int num; cout << "Enter a number to ceck odd or even" << endl;
    cin>>num;//get input from user
    find_Oddeven(num);//calling the function
    getch();
    return 0;

}

//create function 
int find_Oddeven(int num){//function definition
    if(((num>>1)<<1)==num ){
        cout<<num<<"is even ";
    } else{
        cout<<num<<"is odd";
     }
}

When the above code is executed, it produces the following results

Case 1

Enter a number to check odd or even
987

987 is odd

Case 2

Enter a number to check odd or even
456

456 is even

 

Suggested for you

C++ operator

C++ Data type

C++ variable

C++ if statements

C++ function

 

Python program find factorial of a number using recursion
Python program to check a number is even or odd using function
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

20 hours ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago