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

Introduction To Programming Lecturer #2

This document provides an introduction to programming concepts like variables, data types, operators, and input/output. It explains that a variable is a named location in memory that can store a value of a particular type, like int or float. Arithmetic operators like +, -, *, /, and % are covered along with precedence rules. The document demonstrates declaring and assigning values to multiple variables, then performing calculations and output. An example program calculates the total and average age of 10 students by collecting their ages using input and storing in variables.

Uploaded by

ameena
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Introduction To Programming Lecturer #2

This document provides an introduction to programming concepts like variables, data types, operators, and input/output. It explains that a variable is a named location in memory that can store a value of a particular type, like int or float. Arithmetic operators like +, -, *, /, and % are covered along with precedence rules. The document demonstrates declaring and assigning values to multiple variables, then performing calculations and output. An example program calculates the total and average age of 10 students by collecting their ages using input and storing in variables.

Uploaded by

ameena
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Introduction to

Programming

Lecturer #2
Reading Material
Deitel & Deitel C++ How to
Program
chapter 1
1.2, 1.3, 1.4, 1.6, 1.7, 1.11, 1.12,
1.13,1.19, 1.20, 1.21, 1.22
What is programming
A precise sequence of steps to
solve a particular problem
Editor
Interpreter and Compilers
Debuggers
Linker
loader
#include <iostream.h>
main ( )
{
cout << Welcome to Virtual University
;
}
Variable

Variable X
Variable
Pic of the memory

25

name
of the
variabl
10323 e
Variable
Variable starts with
1. Character
2. Underscore _ (Not
Recommended)
Variable
Small post box

X
Variable
Variable is the name of a
location in
the memory

e.g. x= 2;
Variable
In a program a variable
has:
1. Name

2. Type

3. Size

4. Value
Assignment
Operator
=
x=2

X
2
Assignment
Operator
L.H.S = R.H.S.

X+ 3 = y + 4 Wrong
Z = x +4
x +4 = Z Wrong
X = 10 ; X 10
X = 30 ;
X 30
X = X + 1;
X
10 + 1
= 11
X
Data type
int i ; ->
Declaration line

i
#include <iostream.h>
main ( )
{
int x ;
int y ;
int z ;
x = 10 ;
y = 20 ;
z=x+y;

cout << " x = " ;


cout << x ;

cout << " y = " ;


cout << y ;

cout << " z =x + y = " ;


cout << z ;
}
int x, y, z ;
int x; int y; int z
;
Data Types
1. int
2. short
3. long
4. float
5. double
6. char
Arithmetic
operators
Plus +

Minus -

Multiply *

Divide /

Modulus %
Arithmetic
operators
i+j
x*y
a/b
a%b
% = Remainder

5%2=1
2%2=0
4/2=2
5/2=?
Precedence
Highest: ()
Next: * , / , %
Lowest: +,-
#include <iostream.h>
main ( )
{
int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10 ;
int TotalAge ;
int AverageAge ;

cout << Please enter the age of student 1: ;


cin >> age1 ;

cout << Please enter the age of student 2: ;


cin >> age2 ;
:
:
TotalAge = age1+ age2 + age3+ age4+ age5+age6+ age7+ age8+age9 +
age10 ;
AverageAge = TotalAge / 10 ;

cout<< The average age of the class is : << AverageAge ;


}

You might also like