Tuesday 27 November 2012

Doing some addition

Purpose of Program


Add up two integer numbers, and then print out the result

Program




    #include 

    using namespace std;

    int main()
    {
        // declare three integer variables
        int a, b,c;

        // enter the variables

        cout << "Enter the First Number :";
        cin >> a;
        cout << endl << "Enter the Second Number :";
        cin >> b;

        // do some mathematics - add a and b together,
        // then assign the result to c
        c = a + b;
        cout << endl;
        cout << a  << " + " << b << " = " << c ;
    }



Some thoughts

This program declares three integers, a, b, c. Integers are numbers like -1, 2, 5, 4 etc. Whole numbers, in other words. If you put in a number like 1.2 this program will produce an error.

Every variable has a data type. One of the features of most programming languages is that when you put a character like 'c' into an integer they may throw an error. C++ does try to convert types behind the scenes, but it is not always smart about it.

At this stage I am not dealing with these issues. The purpose of the program is simply to add two integers together.




No comments:

Post a Comment