C++ Arithmetic Operator Finding the largest of two integers without comparing them.

Description

C++ Arithmetic Operator Finding the largest of two integers without comparing them.

#include <iostream>

int main(){//from ww w . j  ava 2 s  .  com
  long a {};
  long b {};

  std::cout << "Enter a positive integer: ";
  std::cin >> a;
  std::cout << "Enter another different positive integer: ";
  std::cin >> b;

  long larger {(a*(a / b) + b*(b / a)) / (a / b + b / a)};

  long smaller {(b*(a / b) + a*(b / a)) / (a / b + b / a)};

  std::cout << "The larger integer is " 
            << larger << ".\n" << "The smaller integer is " << smaller << "." << std::endl;
}



PreviousNext

Related