0% found this document useful (0 votes)
14 views25 pages

Lec 4 PF

This document covers programming fundamentals including variables, data types, arithmetic operators, and precedence. It defines variables as named locations in memory that have a name, type, size, and value. Common data types include int, short, long, float, and double. Arithmetic operators like +, -, *, /, and % are explained along with precedence rules. Examples are provided to demonstrate variable declaration and usage, data types, operators, and coding practice problems.

Uploaded by

wajeehadeveloper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views25 pages

Lec 4 PF

This document covers programming fundamentals including variables, data types, arithmetic operators, and precedence. It defines variables as named locations in memory that have a name, type, size, and value. Common data types include int, short, long, float, and double. Arithmetic operators like +, -, *, /, and % are explained along with precedence rules. Examples are provided to demonstrate variable declaration and usage, data types, operators, and coding practice problems.

Uploaded by

wajeehadeveloper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Programming Fundamentals

Lesson 4
#include <iostream>
using namespace std;
Int main ( )
{
cout << “ Welcome to Programming Fundamental“;
}
Variable

Variable X
Variable
 Pic of the memory

 25

name
 10323 of the
variable
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>
Using namespace std;
Int 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 ;
double x, y, z ;
double x; double y;
double 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: +,-
Practice
Find the largest and smallest short, int, long, float, and double on your machine.
Which of these data types requires the least amount of memory?
Which of the following are correct literals for floating-point numbers?
12.3, 12.3e+2, 23.4e-2, –334.4, 20.5, 39, 40
Show the result of the following remainders:
56 % 6
78 % 4
34 % 5
34 % 15
5%1
1%5
If today is Tuesday, what day will it be in 100 days?
What is the result of 25 / 4? How would you rewrite the expression if you wished
the result to be a floating-point number?
Show the result of the following code:
cout << 2 * (5 / 2 + 5 / 2) << endl;
cout << 2 * 5 / 2 + 2 * 5 / 2 << endl;
cout << 2 * (5 / 2) << endl;
cout << 2 * 5 / 2 << endl;
Practice
Are the following statements correct? If so, show the output.
cout << "25 / 4 is " << 25 / 4 << endl;
cout << "25 / 4.0 is " << 25 / 4.0 << endl;
cout << "3 * 2 / 4 is " << 3 * 2 / 4 << endl;
cout << "3.0 * 2 / 4 is " << 3.0 * 2 / 4 << endl;
Write a statement to display the result of 23.5.
How do you write the statements to let the user enter an
integer and a double value from the keyboard?
What is the printout if you entered 2 2.5 when executing the
following code?
double width;
double height;
cin >> width >> height;
cout << width * height;

You might also like