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

C++ Lecture 1 PPT

Uploaded by

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

C++ Lecture 1 PPT

Uploaded by

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

C++

Output and Variables


Lecture- 1

Raghav Garg
Today’s checklist

1) Basic Printing, \n, printing numbers also with + and -.


2) Variables, printing variables, int, float,bool,char and +,-,*,/ of
integers.
3) Modulus operator and increment ,decrement operator
4) Variables naming rules.
5) Comments in C++
Basic program in C++

#include<iostream>

using namespace std;

int main(){

cout<<“hello world”;
return 0;
}
How to move in next line?

Example :

cout<<“Hello PW”;

cout<<“Hello CW”;

Output will be ?
Use of escape sequence ‘\n’ and endl

Example :

cout<<“Hello PW”;

cout<<“\n”; or cout<<endl;

cout<<“Hello CW”;

Output will be ?
Use of escape sequence ‘\n’

Predict the output :


main(){

cout<<"nn\n\nnn\n";

cout<<"nn/n/nnn/n";

}
Printing Numbers (what computer thinks is a number
and what is a number)

Examples :

cout<<4;

cout<<4+3;

cout<<“4+3”;
Variables and their Declaration
Let us focus on int data type as of now.

1) Variables as containers :
Printing Variables in C++ & Updation of Variables
int x = 5;
cout<<x;
x = 7;
cout<<x;
x = x + 6;
cout<<x;
x = x - 20;
cout<<x;
Arithmetic operations on int data type
int x = 5;

int y = 2;

cout<<x+y;

cout<<x-y;

cout<<x*y;

cout<<x/y;
Increment - Decrement operators and Comments
int x = 5;
x<+;
cout<<x;
x<-;
cout<<x;
<+x;
cout<<x;
–-x;
cout<<x;
Example : Take two integers input, a and b : a>b, and find the
remainder when a is divided by b.
Modulus Operator(%)

Used to find the remainder

int a=10;
int b=4;
int Remainder = a%b
Float data type
float x = 3.1;
Arithmetic operations on float data type
float x = 5;

float y = 2;

cout<<x+y;

cout<<x-y;

cout<<x*y;

cout<<x/y;
Example : Calculating percentage of 5 subjects
float x1 = 90; </ x1 can be physics
float x2 = 91; </ x2 can be chemistry
float x3 = 92; </ x3 can be maths
float x4 = 93; </ x4 can be english
float x5 = 94; </ ohh wait comments ke baare me to bataya hi nahi xD
float percent = (x1 + x2 + x3 + x4 + x5)/5;
cout<<percent;
</ change the marks and run each time
Example : Calculating Area of a Circle
float radius = 5;

float pi = 3.1415;

float area = pi*radius*radius;

cout<<area;
Boolean data type
bool x = true;
Example : Find the output
#include<iostream>

using namespace std;

void main(){

bool a = true;

cout<<a<<endl;

}
Example : Find the output
#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.

auto double int break extern enum unsigned while


case sizeof for const static long continue float
else signed do short switch char volatile default
goto struct if union return void register typedef
Variable Naming rules - Examples
Q. Which of the following are invalid variable names and why?

BASICSALARY _basic basic-hra

#MEAN group. 422

population in 2006 over time mindovermatter

FLOAT hELLO queue.

team’svictory Plot#3 2015_DDay


Example : Calculating Simple Interest
float p,r,t,si;
p = 100;
r = 10;
t = 2;
si = (p*r*t)/100;
cout<<si;
Try This!

Predict the output :


int main(){
float a = 5, b = 2;
int c;
c = a % b;
cout<<c;
return 0;
}
What’s this line?

#include <iostream>
int main(){

return 0;

}
Compilation Process
MCQ Time !
MCQ 1
Which of the following statements is false
(1) Each new C++ instruction has to be written on a separate line

(2) Usually all C++ statements are entered in small case letters

(3) Blank spaces may be inserted between two words in a C++


statement

(4) Blank spaces cannot be inserted within a variable name


MCQ 2
If a is an integer variable, a = 5 / 2 ; will return a value
(1) 2.5

(2) 3

(3) 2

(4) 0
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

(3) Cannot be determined

(4) None of the above


THANK YOU

You might also like