Professional Documents
Culture Documents
C++ Lecture 1 PPT PDF
C++ Lecture 1 PPT PDF
Raghav Garg
Today’s checklist
#include<iostream>
int main(){
cout<<“hello world”;
return 0;
}
How to move in next line?
Example :
cout<<“Hello PW”;
cout<<“Hello CW”;
Output will be ?
Hello PWHello PW
Use of escape sequence ‘\n’ and endl
Example :
cout<<“Hello PW”;
cout<<“\n”; or cout<<endl;
cout<<“Hello CW”;
Output will be ?
I
# include ciostream> Output
Hello PW
namespace std;
·
using
main() [
&
int
I
· nn
Predict the output :
⑧
main(){
· nn
cout<<"nn\n\nnn\n";
·
nu/n/nnn/n
cout<<"nn/n/nnn/n";
}
Printing Numbers (what computer thinks is a number
and what is a number)
Examples :
couts"
I";
text
cout<<4;
!
cout<<4+3;
cout<<“4+3”;
cout <>
number;
Variables and their Declaration
(our
Let us focus on int data type as of now.
1) Variables as containers :
↳
*
Printing Variables in C++ & Updation of Variables
Output
&
I
int x = 5;
·
19
cout<<x;
·
29
x = 7; /
cout<<x; -
x = x + 6; w
v
cout<<x;
v
x = x - 20;
cout<<x;
x a
=
1
+
x 19
=
10729
+
Printing Variables in C++ & Updation of Variables
x
Output
I
/int x = 5;
·
57 13 -
7
vcout<<x;
⑤x = 7;
-cout<<x;
vx = x + 6;
⑥
cout<<x;
/x = x - 20;
vcout<<x;
↓
Arithmetic operations on int data type
int x = 5;
+. -,
·
int y = 2; special
cout<<x+y;
cout<<x-y;
⑤ Δ
cout<<x*y; x
y
cout<<x/y;
5/2 2.5
= - 2
29/10 2.9-
=
2
Increment - Decrement operators and Comments
int x = 5; x +
+
E- x
x
=
1
+
x<+; #
+ 2
+
cout<<x;
x<-;
cout<<x;
<+x;
cout<<x;
–-x;
cout<<x;
-:
6
int x 5;
=
↳
cout << n ++ cendl;n
cont <<n;
vint x 5;
=
Xcout<(n;
Example : Take two integers input, a and b : a>b, and find the
- >
<
-
int a 10;
=
3π Divisor quotient
a
-
9
-
int b 3;
-
#
=
Remainder
int vi - -
*
bq
+
* =
2
a
Quotient Remainder
+
Dividend Divisor X
=
a b
q U
-
v
=
a -
q
b+
NB ππ
N q 2
I
v & Output
/
(b
a)
+
v a
= -
01
=
v
-
2 16
= -
(3* 5)
v 16
= -
15 1
=
Modulus Operator(%) if axb > a %ob-remainder
-
a be
=
2%4 2
int a=10;
=
"u
int b=4;
4- π
int Remainder = a%b ⑮ &
Y
<b 19
if then
②
a
-
16
a%b a
=
=
BOD MAS
Properties ↓
a%ob a 1
#
-
+
<b
2) if
=
a
+
2) a%( b)
- a%b
=
3)( a)%b
-
(a%ob)
=
u)(a)%( b) -
( a)%b
=
-
=
-
(a%b)
Float data type
float x = 3.1;
↓
T
2.5
2) Cout << 5.012;
2.5
3) cout <> S/2.0;
float y = 2;
cout<<x+y; - 7
cout<<x-y; - 3
cout<<x*y; -10
cout<<x/y; -2.5
Example : Calculating percentage of 5 subjects
float x1 = 90; </ x1 can be physics ↓
float x2 = 91; </ x2 can be chemistry xΔ
float pi = 3.1415;
64x3.14
float area = pi*radius*radius;
cout<<area;
A HV
=
H+
=
V r
+
+ *
3./4
=
r 2
Boolean data type
true 1
+ on
bool x = true;
=
false false 0
=
-
off
I
No to
#include<iostream>
using namespace std;
int main(){
int a = 0,b = 5;
</cout<<b<<endl;
cout<<a<<endl;
}
Variable Naming rules
1) Variables can start from an alphabet or underscore _ or $.
2) Special characters except _ and $ are not allowed.
3) Some particular keywords are not allowed.
4) Commas or blanks are not allowed.
floatn;
Variable Naming rules - Examples
Q. Which of the following are invalid variable names and why?
-x2.2
+
2
=
1000
float
=
p
27.5
floatv
=
t 3.5
float
=
floatsi;
Try This! Output
I
Predict the output : 05 1x
Error
int main(){
float a = 5, b = 2; ↓
a
int c;
c = a % b;
cout<<c;
return 0;
}
operator
asa
modulus -
What’s this line?
#include <iostream>
int main(){
return 0;
}
Compilation Process mingu vscode
↓
code, English/C++ Confiler computer
o's I's
MCQ Time !
MCQ 1
Which of the following statements is false
(1) Each new C++ instruction has to be written on a separate line false
(2) Usually all C++ statements are entered in small case letters true
(2) 3
v
(3) 2
(4) 0
it a;
a 5/2;
=
MCQ 3
What will be the value of d if d is a float after the operation
d = 2 / 7.0?
(1) 0
(2) 0.2857
v
zFs
xent
(3) Cannot be determined float
d =
floatd; &
d 2/7.0;
=
*
THANK YOU