0
Using C++ take 3 values from user and show which value is smaller and which is larger using if() conditions...
I am new in C++. And want to know how this happens
3 ответов
+ 4
if(thisSoundsLikeHomeWork) {
cout << "I no help" << endl; }
I can tell you "how" it happens. I will not show you "how" to do it though.
How it happens.
If statements compare an expression to a true condition. The format of an if statement is as follows:
if( expression ) { action }
Consider the following if statements
bool a = true;
if(a == true) { //do stuff }
// expression evaluates as true.
if(a == false) {// do stuff }
//expression evaluates to false.
In the above example only one of the statements will perform an action (or 'do stuff') because only one expression is evaluated to be true. The first example.
Note: the examples can also be shortened to if(a) and if(!a).
We can also consider the following examples:
bool a = true;
if(a == true)
if(a !=false)
Both of these examples are evaluated to be true. Note the use of the not equal to operator !=
now using this information we can compare almost anything to a true or false condition using logical operators. In your case greater than and less than will come in handy.
Hope this helps.
+ 3
int a,b,c;
cout<<"Enter three numbers";
cin>>a>>b>>c;
if(a>b && a>c)
cout<<"%d is greater"<<a;
else if(b>a && b>c)
cout<<"%d is greter"<<b;
else
cout<<"%d is greater"<<c;
+ 3
Steps to solve, flexible for any amount of numbers, try it:
* Declare an array of size 3
* Use a for loop to go through the array, for each element let cin (the users input) fill the array.
* Then, create temp variables.
Called: min, max;
Set them both equal to array[0]; (1st element).
* Use another for loop to go through the array
comparing the min and max with the next element in the array. Compare with an if statement.
* When a new min or max is found, set the min and max variable to the current array element that you've just checked.
* Output the resulting min/max variables.