01 Introduction 2 PDF
01 Introduction 2 PDF
01 Introduction 2 PDF
First Year
Scientific Research
Al- Mustansiriya University
College of Engineering
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.
void, is the return type of the main function. When the return type of a function is void,
this function will not passes back any value to the calling function.
Some programmers use int as a return type for the main function, in this case a
return(0) statement must be written as a last statement of the main function-body.
//, text after these symbols is a comment. It does not affect the program code, and
compilers normally ignore it.
cout, the input stream object. It passes the characters quotes (“) to the terminal screen.
cin, the input stream object. It reads the input values from the keyboard.
<<, the stream insertion operator (or send operator).
The endl is used in c++ to represent a new line, as shown in the following
example:
Example 2
#include<iostream.h>
void main( )
{
cout << “hallow” << 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:
Example 3
#include<iostream.h>
void main( )
{
cout << “hallow \n”;
cout << “students”;
}
3 Variables Declaration:
A declaration is a process of naming the variables and their statements
datatypes in C++. C++ allows declaration of the variables before and after
executable statements. A variable ia an object that may be take on values of
the specified type.
Also ,a variable is a location in the computer’s memory where a value
can be stored for later use by the program. Variables are like buckets that
hold data. These data buckets are really locations in the computer’s memory.
The variable must be declared by specifying the datatype and the identifier.
datatype id.1, id2, …,idn;
Example 5
The following program reads three different inputs and outputs it.
#include<iostream.h>
void main( )
{
int n; float f; char c;
cout << “input integer number:”;
cin>>n;
cout<<endl;
cout << “input decimal number:”;
cin>>f;
cout<<endl;
cout << “input character:”;
cin>>c;
}
4 Constants:
Like variables, constants are data storage locations. Unlike variables, and as
the name implies, constants don’t change.
const int myage=23;
const double pi=3.14;
const float salary=20.5;
Example 6
Write a program that reads the radius of a circle, then computes and
outputs its area.
#include<iostream.h>
void main( )
{
const float pi = 3.14;
int r; float c;
cout << “enter the radius of circle:”;
cin>>r;
cout<<endl;
c = r * r * pi;
cout << “the area of circle:” << c;
}
Example 7
The following program computes the arethmatic operators.
#include<iostream.h>
void main( )
{ Output:
int a,b,sum,sub,mul,div; Enter any two numbers
cout << “enter any two numbers<<endl; 10 20
A=10 b=20 sum=30
cin>> a>>b;
Sub=-10
sum=a+b; Mul=200
sub=a-b; Div=0
mul=a*b;
div=a/b;
cout<<”a=”<<a<<”b=”<<b<<”sum=”<<sum<<endl;
cout<<”sub=”<<sub<<endl;
cout<<”mul=”<<mul<<endl;
cout<<”div=”<<div<<endl;
}
Example 8
The following program computes different division operators.
#include<iostream.h>
void main( )
{
int x, y, z, r ;
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;
}
The modulus operator “%” is used with integer operands (int, short, long,
unsigned). It can’t be used with float or double operands.
Example 9
#include<iostream.h>
void main( )
{
int y1, y2;
y1 = 8 % 3;
y2 = -17 % 3;
cout << "y1="<< y1 <<endl;
cout << "y2="<< y2 <<endl;
}
Lecture 5
Example 1:
Write the following equation as a C++ expression:
Solution:
f = (a + b + c + d + e) / 10;
Note: the parentheses here are required because division has
higher precedence than addition.
Example 2:
State the order of evaluation for the following expression:
Z = P * R % Q + W / X – Y;
Solution:
1. *
2. %
3. /
4. +
5. -
Example 1
Write C++ program to perform the above equation:
#include<iostream.h>
void main( )
{
int Z, P, R, Q, W, X, Y;
cout << "enter P:"; cin >> P;
cout << "enter R:"; cin >> R;
cout << "enter Q:"; cin >> Q;
cout << "enter W:"; cin >> W;
cout << "enter X:"; cin >> X;
cout << "enter Y:"; cin >> Y;
Z= P * R % Q + W / X - Y;
cout << "the result="<< Z;
The ++ and - - operators can be written either before the variable (prefix
notation) or after the variable (postfix notation) as in the following:
Output: Output:
8 7
8 8
3 Manipulator Functions:
They are special stream functions that change certain characteristics of the
input and output.
(a) Endl: Generate a carriage return or line feed character.
Cout << “a” << endl;
(b) Setbase: It is used to convert the base of one numeric value into a
nother base
Dec(base 10), hex(base 16), oct(base 8)
Example 2
Write C++ program to convert a base of a number:
#include<iostream.h> Enter number
void main( ) 10
{ Decimal base=10
int value; Hexadecimal base=a
Octal base=12
cout << "enter number:"; cin >> value;
cout << "Decimal base=”<<dec<<value<<endl;
cout << "Hexadecimal base=”<<hex<<value<<endl;
cout << "Octa base=”<<oct<<value<<endl;
}