Here are C++ programs to check whether a given number is even or odd. First program use modulus operator second program checks even or odd without using modulus operator, third program use Bitwise AND operator and fourth program use conditional operator and in fifth program we use class to check whether the given number is odd or even.
C++ programs to check whether a given number is even or odd
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter the number ";
cin>>num;
if(num%2==0)
cout<<"Number is even ";
else
cout<<"Number is odd ";
}
using namespace std;
int main()
{
int num;
cout<<"Enter the number ";
cin>>num;
if(num%2==0)
cout<<"Number is even ";
else
cout<<"Number is odd ";
}
C++ programs to check whether a given number is even or odd without using modulus operator
#include<iostream>
using namespace std;
int main()
{
int num;
float num1;
cout<<"Enter the number ";
cin>>num;
num1=num;
if(num/2==num1/2)
cout<<"Number is even ";
else
cout<<"Number is odd ";
}
using namespace std;
int main()
{
int num;
float num1;
cout<<"Enter the number ";
cin>>num;
num1=num;
if(num/2==num1/2)
cout<<"Number is even ";
else
cout<<"Number is odd ";
}
C++ programs to check whether a given number is even or odd using Bitwise AND operator
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter the number ";
cin>>num;
if((num&1)==0)
cout<<"Number is even ";
else
cout<<"Number is odd ";
}
using namespace std;
int main()
{
int num;
cout<<"Enter the number ";
cin>>num;
if((num&1)==0)
cout<<"Number is even ";
else
cout<<"Number is odd ";
}
C++ programs to check whether a given number is even or odd using conditional operator
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter the number ";
cin>>num;
num%2==0?cout<<"Number is even ":cout<<"Number is odd ";
}
using namespace std;
int main()
{
int num;
cout<<"Enter the number ";
cin>>num;
num%2==0?cout<<"Number is even ":cout<<"Number is odd ";
}
C++ programs to check whether a given number is even or odd using class
#include<iostream>
using namespace std;
class evenodd
{
private:
int num;
public:
void getnum()
{
cout<<"Enter the number : ";
cin>>num;
}
void check()
{
if(num%2==0)
cout<<"Number is even ";
else
cout<<"Number is odd ";
}
};
int main()
{
evenodd obj;
obj.getnum();
obj.check();
return 0;
}
using namespace std;
class evenodd
{
private:
int num;
public:
void getnum()
{
cout<<"Enter the number : ";
cin>>num;
}
void check()
{
if(num%2==0)
cout<<"Number is even ";
else
cout<<"Number is odd ";
}
};
int main()
{
evenodd obj;
obj.getnum();
obj.check();
return 0;
}
In c++ programming, C++ variables is the most important part to get started. We should read in detail about variables if we want to become a professional programmer.
ReplyDeleteI appreciate your work.
ReplyDelete