Uncategorized

C++ program to check whether a number is even or odd|6 ways

C++ program to check whether a number is even or odd|6 ways

In this tutorial, we will discuss the C++ program to check whether a number is even or odd|6 ways

In this post, we are going to learn how to check whether the given numbers is even or odd using different 6 ways in C++ language

 

Check whether a number is even or odd – using standard method

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num=78;
    //using modular operator
    if((num%2)==0){
  cout<<num<<" is an Even number";
}else{
 cout<<num<<" is an odd number";
}
getch();
    return 0;
}

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

78 is an Even number

 

In this program,

  • integer variables num is declared and initialized
  • The given number is tested whether it is odd or even
  • Then,the program is displayed the output of using cout<<

 

Check whether a number is even or odd – using user input

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num;
    cout<<"Enter a integer number: ";
   cin>>num;//read the input from the user
    //using modular operator
    if((num%2)==0){
  cout<<num<<" is an Even number";
}else{
 cout<<num<<" is an odd number";
}
getch();
    return 0;
}

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

case 1

Enter a integer number: 459
459 is an odd number

case 2

Enter a integer number: 964
964 is an even number

 

Approach

  • integer variables num is declared.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using cin>> and store the variable num.
  • The given number is tested whether it is odd or even
  • Then,the program is displayed the output of using cout<<

Check whether a number is even or odd – using switch case

Program 3

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num;
   /* int num1=450;
  int num2=561;*/
  printf("Enter any number to check odd or even\n");
  scanf("%d",&num);//read the input from the user
switch(num%2){
case 0:
    printf("%d is an even number",num);
  break;
case 1:
    printf("%d is an odd number",num);
}
getch();
    return 0;
}

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

Enter any number to check odd or even
1234
1234 is an even number

 

Approach

  • integer variables num is declared.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using cin>> and store the variable num.
  • The given number is tested whether it is odd or even using switch case
  • Then,the program is displayed the output of using cout<<

check whether a number is even or odd – using function

Program 4

#include <iostream>
#include <conio.h>
using namespace std;

int findOddeven(int);//function prototype
int main()
{
    int num;
   cout<<"Enter a integer number: ";
    cin>>num;
  //read input from the user
findOddeven(num);//function call
getch();
    return 0;
}

findOddeven(int num){//function definition
if((num%2)==0){
  cout<<num<<" is an Even number";
}else{
 cout<<num<<" is an Odd number";
}

}

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

case 1

Enter a integer number: 1110
1110 is an Even number

case 2

Enter a integer number: 1111
1111 is an odd number

 

Approach

  • Declare a function named as findOddeven(int); with parameter
  • integer variables num is declared.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using cin>> and store the variable num.
  • Define the function  findOddeven(); to find whether the number is odd or even
  • Call the function to produce output
  • finally, the program displays the output using cout<< function

 

Check whether a number is even or odd – using pointer

Program 5

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num,*ptr;//declare the pointer and normal variable
    cout<<"Enter number to check for odd/even\n";
    cin>>num;//read input from the user
    ptr=&num;//assign address of num variable to pointer variable

    if(*ptr %2 ==0)//check value for even
        cout<<num<<" is an even";
    else
         cout<<num<<" is an odd";
getch();

    //printf("Hello world!\n");
    return 0;
}

 

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

Case 1

Enter number for check odd/even
7898
7898 is an even

 

Case 2

Enter number for check odd/even
4571
4571 is an odd

 

Approach

  • integer and pointer variables num, *ptr are declared.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using cin>> and store the variable num.
  • The address of variable num is assigned to pointer variable(*ptr)
  • The given number is tested whether it is odd or even using pointer variable
  • Then,the program is displayed the output of using cout<<

 

 

Check whether a number is even or odd – using recursion

Program 6

#include <iostream>
#include <conio.h>
using namespace std;

int findOddeven(int);
int main()
{
    int num;
   cout<<"Enter a integer number: ";
    cin>>num;
  //read input from the user

  if(findOddeven(num)){
  cout<<num<<" is an Even number";
}else{
 cout<<num<<" is an Odd number";
}

getch();
    return 0;
}

int findOddeven(int num){//function definition
if(num==0){
  return 1;
}else if(num==1){
  return 0;
}
else if(num<0){
  return findOddeven(-num);
}
else{
  return findOddeven(num-2);

}
}

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

Case 1

Enter a integer number: 458
458 is a even number

 

Case 2

Enter a integer number: 4581
4581 is a odd number

 

Similar post

Python program to check whether a number is even or odd

Java program to check whether a number is even or odd

C program to check whether a number is even or odd

 

 

Suggested for you

Data type in C++ language

Variable in C++ language

Operator in C++ language

While loop in C++ language

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

4 weeks ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

4 weeks ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.