Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

can someone please correct me ? i have to put in order 3 any numbers and i dont know what i do wrong.

+5 votes
asked Feb 20, 2021 by slendy (180 points)
#include <iostream>
using namespace std;
int main () {
int a, b, c;
cin>>a>>b>>c;
if (a=>b=>c) {
     cout<<c;
    cout<<" ";
     cout<<b;
    cout<<" ";
     cout<<a;
    cout<<" ";
}
 if else (a=>c=>b) {
      cout<<b;
    cout<<" ";
     cout<<c;
    cout<<" ";
     cout<<a;
    cout<<" ";
 }
 if  (b=>a=>c) {
      cout<<c;
    cout<<" ";
     cout<<a;
    cout<<" ";
     cout<<b;
    cout<<" ";
 }
 if else (b=>c=>a) {
      cout<<a;
    cout<<" ";
     cout<<c;
    cout<<" ";
     cout<<b;
    cout<<" ";
 }
  if (c=>a=>b) {
     cout<<b;
    cout<<" ";
     cout<<a;
    cout<<" ";
     cout<<c;
    cout<<" ";
  }
  if else (c=>b=>a) {
       cout<<a;
    cout<<" ";
     cout<<b;
    cout<<" ";
     cout<<c;
    cout<<" ";

  }
  return 0;
}

6 Answers

–1 vote
answered Feb 21, 2021 by Shubham-09-09 (120 points)
#include <iostream>
using namespace std;
int main ()
{
    int a, b, c;
    cin>>a>>b>>c;
    if(a>b && b>c )
    {
        cout<<a<<endl<<b<<endl<<c;
    }
    else if(a>b && b<c )
    {
        cout<<a<<endl<<c<<endl<<b;
    }
    else if(b>a &&  c>a)
    {
        cout<<b<<endl<<c<<endl<<a;
    }
    else if(b>a  && c<a)
    {
        cout<<b<<endl<<a<<endl<<c;
    }
    else if(c>a && a>b)
    {
        cout<<c<<endl<<a<<endl<<b;
    }
    else if(c>a && a<b)
    {
        cout<<c<<endl<<b<<endl<<a;
    }

}

this is a kind of format you can use to compare values where > should be before = and for two conditions in one step use &&.i just created basic now you can edit according to you and condition is else if not if else
commented Feb 25, 2021 by Peter Minarik (84,720 points)
edited May 19, 2021 by Peter Minarik
This is wrong.

I can easily provide input that would break your logic of returning numbers in ascending orders. E.g.: 5, 3, 7. It would return 5, 7, 3; which is clearly not in ascending order.

The problem is that you did apply the transitivity of the less than operator wrong. You should have done a > c && c > b (, instead of a > b && b < c).

Please, read https://en.wikipedia.org/wiki/Inequality_(mathematics)#Transitivity
+1 vote
answered Feb 22, 2021 by Peter Minarik (84,720 points)
edited Feb 25, 2021 by Peter Minarik

In Mathematics you can make expressions like a >= b >= c, but in most programming languages, you cannot. You should do

if (a >= b && b >= c)
{
    // ...
}

instead.

Also, in the last condition, you have the if and the else in the wrong order:

if (c => a && a => b)
{
    // ...
}
else if (c => b && b => a)
{
    // ...
}

An alternative is to keep only the else, and you remove the if and the condition, as c >= b >= a is the only remaining case, so the else should cover it, no need to check whether this is happening with the if statement.

After these changes, I think you code would work fine.

Also, I'd suggest looking into various sorting algorithms. You'll make a good use of them.

https://en.wikipedia.org/wiki/Sorting_algorithm#Popular_sorting_algorithms

0 votes
answered Feb 23, 2021 by Nishchay Tayal (150 points)
#include <iostream>

using namespace std;

int main () {
int a, b, c;
cin >> a ;
cin >> b ;
cin >> c ;
if (a>=b>=c) {
     cout<<c;
    cout<<" ";
     cout<<b;
    cout<<" ";
     cout<<a;
    cout<<" ";
}
  else if (a>=c>=b) {
      cout<<b;
    cout<<" ";
     cout<<c;
    cout<<" ";
     cout<<a;
    cout<<" ";
 }
 else if (b>=a>=c) {
      cout<<c;
    cout<<" ";
     cout<<a;
    cout<<" ";
     cout<<b;
    cout<<" ";
 }
 else if (b>=c>=a) {
      cout<<a;
    cout<<" ";
     cout<<c;
    cout<<" ";
     cout<<b;
    cout<<" ";
 }
  else if (c>=a>=b) {
     cout<<b;
    cout<<" ";
     cout<<a;
    cout<<" ";
     cout<<c;
    cout<<" ";
  }
   else if  (c>=b>=a) {
       cout<<a;
    cout<<" ";
     cout<<b;
    cout<<" ";
     cout<<c;
    cout<<" ";

  }
  return 0;
}
0 votes
answered May 18, 2021 by Vikas Kumar (190 points)
There is a way to get a proper answer that is write the program in a nested if- else multiple times, means you have to one if- else in another if- else multiple times. The program is shown below:

# include <iostream>

using namespace std;

int main () {

int a, b, c;

cin>>a>>b>>c;

if (a > b) {

if (a > c) {

if (c > b) {

cout<<""<<b<<" < "<<c<<" < "<<a<<"";

}

else {

cout<<""<<c<<" < "<<b<<" < "<<a<<"";

}

}

else {

cout<<""<<b<<" < "<<a<<" < "<<c<<"";

}

}

else {

if (b > c) {

if (c > a) {

cout<<""<<a<<" < "<<c<<" < "<<b<<"";

}

else {

cout<<""<<c<<" < "<<a<<" < "<<b<<"";

}

}

else {

cout<<""<<a<<" < "<<b<<" < "<<c<<"";

}

}

return 0;

}

I hope this would be helpful.
0 votes
answered Sep 9, 2023 by Ananya K.J (300 points)
#include <iostream>

using namespace std;
int big(int x,int y)
{
   return(x>y)?x:y;
}

int main()
{
   int a,b,c,d,s;
   cin>>a>>b>>c>>d;
   s=big(big(big(a,b),c),d);
   cout<<s;

    return 0;
}

ur code is very lengthy ...u can use this for reference.
0 votes
answered Sep 10, 2023 by josue (140 points)

#include <iostream>

using namespace std;
int main () {
int a, b, c;
    
    cin >> a >> b >> c;
        
        if (a>=b && b>=c) {
            cout<<c;
            cout<<" ";
            cout<<b;
            cout<<" ";
            cout<<a;
            cout<<" ";
        }
        
   return 0;
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...