if-else statement
if
statement is most commonly used with the following format:
where
else
part is optional.
In this case, either of the two statements are executed depending upon the value of the expression. If the expression has a non-zero value (i.e., if the expression is true), then statement1 will be executed. Otherwise (i.e., if expression is false), statement2will be executed.
Example:
Given here is a program which reads a number and then prints whether the given number is even or odd.
Given here is a program which reads a number and then prints whether the given number is even or odd.
Note: If we divide a number by 2 and if the remainder is 0 then the number is EVEN else the number is ODD
1
2
3
4
5
6
7
8
9
10
11
12
|
#include<iostream.h>
void main()
{
int n;
cout << "Enter a number :";
cin >> n;
if(n%2 == 0)
cout << "The number is EVEN";
else
cout << "The number is ODD";
}
|
0 comments:
Post a Comment