You are on page 1of 4

EL 125 Programming Fundamentals

Experiment # 01

Working with Constants, Variables and Arithmetic Operators


Performed on 27 Feb, 2017

Student Name:
Roll Number:

Maximum Marks Performance = 05 Viva = 05 Total = 10


Marks Obtained

Remarks (if any)

Experiment evaluated by

Instructor Name:

Signature and Date:

Copyright Department of Electrical Engineering Usman Institute of Technology


Theory

Constants:
Constant is an entity that does not change.
Variables:
An entity that may vary during program execution.
Keyword:
A predefined word that cannot be used as a variable name. It is also called Reserved Words.
Character Set of C++:
It is a Combination of Alphabets, Digits, and Special symbols to form Constants, Variables
and Keywords.
Variable Declaration:
Variable is generally declared as:

TYPE VAR;

Here Type is C++ data type and VAR is the variable name. You can also declare more
than one variables of same type by using comma-separated list. For example

TYPE VAR1, VAR2, VAR 3;

Value Assignment to the Variable:


Depending on the data type, a programmer can assign proper value to the variable. The
general form of an assignment statement is

variable_name = value;
For Example:
a=10;
Basic Input and Output:
C++ I/O occurs in streams, which are sequences of bytes. If bytes flows from a device like a
keyboard, a disk drive, or a network connection etc. to main memory, this is called input
operation and if bytes flows from main memory to a device like a display screen, a printer, a
disk drive, or a network connection, etc, this is called output operation.

The standard output stream (cout):


The predefined object cout is an instance of ostream class. The cout object is said to be
"connected to" the standard output device, which usually is the display screen. The cout is
used in conjunction with the stream insertion operator, which is written as << which are two
less than signs.

For Example:
cout <<Welcome to Programming Fundamentals Lab;

The standard input stream (cin):


The predefined object cin is an instance of istream class. The cin object is said to be
attached to the standard input device, which usually is the keyboard. The cin is used in
conjunction with the stream extraction operator, which is written as >> which are two greater
than signs

Copyright Department of Electrical Engineering Usman Institute of Technology


For Example:
int val;
cout<< Enter an integer value:;
cin>>val;

Arithmetic operator and Expression:


An expression is a combination of operators and operands to make programming easier and
efficient. C++ expression follows the rules of algebra. C++ describes the following arithmetic
operators:

Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus or Remainder

The +, -, /, * operators are used with any of the data type but % operator can only be
used with integer type only.

Operator Precedence:
For an expression it is necessary to know about the order of precedence of arithmetic
operators. The *, / and % operator are higher precedence than the + and operators.
However, a programmer can alter the order of evaluation using parentheses.
For example, the result of the following expression is 0.

40 - 10 * 4

But the following produces result of 120:

(40 - 10) * 4

Operators Associatively:
Associatively may be left associative (left to right) or right associative (right to left). All
arithmetic operators are left associative; assignment operators (+=, -=, *=, =, %=, =)
are right associative.

For Example:
int a=10, b=5, c=2;
a + b * c; 10 + (5*2) = 20
a * b % c; (10 * 5) % 2 = 0

Copyright Department of Electrical Engineering Usman Institute of Technology


Observations

Program

#include <iostream>
using namespace std;
int main ()
{
// Variable declaration int a, b;
int c;
float f;
// initialization b = 20;
cout<<Enter value of a:\n;
cin >> a;
c = a + b;
cout << c << endl ;
f = 70.0/3.0;
cout << f << endl ;
return 0;
}

Do it yourself

Simple Task:

1. Write a program in C++ (already discussed in the class) to display the sizes of
various data types and complete the following table:

Data Type Memory Size in Bytes


Integer
Long Integer
Character
Short
Long Double Floating Point

2. Take length & breadth of a square and radius of a circle as input through the
keyboard. Write a program to calculate the area of the square and the area and
circumference of the circle
3. Temperature of a city in Fahrenheit degrees in input through the Keyboard. Write a
program to convert this temperature into Centigrade.
5
= ( 32)
9
Home Task:
1. Write a program for which can perform simple arithmetical operations take input
from user.
(Hint: Perform Addition, Subtraction, Multiplication and division of two numbers)

Copyright Department of Electrical Engineering Usman Institute of Technology

You might also like