Write C++ programs for the following.
a. Input a number from the user and check if it is an even or odd number
Answers
Answer:
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.
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;
}
Output :-
Enter an integer: 23
23 is odd.
hope it helps u
mark me as brainliest
Explanation:
Answer:
#include <iostream>
using namespace std;
int main()
{
/* one variable is required*/
/* user have to enter value and it will later be checked*/
int a;
cout<<"enter the number";
cin>>a;
if(a%2==0)
{
printf("it is even");
}
else
{
printf("it is odd");
}
return 0;
}
The output of the code:-
enter the number 6
it is even
Check Even or Odd using functions
#include <iostream>
using namespace std;
int oddEven(int); // function prototype
int main()
{
/* one variable is required*/
/* user have to enter value and it will later be checked*/
int a;
cout<<"enter the number";
cin>>a;
oddEven(a); //call the function
return 0;
}
int oddEven(int a){
if(a%2==0)
{
printf("it is even");
}
else
{
printf("it is odd");
}
}
The output of the code:-
Enter the number 5
it is odd