A Simple Program (Continued)
![]() |
Click on image to enlarge it. |
Output of the above program:
Another Example:
![]() |
Click on image to enlarge it. |
Output of the above program:
Another Simple Program:
Adding Two Integers:
- Variables
- Location in memory where value can be stored
- Fundamental data types
- int - integer numbers (whole numbers + and -)
- char - characters
- float/double - floating point numbers
- Declare variables with name and data type before use
int integer1; int integer2; int sum;
- Can declare several variables of same type in one declaration
- comma-separated list
int integer1, integer2, sum;
- Variable names
- Valid identifier
- Series of characters (letters, digits, underscores)
- Cannot begin with digit
- Case sensitive
- Input stream object
- >> (stream extraction operator)
- used with cin
- Waits for user to input value, then press Enter (Return) key
- Stores value in variable to right of operator
- Coverts value to variable data type
- = (assignment operator)
- Assigns value to variable
- Binary operator (works on two operands)
- Example:
- sum = variable1 + variable2;
- Operands : sum, value of expression, variable1+variable2
Code:
// http://welearncpp.blogspot.com // code3.cpp // Addition Program #include <iostream> using namespace std; // function main begins execution of program int main() { int integer1; // first number to be input by user int integer2; // second number to be input by user int sum; // variable in which sum will be stored cout << "Enter first integer: "; // prompt cin >> integer1; // read an integer cout << "Enter second integer: "; // prompt cin >> integer2; // read an integer sum = integer1 + integer2; // assign result to sum cout << "Sum is " << sum << endl; // print sum return 0; // indicates successfull execution of program } // ends the main function
Output of the above program:
Well after this class you might be able to create simple programs such as simple calculator. We will soon come back with a new class for new things..
For complete list of manipulators visit this site:
http://www.cplusplus.com/reference/library/manipulators/