[Solved] How to get a both numbers are equal or this one is greater?


Apparently you don’t know how to use if else correctly in c++. You can look them up e.g. here.

One correct way your program could look like:

#include <iostream>    
#include <string>    
using namespace std;
int main(int argc, char* argv[])
{
    int a, b, c = 0;
    cout << "Enter value of 1st variable: ";
    cin >> a;
    cout << "Enter value of 2nd variable: ";
    cin >> b;


    if (a == b){
        cout << "Both are equal";
    } else {
        if (a > b) {
            c = a;
        } else if (a < b){ //<-- As indicated by @Thomas Matthews you don't actually need the third check here
            c = b;
        }
        cout << "Greater value is " << c;
    }
} 

Other ways to write this:

if (a == b){
    cout << "Both are equal";
}else if (a > b) {
    cout << "Greater value is " << a;
} else {        
    cout << "Greater value is " << b;
}

OR (less obvious):

int t = a - b;
switch ((t > 0) - (t < 0)){ //<-- effectively computing the sign of t
    case -1: cout << "Greater value is " << b; break;
    case 0: cout << "Both are equal"; break;
    case 1: cout << "Greater value is " << a; break;
}

3

solved How to get a both numbers are equal or this one is greater?