Computer Science, asked by Anonymous, 1 year ago

*️⃣ Write a program, input number and check if it is even or odd number. *️⃣

*️⃣ Question of C++ *️⃣

❎ No Spams ‼️

Answers

Answered by DevanshiAgnihotri
4

Example 1: Check Whether Number is Even or Odd using if else

#include <iostream>

using namespace std;

int main()

{

   int n;

   cout << "Enter an integer: ";

   cin >> n;

   if ( n % 2 == 0)

       cout << n << " is even.";

   else

       cout << n << " is odd.";

   return 0;

}

Output

Enter an integer: 23

23 is odd.

In this program, if..else statement is used to check whether n%2 == 0 is true or not. If this expression is true, n is even if not n is odd.

You can also use ternary operators ?: instead of if..else statement. Ternary operator is short hand notation of if...else statement.

Example 2: Check Whether Number is Even or Odd using ternary operators

#include <iostream>

using namespace std;

int main()

{

   int n;

   cout << "Enter an integer: ";

   cin >> n;

   

   (n % 2 == 0) ? cout << n << " is even." :  cout << n << " is odd.";

   

   return 0;

}


Anonymous: Tysm bro
DevanshiAgnihotri: i am a girl bro
DevanshiAgnihotri: dp is of alan walker
DevanshiAgnihotri: tysm for brainliest
Anonymous: Ur most wlcm :)
Anonymous: Yaa it's Alan
DevanshiAgnihotri: u r also fan of him
Anonymous: Yup
Answered by BrainlyEmpire
9

u can also do it in Java programs

Attachments:
Similar questions