Lec 4 PF
Lec 4 PF
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;
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;