Translate

C++ Adding Integers

This program uses the input stream as cin and the stream extraction operator, >>, to obtain two integers typed by a user at the keyboard, computes the sum of these values and outputs the result. For example, the computer will ask user to input an integer. So, the command is: cout << "Enter first integer: ";

The second step is,
computer or our program will record the input value from the keyboard which entered by user. The command is: cin >> integer
Please note, that the integer after cin>> above we call as variable. And the variable must declare first before we run the program, and we put the variable after main().

Let we take a look for the example.

Displays the sum of two integers entered from the keyboard:


  1. #include <iostream> // this will allow program to perform input and output
  2.  using namespace std; /* we use the standard c++ namespace for all function, including cout and cin*/
  3.    // function main. This begins the program execution
  4.    int main()
  5.    {
  6.       // variable declarations
  7.      int numb1; // first integer that will be add by user  
  8.      int numb2; // second integer that will be add by user 
  9.      int sum; // sum of variable numb1 and numb2

  10.      cout << "Please Enter The first integer: "; // prompt user for data
  11.      cin >> numb1; // read first integer from user into numb1

  12.      cout << "Please Enter The second integer: "; // prompt user for data
  13.      cin >> numb2; // read second integer from user into numb2

  14.      sum = numb1 + numb2; // add the numbers; store result in sum

  15.      cout << "And The Sum is " << sum << endl; // display sum, then make new line

  16.      return 0;
  17. }




And the Result is:





The program begins execution with function main (see int main()).
The left brace ({) function is to marks the beginning of our main's body and the corresponding right brace (}) marks the end of our main program.
int numb1; int numb2; and int sum; is the declarations. The identifiers of numb1, numb2 and sum are the names of variables. And variable is a location in the computer's memory where a value can be stored for use by a program.

These declarations specify that the variables numb1, numb2 and sum are data of type int, meaning that these variables will hold the integer values, i.e., whole numbers such as 8, 44, 0 and 6655665.

All variables must be declared with a name and a data type before they can be used in a program. Several variables of the same type may be declared in one declaration or in multiple declarations. We could have declared all three variables in one declaration as follows:
int numb1, numb2, sum;

Declarations of variables can be placed almost anywhere in a program, but they must appear before their corresponding variables are used in the program. To calculate the number of the number of variables and numb2, numb1 and to provide the sum of these two variables, we use the assignment operator =.

The statement will be read as, "the total value of numb1 + numb2." Most calculations are performed in assignment statements. Operator = and operator + called binary operators because each operator has two operands. In the case of the + operator, both operands are numb1 and numb2. And in the case of operator = before, both operands are numbers and the value of the expression  numb1 numb2, as shown at the syntax: << "Sum is " << sum <<endl;

Thus, the variable is something that you can think of as a magic box in which you can place information-or, rather, the data. But what kind of data? All the data on the computer numerical eventually, but is set to be one of the three basic formats: integer, floating-point, and text strings.

Note that the previous statement outputs multiple values ​​of different types. Stream insertion operator "knows" how to output any type of data. Using multiple stream insertion operator (<<) in a single statement referred to as concatenating, chaining or cascading stream insertion. It is necessary to have some statement to output multiple pieces of data.

Calculations can also be made in the report output. We can combine the statement:
sum = numb1 + numb2;
and the statement:
cout << "Sum is " << sum <<endl;
in to the statement:
cout << "Sum is " << numb1 + numb2 <<endl;

And it eliminates the need for a lot of the number of variables, or in other words: Saves syntax.

The variable names numb1 and numb2 is the actual number corresponding to the location in the computer's memory. Each variable has a name, type, size and value. When the statement cin >> numb1, run, characters typed by the user via the keyboard will be converted to an integer and assigned to a memory location where the variable name numb1 set by the C compiler. Suppose the user enters the number 45 as the value for numb1, then the computer will put 45 to the location numb1.

Each time the value is placed in the memory location, then the new value will overwrite the previous value that was in that location. Thus, placing a new value to a memory location can be said to be damaging. when the statement cin >> numb2 run, let users enter a value of 72 of the keyboard, this value will be placed into the location numb2.

Once the program gets the value for numb1 and numb2, it will add the values ​​and the number of places to the number of variables. Statement Number = numb1 + numb2, who also added that replaces whatever value is stored in the number. This occurs when the amount calculated from numb2, numb1 and placed in a number of locations (regardless of the value of what may already exist in variable amounts, and missing values​​). After the calculated amount, the memory will appear exactly as they did before they were used in the calculation of the amount.
The values ​​used are not destroyed, although computers perform calculations and occurs overwriting the memory. Thus, when the value read from the memory location, the process is not destructive.

No comments:

Post a Comment