Simple Calculations


Program Listing

SimpleCalcs.cpp

/************************************************************************
*                                                                       *
*   Program:    Simple Calcs                                            *
*                                                                       *
************************************************************************/

#include <iostream>
#include <string>

using namespace std;

int main() {

//  Do an integer calculation

    int a;                                      // An integer
    int b;                                      // Another integer
    cout << "Enter two integers:  " << flush;
    cin >> a >> b;                              // Read two integers
    cout << "The sum is " << a + b << endl;     // Print their total

//  Do a floating-point calculation

    cout << endl;
    float x;                                    // A float
    cout << "Enter a floating-point number:  " << flush;
    cin >> x;                                   // Read a float
    float recip = 1/x;                          // Find its reciprocal
    cout << "The reciprocal is " << recip << endl;

//  Do a character "calculation"

    cout << endl;
    char r = '1';                               // The character '1'
    char s = '2';                               // The character '2'
    char sum = r + s;                           // Add the chars
    cout << "The sum of '1' and '2' is " << sum << endl;
    
    return 0;
}

Sample Run

Enter two integers:  4 8
The sum is 12

Enter a floating-point number:  3.2
The reciprocal is 0.3125

The sum of '1' and '2' is c



Return to Lectures page


e-mail me at robbk@hsc.edu

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

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

Return to Coms 261 home page