CPCS LAB 2 arithmetic operator cin I Mona
CPCS LAB 2 arithmetic operator & cin I. Mona Alshehri
The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns. The output is right justified. If the number of specified columns is less than the number of columns required by the output, then the output is automatically expanded to the required number of columns. To use the manipulator setw, the program must include the header file iomanip. h
Example: cout<<"1234567890"<<endl; cout<<setw(5)<<11<<endl; cout<<setw(5)<<“ALA”<<endl; cout<<setw(6)<<123<<endl; cout<<setw(2)<<345<<setw(4)<<19<<endl; Output: 1234567890 11 ALA 123 345 19
Arithmetic operators: + * / % addition subtraction multiplication division remainder (mod operator) Examples: f+7 r%s p-c b*m x/y
Rules of arithmetic operator precedence: () * / % + - inside to outside left to right The operators +, -, *, and / can be used with both integral and floating point data types, while % is used only for integral data type to find the remainder in ordinary division. Example: (3+4)* 5 + 2 /(32 -1) + 8%3
Example Arithmetic Expression 2 + 5 13 + 89 34 - 20 45 - 90 2 * 7 5/2 14 / 7 Result 7 102 14 -45 14 2 2 Description
Example Expression 34 % 5 -34 % 5 is Result Description 4 In the division 34/5, the quotient is 6 and the remainder is 4. -4 In the division -34 / 5, the quotient -6 and the remainder is -4. 4 % 6 4 In the division 4/6, the quotient is 0 and the remainder is 4.
Assignment operator: = (right to left) example: a = 32; a = a-2; Additional assignment operators: += -= *= /= %= a=a+2; a+=2; a=a-2; a-=2; c=c*2; c*=2; c=c/2; c/=2; c=c%2; c%=2;
Increment operator - increment the value of a variable by 1 Decrement operator- decrement the value of a variable by 1. Pre Increment: Post Increment: ++variable++ Pre Decrement: --variable Post Decrement: variable--
Example: // Preincrementing and postincrementing #include <iostream. h> int main() { int c; c = 5; cout << c << endl; // print 5 cout << c++ << endl; // print 5 then postincrement cout << c << endl; // print 6 }
Example: { } int c = 5; cout << c << endl; // print 5 cout << ++c << endl; // preincrement then print 6 cout << c << endl; // print 6 getch(); // successful termination
A Look at cin Int num 1; cout << “Enter a number: “; cin >> num 1; cout << cin >> insertion extraction “put to” “get from” whitespace characters ignored
cin & cout main() { cout << cin >> }
OUTPUT Statement: The output statement is used to display items on the screen. The syntax of cout together with << is cout<<item; OR cout<<item_1<<item_2<<…<<item_n; OR cout<<item_1 <<item_2 <<. . . <<item_n;
Types of items 1. Numbers: integer or float numbers cout<<10; 10 cout<<1. 25; 1. 25 cout<<1<<4<<2; 142 cout<<-534; -534 cout<<1, 25; Incorrect statement(syntax error)
Types of items 2. Characters: A single character must be enclosed in single quotes such as: cout<<‘A’; cout<<‘H’<<‘e’<<‘l’<<‘o’; cout<<‘A’<<‘B’<<‘C’; A Hello ABC
Types of items 3. Text: Multiple characters must be enclosed in double quotes such as: cout<<“Hello”; Hello cout<<“Mona Ali Mohammad”; Mona Ali Mohammad cout<<“Mona”<<“Ali”<<“Mohammad”; Mona. Ali. Mohammad cout<<“Mona”<<“ Ali ”<<“ Mohammad”; Mona Ali Mohammad
Types of items 4. Special Characters: The new line character : 'n‘ When n is encountered in the string, the cursor is positioned at the beginning of the next line. n may appear anywhere in the string. cout<<“Hellon. There”; Hello There The tab character : 't‘ Tab to next tab stop approx 8 spaces for each t cout<<“Hellot. There”; Hello There
Common Programming Errors not initializing variables before use 3 forgetting >> to separate variables in cin 3 applying ++ or -- to an expression (x-y)++ 3
Exercise: Write a program display your name in point that was specified by user (x, y)?
- Slides: 20