0% found this document useful (0 votes)
1 views

lecture 3

Lecture 3 covers the basics of C++ input/output operations, including the structure of a C++ program and the use of cin and cout for reading from the keyboard and displaying output. It explains arithmetic operators, their precedence, and provides examples of programs that demonstrate variable declaration, input/output, and arithmetic calculations. Additionally, it discusses assignment operators, increment/decrement operators, and provides sample outputs for various C++ code snippets.

Uploaded by

jafar.bh200
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

lecture 3

Lecture 3 covers the basics of C++ input/output operations, including the structure of a C++ program and the use of cin and cout for reading from the keyboard and displaying output. It explains arithmetic operators, their precedence, and provides examples of programs that demonstrate variable declaration, input/output, and arithmetic calculations. Additionally, it discusses assignment operators, increment/decrement operators, and provides sample outputs for various C++ code snippets.

Uploaded by

jafar.bh200
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Lecture 3

C++ language
input/output

Prof. Dr. Ashwaq Alabaichi


[email protected]
[email protected]

1
• The typical C++ program structure is given below:
Program heading
Begin
Type or variable declaration
Statements of operation Results
end
The keyboard and screen I/O instructions in C++ are:
cout/ display an object onto the screen:
cout<<var1<<var2<<…<<varn;
cin/ It is used to read an object from keyboard:
cin>>var1>>var2>>…>>varn;

Every group of related functions is stored in a separate library called (header file).To use
the cin and cout , must include the header file iostream .
main( ), is the name of C++ function. Every C++ program must have a function called
main.
{indicates the start of the statements in the function.
} indicates the end of the statements in the function.
//text after these symbols is a comment. It does not affect the program code and
compilers normally ignore it.

2
#include<iostream.h>
main( )
{
// A program to print welcome
cout << "Welcome";
}

Example 2
#include<iostream.h>
main( )
{
cout << "hello" << endl;
cout << "students";
}
The \n is a special escape code, also used in C++ to represent a new line, as shown in the
following example:

3
#include<iostream.h>
main( )
{
cout << " hello\n“;
cout << "students";
}
Output:
hello
students

4
The following program print the three different variables .
#include<iostream.h>
main( )
{
int num=3;
cout << " number= " <<num<< " \n ";
char ch=’a’;
cout << " character= " <<ch<< " \n ";
float fa=-34.45;
cout<< " real number= " <<fa;
}
Output:
number=3
character=a
real number=-34.45

5
The following program reads three different inputs and outputs it.
#include<iostream.h>
main( )
{
int n; float f; char c;
cout << "input integer number:";
cin>>n;
cout<<n<<endl;
cout << "input decimal number:";
cin>>f;
cout<<f<<endl;
cout << "input character:";
cin>>c;
cout<<c;
}

Output:
input integer number: 5
input decimal number: 5.6
input character: A

6
Arithmetic operators

Arithmetic operators: These operators require two variables to be evaluated:

+ addition, - subtraction, * multiplication, / division ,% modular

The division result are:

Integer / integer = integer► 39/7=5


Integer / float = float ► 39/7.0 =5.57
float / integer = float ► 39.0/7 =5.57
float / float = float ► 39.0/7.0=5.57

while 39%5=4, since 39=7*5+4

Arithmetic operators as per precedence:

( ) for grouping the variables.


++, – –
-, + Unary minus , plus for negative, positive number .
* , /,% multiplication, division, modular.
+, - addition and subtraction.

7
Write a program that reads the radius of a circle, then computes and outputs its area.

#include<iostream.h>
#include<conio.h>
main( )
{
const float pi = 3.14;
float r;
float c;
cout << "enter the radius of circle:";
cin>>r;
c = r * r * pi;
cout << "the area of circle:" << c;
getch ( );
return 0;
}

OUTPUT:
enter the radius of circle:5.5
the area of circle: 94.985

8
The following program computes the arithmetic operators.
#include<iostream.h>
#include<conio.h>
main( )
{
float a, sum, sub, mul, b, div;

cout << "enter any two numbers\n";


cin>> a>>b;
//sum=a+b;
//sub=a-b;
//mul=a*b;
// div=a/b;
cout<<"a="<<a<<" b="<<b<<" sum="<<a+b<<endl<<"sub="<<a-b<<endl<<"mul="<<a*b<<endl
<<"div="<<a/b;
//cout<<"sub="<<sub<<endl;
//cout<<"mul="<<mul<<endl;
//cout<<"div="<<divl;
getch ( );
return 0;
}
Output:
Enter any two numbers
10.0 20.0
a=10.0 b=20.0 sum=30.0
sub=-10.0
mul=200.0 9
The following program computes different division operators.
#include<iostream.h>
#include<conio.h>
main( )
{
int y, z, r ;
float x;
x= 7/ 2;
cout << "x=" << x <<endl;
y=17/-3;
cout << "y="<< y <<endl;
z=-17/3;
cout << "z="<< z <<endl;
r=-17/-3;
cout << "r="<< r <<endl;
getch ( );
return 0;
}
Output:
x=3
y=-5
z=-5
r=5

