Integer Calculations


Program Listing

IntCalcs.cpp

/****************************************************************************
*                                                                           *
*   Program:    IntCalcs                                                    *
*                                                                           *
****************************************************************************/

#include <iostream>
#include <string>

using namespace std;

int main() {

//  Integer division

    int i = 50/16;
    int j = 50/17;
    
    cout << "50/16 = " << i << endl;
    cout << "50/17 = " << j << endl << endl;

//  Remainder operator

    i = 50 % 16;
    j = 50 % 17;
    
    cout << "50 % 16 = " << i << endl;
    cout << "50 % 17 = " << j << endl << endl;

//  Multiplication has precedence over addition

    i = 2 + 3 * 4;
    j = (2 + 3) * 4;
    
    cout << "2 + 3 * 4 = " << i << endl;
    cout << "(2 + 3) * 4 = " << j << endl << endl;

//  Subtraction is left associative

    i = 10 - 9 - 8;
    j = 10 - (9 - 8);
    
    cout << "10 - 9 - 8 = " << i << endl;
    cout << "10 - (9 - 8) = " << j << endl;
    
    return 0;
}

Sample Run

50/16 = 3
50/17 = 2

50 % 16 = 2
50 % 17 = 16

2 + 3 * 4 = 14
(2 + 3) * 4 = 20

10 - 9 - 8 = -7
10 - (9 - 8) = 9



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:04:24 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/IntCalcs.html

Return to Coms 261 home page