lecture 3
lecture 3
C++ language
input/output
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
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;
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 5. y = 65 + 7; (Last a dd it ion)
65 + 7 is 72
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;
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
}
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