10
The modulus operator “%” is used with integer operands (int, short, long, unsigned). It can’t be used
with float or double operands.

#include<iostream.h>
#include<conio.h>
main( )
{
int y1, y2;
y1 = 8 % 3;
y2 = -17 % 3;
cout << "y1="<< y1 <<endl;
cout << "y2="<< y2 <<endl;
getch ( );
return 0;
}
y1=2
y2=-2

comentes
int a=0 // this is a =0
int a=0 /* the compiler
cannot read this*/

11
Cont…

St ep 1. y = 2 * 5 * 5 + 3 * 5 + 7; (Left m ost mult ip lic at ion)


2 * 5 is 10

St ep 2. y = 10 * 5 + 3 * 5 + 7; (Left m ost mult ip lic at ion)


10 * 5 is 50

St ep 3. y = 50 + 3 * 5 + 7; (Mult ip lic at ion bef ore ad dition)


3 * 5 is 15

St ep 4. y = 50 + 15 + 7; (Left m ost ad dit ion)


50 + 15 is 65

St ep 5. y = 65 + 7; (Last a dd it ion)
65 + 7 is 72

St ep 6. y = 72; (Last op era t io n—p la c e 72 in y )

Ex:
X+Y*X-Z, where X=5, Y=6, and Z=8.
5 +(7-9) 6*5-8 → 5+(6*5)-8 → 5+30-8 → 35-8=27
12
Assignment Operators: In C++ language, you can use the assignment operator within any valid
expression. The general form of the assignment operator is
variable_name = expression;
where an expression may be as simple as a single constant or as complex as you require. The
target, or left part, of the assignment must be an object, such as a variable, that can receive a
value.

Ex: x=x+5;
y=y*10;
The operational assignment operator can be written in the following form:
Variable operator = expression;
Ex: x+=5;
y*=10;

Variable Initializations:
The general form of initialization is:
type variable_name = value;
or
type variable_name;
variable_name = value;
Ex:
char ch = 'a';
int first = 0;
float balance = 123.23;
13
Rewrite the equivalent statements for the following examples, and find it
results. Assume: X=2 , Y=3 , Z=4 , V=12 , C=8.

14
Increment and Decrement Operator
The operator ++ adds 1 to its operand, and -- subtracts one
In other words :
x = x+1; is the same as ++x; and x = x-1; is the same as --x;

Increment and decrement operators may either precede (prefix) or follow (postfix) the operand.

In prefix the increment or decrement operation is performed before obtaining the value of the
operand for use in the expression whereas in postfix the increment or decrement operation is
performed after obtaining the value of the operand for use in the expression
Ex:
x = 10;
y = ++x;

Set y and x to 11.


However, if you write the code as
x = 10;
y = x++;
cout
y is set to 10,but x is set to 11

15
16
display the output of the following program
main()
{
int inc;
inc=0;
cout <<inc<<“\n”;
inc++;
cout<<inc<<“\n”;
++inc;
cout<<inc;
}
0
1
2
Is equivalent to the program
main()
{
int inc=0;
cout <<inc<<“\n”;
inc=inc+1;
cout<<inc<<“\n”;
inc+=1;
cout<<inc;
}
17
display the output of the following program
main()
{
int inc;
inc=0; The results are
// cout<<inc<<"\n"; inc= 0
cout<<inc++ <<"\n"; inc= 0
cout<<inc<<"\n"; Inc= 1
cout<<++inc<<"\n"; Inc= 2
// cout<<inc; Inc= 2
}

display the output of the following program

main()
{
The results are
int dec;
Dec= 2
dec =2;
cout <<dec<<"\n";
Dec= 1
dec--;
cout<<dec<<"\n";
Dec=0
--dec;
cout <<dec<<"\n";
}

18
main()
{
int dec;
dec=2; The results are
cout<<dec<<"\n"; 2
cout<<dec--<<"\n"; 2
cout <<dec<<"\n"; 1
cout<<--dec<<"\n"; 0
cout <<dec; 0
}

main()
{
int dec;
dec=2; The results are
cout<<dec<<"\n"; Dec= 2
dec=dec-1; Dec= 1
cout<<dec<<"\n"; Dec=0
dec-=1;
cout<<dec;
}

19
What is the output of the following program
#include <iostream.h>
#include<conio.h>
main()
{
int s1=5, s2=10, s3;
cout<<++s1<<endl;
cout<<s2++<<endl;
s3=++s1-s2--;
cout<<s3<<endl<<s2;
getch();
return 0;
}
output
6
10
-4
10
Summary/Arithmetic Operators
-- Decrement
++ Increment
- unary minus , + unary plus
* Multiplication, / Division, % Modulus
– Subtraction, + Addition
20

You might also like