-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathlargest-and-smallest
46 lines (36 loc) · 1.13 KB
/
largest-and-smallest
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
#include <iostream>
using namespace std;
int main()
{
cout << "\n\nWelcome to Studytonight :-)\n\n\n";
cout << " ===== Program to find the Largest and the Smallest number among 3 numbers ===== \n\n";
//variable declaration
int n1, n2, n3, smallest, largest;
//taking input from the command line (user) all at once
cout << " Enter the three numbers : \n\n\n";
cin >> n1 >> n2 >> n3;
//assign initial value for comparison (as the undefined variables store a random value)
smallest = n1;
largest = n2;
//logic to find the Smallest and the Largest number - Remember, each variable stores only the latest value inserted into it.
if (n2 < smallest)
{
smallest = n2;
}
if (n3 < smallest)
{
smallest = n3;
}
if (n3 > largest)
{
largest = n3;
}
if (n2 > largest)
{
largest = n2;
}
cout << "\n\n The Smallest number among ( " << n1 << ", " << n2 << ", " << n3 << " ) is : " << smallest;
cout << "\n\n The Largest number among ( " << n1 << ", " << n2 << ", " << n3 << " ) is : " << largest;
cout << "\n\n\n";
return 0;
}