C++ Arithmetic Operator exact division of one integer by another

Description

C++ Arithmetic Operator exact division of one integer by another

#include <iostream>
using std::cin;//from w ww  .  j  av  a  2  s.  co m
using std::cout;
using std::endl;

int main() {
  int value1 = 0;
  int value2 = 0;
  int remainder = 0;

  cout << "Please input two positive integers, separated by a space: ";
  cin >> value1 >> value2;
  cout << endl;

  if((value1 <= 0) || (value2 <= 0)) {      // Valid input?
    cout << "Sorry - positive integers only." << endl;
    return 1;
  }

  if(value1 < value2) { // Ensure that value1 is not smaller than value2
    int temp = value1;  // ... swap if necessary
    value1 = value2;
    value2 = temp;
  }

  remainder = value1 % value2;

  if(0 == remainder)
    cout << value2 << " divides into " << value1 << " exactly. " << endl;
  else
    cout << value1 << " is not exactly divisible by " << value2 << endl;

  return 0;
}



PreviousNext

Related