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

C++ examples for Operator:Arithmetic Operator

Description

Finding the largest of two integers without comparing them.

Demo Code

#include <iostream>

int main(){//from w w  w.  j  a  v  a2  s  . c o  m
  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;
}

Result


Related Tutorials