76.2k views
3 votes
Using for loop . Input an integer and identify whether it's EVEN OR ODD ( without using modulo operator )

using only . C++ programming

User ZygD
by
5.0k points

1 Answer

3 votes

Answer:

Following are the program in c++ language

#include<iostream> // header file

using namespace std; // namespace

int main()

{

int n1,i; // variable declaration

cout<<"Enter Number: ";

cin>>n1; // input number

for(i=n1;i>1;i=i-2) // check the condition

{

n1 = n1-2; // decrement the value of number by -2

}

if(n1==0) // check if it is 0

cout<<"number is EVEN"<<"\\"; // print even

else

cout<<"number is ODD"<<"\\"; // print odd

return 0;

}

Output:

Enter Number: 45

number is ODD

Again run the program

Enter Number:10

number is EVEN

Explanation:

In this program, we input the number by a user in variable n1. After that, we iterate the for loop and initialize the value of a variable" i" in for loop and check the condition.

In each iteration, the variable n1 is decrement by 2 and store it n1.

Finally, check if n1==0 then it print number is EVEN otherwise it print number is ODD.

User Jostein
by
6.1k points