-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path06_if_Statements.cpp
More file actions
53 lines (46 loc) · 1.21 KB
/
06_if_Statements.cpp
File metadata and controls
53 lines (46 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;
/*Conditions and IF Statements
Using different actions for different decisions using conditions
Syntax:
if (condition){block of codes if the condition is TRUE}
*/
int main(){
//Example: Checking whether a number is EVEN or ODD
int number;
cout << "Enter a number: ";
cin >> number;
if (number%2 == 0) cout << "Even\n";
if (number%2 != 0) cout << "Odd\n";
/*
if (condition){...}
else {...}
*/
//EVEN or ODD using if else
//if (number%2 == 0) cout << "Even\n";
//else cout << "Odd\n";
//Example: Checking whether a number divisible by 6
int num;
cout << "Enter a number: ";
cin >> num;
if ((num%2 == 0) && (num%3 == 0)) cout << num << " is divisible by 6. \n";
else cout << num << " is not divisible by 6. \n";
/*
if (condition){...}
else if (condition) {...}
else {...}
*/
//Example:
int time_h;
cout << "Time: \n";
cin >> time_h;
if (time_h <= 10){
cout << "Good Morning!\n";
}
else if (time_h < 20 && time_h > 10){
cout << "Good Day!\n";
}
else {
cout << "Good Evening!\n";
}
}