Computer Programming (Lab #01 Manual)
Computer Programming (Lab #01 Manual)
Computer Programming (Lab #01 Manual)
01
REVIEW AND IMPLEMENTATION OF BASIC STATEMENTS OF C++
LANGUAGE (PART-I)
PRE LAB TASK
Objectives:
The objectives of this session are:-
Become familiar with the basic components of a C++ program, including functions,
special
symbols, and identifiers
Explore simple data types
Discover how to use arithmetic operators
Examine how a program evaluates arithmetic expressions
Learn what an assignment statement is and what it does
Introduction:
C++ Language
C++ is an object oriented programming (OOP) language, developed by Bjarne Stroustrup, and is
an extension of C language. It is therefore possible to code C++ in a "C style" or "object-oriented style
Theory:
Sample Run: (When you compile and execute this program, the following five lines will
appear on the screen.)
Example:-
What is the output of following program?
Arithmetic Operators:-
The arithmetic operators are the symbols that represent arithmetic operations. These are used in
arithmetic expression. Each arithmetic operator operates upon two numeric values and returns a
value.
The following arithmetic operators are used in C++.
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% For remainder
All arithmetic operators except the remainder operator are used for all type of numeric data.
The remained or modulus operator (% operator) can only be used for integer type data.
Example:-
Write a C++ program to perform the arithmetic operation by using all arithmetic operators. Also
print the result on screen. Use the % operator to find the remainder. For example, 5%3 returns a
remainder of 2.
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int d,p,s,m,r;
p=5+2;
s=5-2;
m=5*2;
d=5/2;
r=5%2;
cout<<"Addition of 5 and 2 is "<<p<<endl;
cout<<"Subtraction of 5 and 2 is "<<s<<endl;
cout<<"Multiplication of 5 and 2 is
"<<m<<endl; cout<<"Division of 5 and 2 is
"<<d<<endl; cout<<"Remainder of 5/2 is
"<<r<<endl; system("PAUSE");
return 0;
}
Note:-
There is no operator for the exponential power of a given number in C++. However the built-in function
“pow” is used Arithmetic Expressions:-
An arithmetic expression is a combination of variable constants and arithmetic operators. It is used to
calculate the value of an arithmetic formula. It returns a single value after evaluating the expression.
The value of the expression is assigned to a variable on the left side of “=” operator. This variable
is known as the receiving variable.
The operator “=” is called assignment operator. It is used to assign the calculated value of the
expression to the receiving variable. The receiving variable must be of numeric type.
For example if m=10, x=5 the expression may be written as:-
m*x+100
In the above example, “m*x+100” is an arithmetic expression. This expression returns a single
value. This value is assigned to a single variable using the assignment operator. Thus if “res” is the
receiving variable, the assignment statement is written as:
res=m*x*100
An arithmetic expression may be a single value i.e. a constant or a variable name. For example the
following is a valid assignment statement.
a=10;
The order in which the arithmetic expression is evaluated is called the order of precedence. It is
also known as hierarchy of operation
When an arithmetic expression is evaluated, the computer performs only one operation at one
time. In an expression in C++ the operations are performed in the following order.
1. All multiplications and divisions are performed first from left to right.
2. All additions and subtractions are then performed from left to right.
3. If the parenthesis are used in an expression, the expression within parenthesis are first
computed from left to right.
4. When parenthesis are used within parenthesis, the expression within innermost parenthesis is
evaluated first.
For example:
(4-(3*5))+2
Is evaluated as follow:
1. (3*5) is computed and returns value of 15.
2. 4-15 is computed and then return a value of -11.
3. -11+2 is computed and returns value of -9.
Escape Sequences:-
Special non printing characters are used to control printing on the output device. These are called
escape sequence. These characters are not printed. These are used inside string constants or
independently. These are written in single of double quotes.
An escape sequence is a combination of backslash “\” and a code character. The backslash is called
the control character. For example to transfer the printing control to the next line the escape
sequence is written as \n. Thus to transfer printing control to the next line, the output statement is
written as:
In the above statement “\n” is the escape sequence character. It shifts the printing control to the next
line after the string is printed.
An escape sequence can be used anywhere in the output stream. It can be used at the beginning
of the string, in the middle or at the end of the string.
If it is used at the beginning of the string then the string will be printed after printing a blank line.
To print the above string in three lines the statement is written as:
The “setw” stands for set width. This manipulator is used to set the width of the output on the output device.
The “setw” manipulator specifies the width for an output. The output is printed left justified in the specified width.
It syntax is:
setw(n)
Where
When the above statement is executed the word “Pakistan” is printed on the computer screen left justified
in first 10 columns. Similarly, the word “Islamabad” is printed left justified in column 11-25. The “setw”
manipulator is a part of iomanip header file. If this manipulator is to be used in the program then this header
file should be included at the beginning of the program using include statement.
The “cin” stands for console input. It is pronounced as “See In”. It is an input stream. It is used as
an input statement to get input from the keyboard during the execution of the program.
When an input statement is executed the computer waits to receive an input from keyboard. When a value is
typed and Enter key is pressed the value is assigned to the variable and control shifts to the next statement.
The “cin” object is also a part of iostream header file. The syntax of “cin” is:
cin >> var1 [>>var2------];
Where
cin represents the object name used as an input stream. This stream represents data coming from
the input device keyboard.
>> extraction operator or get from operator. It gets an input from the input device and assigns it to the
variable. var1, var2 represent list of variables. Each variable is separated by extraction operator “>>”.
Usually a separate statement is used for each variable.
At least one variable on the right hand side of the “>>” operator must be used. The use of other
variables is optimal.
For example to input data into variables a, b and c the input statement is written as:
cin>>a>>b>>c;
Example:-
Write a program in C++ to get two numbers from keyboard during program execution.
Calculate the sum and product of the numbers and then print the result on the screen.
#include<iostream>
#include<cstdlib>
#include<iomanip>
using namespace std;
int main()
{
int n1,n2,s,p;
cout<<"Enter First Number ? "<<endl;
cin>>n1;
cout<<"Enter Second Number ? "<<endl;
cin>>n2;
s=n1+n2;
p=n1*n2;
cout<<"The sum of the numers= "<<s<<endl;
cout<<"The product of the numbers =
"<<p<<endl; system ("PAUSE");
return 0;
}
Lab Tasks:
1. Write a program in C++ to get the age of the person. The program should calculate and
display the age of the person in months.
2. Write a program in C++ to read temperature in Fahrenheit. Convert the temperature to
Celsius degrees by using the formula.
𝒄=𝟓/𝟗(𝒇−𝟑𝟐)
3. Ohms Law states that “current flowing through a conductor is directly proportional to
applied voltage provided by source”. Mathematically,
V= I R
V: Applied Voltage
I: current
R: resistance
You are required to write a C++ program which takes ten values of Voltage from user and
calculates the Current.
Case I: R= 20K Ohm
Case II: R=10K Ohm
4. Write a C++ program which takes radius value from user and calculates its Circumference
and area of circle. (Note:𝑪=𝟐𝝅𝒓 , =𝝅𝒓𝟐 )
LAB SESSION
Questions:
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
_______________________________