Integer comparison: Difference between revisions

From Rosetta Code
Content added Content deleted
(basic data operation; provided C++ and Fortran)
 
m (minor formatting correction)
Line 12: Line 12:
int a, b;
int a, b;
std::cin >> a >> b;
std::cin >> a >> b;

// test for less-than
// test for less-than
if (a < b)
if (a < b)
std::cout << a << " is less than " << b << "\n";
std::cout << a << " is less than " << b << "\n";

// test for equality
// test for equality
if (a == b)
if (a == b)
std::cout << a << " is equal to " << b << "\n";
std::cout << a << " is equal to " << b << "\n";

// test for greater-than
// test for greater-than
if (a > b)
if (a > b)
Line 45: Line 45:
write(*, *) a, ' is greater than ', b
write(*, *) a, ' is greater than ', b
end if
end if
c

end
end

Revision as of 09:24, 31 March 2007

Task
Integer comparison
You are encouraged to solve this task according to the task description, using any language you may know.

Basic Data Operation
This is a basic data operation. It represents a fundamental action on a basic data type.

You may see other such operations in the Basic Data Operations category, or:

Integer Operations
Arithmetic | Comparison

Boolean Operations
Bitwise | Logical

String Operations
Concatenation | Interpolation | Comparison | Matching

Memory Operations
Pointers & references | Addresses

Get two integers from the user, and then output if the first one is less, equal or greater than the other. Test the condition for each case separately, so that all three comparison operators are used in the code.

C++

#include <iostream>
#include <istream>
#include <ostream>

int main()
{
  int a, b;
  std::cin >> a >> b;

  // test for less-than
  if (a < b)
    std::cout << a << " is less than " << b << "\n";

  // test for equality
  if (a == b)
    std::cout << a << " is equal to " << b << "\n";

  // test for greater-than
  if (a > b)
    std::cout << a << " is greater than " << b << "\n";
}

Fortran

      program compare
      integer a, b
      read(*,*) a, b
c
c     test for less-than
      if (a .lt. b) then
        write(*, *) a, ' is less than ', b
      end if
c
c     test for equality
      if (a .eq. b) then
        write(*, *) a, ' is equal to ', b
      end if
c
c     test for greater-than
      if (a .gt. b) then
        write(*, *) a, ' is greater than ', b
      end if
c
      end