C++ in 10 Hours.pdf
C++ in 10 Hours.pdf
C++ Programming
in One Shot
Chapter 1 :
Basics
+91 9548457683
1) Basic Printing, \ n and endl wali cheez.
2) Variables, printing variables, int, float, and +,-,*,/ of
integers.
3) Variable naming rules.
4) Comments
5) Taking Input
6) Modulus Operator
7) Float to int, int to float
8) Hierarchy
9) Char and ASCII
Basic program in C+
+
+91 9548457683
#include<iostream>
using namespace
std; int
main(){
cout <“hello
world”;
}
How to move in next
line?
+91 9548457683
Example :
Output will be :
+91 9548457683
Example :
+91 9548457683
now.
1) Variables as containers :
Printing Variables in C & Updation of
Variables
i n t x = 5;
+91 9548457683
cout <x
<endl; x =
7;
cout <x
<endl; x =
x + 6 ; cout
<x <endl; x
= x - 20;
cout <x
+91 9548457683
Arithmetic operations on int data
type
i n t x = 5;
+91 9548457683
i n t y = 2;
cout <x+y
<endl; cout
<x*y <endl;
cout <x/y
<endl; / i ssue
Increment - Decrement
operators
i n t x = 5;
+91 9548457683
x +;
cout <x
<endl; x - ;
cout <x
<endl;
+x;
cout <x
<endl; – - x;
cout <x
<endl;
+91 9548457683
float data type
fl o a t x = 3 . 1 ;
+91 9548457683
Arithmetic operations on float data
type
fl o a t x = 5 ;
+91 9548457683
fl o a t y = 2 ;
cout <endl;
<x+y
cout <x-y <endl;
+91 9548457683
Example : Calculating Simple Interest
+91 9548457683
Homework : Calculate Volume of a
Sphere
+91 9548457683
Variable Naming
rules
1) Variables can s t a r t from an alphabet or underscore _ or
+91 9548457683
$ .
2) Special characters except _ and $ are not allowed.
3) Some p a r t i c u l a r keywords are not allowed.
4) Commas or blanks are not allowed.
Auto double int break extern enum unsigned while
case sizeof for const s t a t i c long continue fl o a t
else signed do short switch char v o l a t i l e default
goto struct if union return void re g i s t e r typedef
Variable Naming rules -
qExamples
. Which of the following are i n v a l i d v a r i a b l e names and
+91 9548457683
why?
BASICSALARY _basic basic-hra
Plot#3
Taking input // Square of a
Number
+91 9548457683
Take 2 numbers input from user and print
their Sum
+91 9548457683
Modulus Operator
+91 9548457683
Modulus Operator
+91 9548457683
Typecasting
+91 9548457683
Hierarchy of
operators
int i = 2 * 3 / 4 ;
+91 9548457683
cout < i ;
char data
type
char ch = ‘ a ’ ;
+91 9548457683
ASCII
values
char ch = ‘ a ’ ;
+91 9548457683
+91 9548457683
MCQ Time !
Homewor
k
In b = 6.6 / a + 2 * n ; which operation will be performed
+91 9548457683
first?
(1) 6.6 / a
(2) a + 2
(3) 2 * n
(4) Depends upon compiler
MCQ
Which of the following statements is
+91 9548457683
false
(1) Each new C++ instruction has to be written on a separate
line
+91 9548457683
The expression, a = 7 / 22 * ( 3.14 + 2 ) * 3 / 5 ; evaluates to
(1) 8.28
(2) 6.28
(3) 3.14
( 4 ) 0
+91 9548457683
MCQ
The expression x = 4 + 2 % - 8
+91 9548457683
evaluates to
(1) -6
(2) 6
(3) 4
( 4 ) None of the
above
**MCQ
What will be the value of d if d is a float after the
+91 9548457683
operation d = 2 / 7?
(1) 0
(2) 0.2857
+91 9548457683
2) nested
3) Else if
ladder
4) Ternary
5) switch
+91 9548457683
IF - ELSE
Ques : Take positive integer input
and tell if it is even or odd
+91 9548457683
+91 9548457683
HW : Take positive integer input
and tell if it is divisible by 5 or
+91 9548457683
not.
Ques : Take integer input and
print the absolute value of that
+91 9548457683
integer
*Ques : If cost price and selling price of an
item is input through the keyboard, write a
program to determine whether the seller has
made profit or incurred loss or no profit no loss.
Also determine how much profit he made or
loss he incurred.
HW : Given the length and breadth of a
rectangle, write a program to find
+91 9548457683
whether the area of the rectangle is
greater than its perimeter.
Multiple Conditions
+91 9548457683
Using && and ||
Ques : Take positive integer input
and tell if it is a three digit
+91 9548457683
number or not.
Ques : Take positive integer input
and tell if it is divisible by 5 and
+91 9548457683
3.
Ques : Take positive integer input
and tell if it is divisible by 5 or 3.
+91 9548457683
Ques : Take 3 numbers input and
tell if they can be the sides of a
+91 9548457683
triangle.
Ques : Take 3 positive integers
input and print the greatest of
+91 9548457683
them.
Homework : Take 3 positive integers
input and print the least of them.
+91 9548457683
HW : Take positive integer input
and tell if it is divisible by 5 or 3
+91 9548457683
but not divisible by 15.
+91 9548457683
Nested If - Else
Ques : Take 3 positive integers
input and print the greatest of
+91 9548457683
them.
HW : If the ages of Ram, Shyam and
Ajay are input through the keyboard,
+91 9548457683
write a program to determine the
youngest of the three.
+91 9548457683
Else If
Ques : Take input percentage of a student
and print the Grade according to marks:
+91 9548457683
or at the origin, viz. (0, 0).
Ternary Operator
+91 9548457683
expression 1 ? expression 2 : expression
3
+91 9548457683
Switch Statement
switch ( integer expression )
{ case constant 1 :
do this ;
case constant 2
: do this ;
case constant 3
: do this ;
default :
do this ;
}
Ques : Write a program to create a calculator
that performs basic arithmetic operations
(add, subtract, multiply and divide) using
+91 9548457683
switch case and functions. The calculator
should input two numbers and an operator
from user.
+91 9548457683
MCQ Time !
Predict the
output
main( ) {
+91 9548457683
int x = 10, y =
20 ;
if ( x ==
y) ;
cout<<x<<”
“<<y ;
}
HW : Predict the
output
int main( ) {
+91 9548457683
int x = 3, y =
5 ; if ( x ==
3)
printf ( "\n%d",
x ) ; else ;
printf ( "\n
%d", y ) ;
}
*Predict the
output
+91 9548457683
m a i n ( ) {
int x = 3, y, z ;
y = x = 10 ;
z = x < 10 ;
}
Chapter 3 :
LOOPS
1) For Loop
+91 9548457683
2) Break and Continue
3) While and Do While Loop
4) Questions using
Operators
5) Pattern Printing Problems
+91 9548457683
What and
Why?
For Loop
+91 9548457683
for(int i = 1; i<10; i+
+){
// code
}
Ques : Print hello world ‘n’ times. Take
‘n’ as input from user
+91 9548457683
How for loop works : the various
parameters.
+91 9548457683
Ques : Print numbers from 1
to 100
+91 9548457683
Ques : Print all the even numbers from
1 to 100
+91 9548457683
HW : Print all the odd numbers from 1
to 100
+91 9548457683
Ques : Print the table
of 19.
+91 9548457683
HW : Print the table of ‘n’. Here ‘n’ is a
integer which user will input.
+91 9548457683
Ques : Display this AP - 1,3,5,7,9..
upto ‘n’ terms.
+91 9548457683
Ques : Display this AP - 1,3,5,7,9..
upto ‘n’ terms.
+91 9548457683
HW : Display this AP - 4,7,10,13,16..
upto ‘n’ terms.
+91 9548457683
Ques : Display this GP - 1,2,4,8,16,32,..
upto ‘n’ terms.
+91 9548457683
HW : Display this GP - 3,12,48,.. upto ‘n’
terms.
+91 9548457683
+91 9548457683
Break;
Ques : WAP to find the highest
factor of a number ‘n’ (other than
+91 9548457683
n itself)
Ques : WAP to check if a number
is composite or not.
+91 9548457683
Ques : WAP to check if a number is
prime or not.
+91 9548457683
+91 9548457683
Continue;
Ques : WAP to print odd numbers
from 1 to 100.
+91 9548457683
While Loop
+91 9548457683
int i = 0;
while(i<10)
{
// code
i++;
}
Do- While
Loop
+91 9548457683
do {
//code
+91 9548457683
int x = 1 ;
while ( x ==
1 ) x=x- 1 ;
cout<<x<<endl
;
}
Predict the
output
main( ) {
+91 9548457683
int i ;
while ( i = 10 )
{ cout<<i<<e
ndl; i = i + 1 ;
}
}
Predict the
output
main( )
+91 9548457683
{
while ( 'a' < 'b' )
cout<< “malayalam is a palindrome"
<<endl ;
}
HW : Predict the
output
main( ) {
+91 9548457683
int i = 10 ;
while ( i = 20
)
printf ( "\nA
computer
buff!" ) ;
}
HW : Predict the
output
main( ) {
+91 9548457683
int x = 4, y = 0;
while ( x >= 0 )
{
x-- ;
y+
+;
if ( x
==
y)
c
o
n
t
+91 9548457683
Questions using
Operators
+- *
/
Ques : WAP to count digits of a
given number.
+91 9548457683
Ques : WAP to count digits of a
given number.
+91 9548457683
Ques : WAP to print sum of digits of a
given number.
+91 9548457683
Ques : WAP to print sum of digits of a
given number.
+91 9548457683
HW : WAP to print product of digits of a
given number.
+91 9548457683
HW : WAP to print sum of all the even
digits of a given number.
+91 9548457683
Ques : WAP to print reverse of a
given number.
+91 9548457683
Ques : WAP to print reverse of a
given number.
+91 9548457683
HW : WAP to print the sum of given
number and its reverse.
+91 9548457683
Ques : Print the factorial of a given
number ‘n’.
+91 9548457683
HW : Print the factorials of first ‘n’
numbers
+91 9548457683
HW : Write a program to print all the ASCII
values and their equivalent characters of 26
alphabets using a while loop.
+91 9548457683
Pattern Printing
Questions
****
****
****
Ques : Print the given
pattern
+91 9548457683
****
****
*
*
Solid
Rectangle
Ques : Print the given
pattern
+91 9548457683
****
****
Solid
Square
Ques : Print the given
pattern
+91 9548457683
123
4
123
4
123
4
123
4
Number
Square
+91 9548457683
Ques : Print the given
pattern
+91 9548457683
A B C
D A B
C D A
B C D
A B C
D
Alphabet
Square
+91 9548457683
Ques : Print the given
pattern
+91 9548457683
**
****
Star Triangle
Ques : Print the given
pattern
+91 9548457683
1
12
123
123
4
Number Triangle
HW : Print the given
pattern
+91 9548457683
A
A
B
A
B
C
A
B
Alphabet Triangle
C
+91 9548457683
*HW : Print the given
pattern
+91 9548457683
1
AB
12
3
ABC
D123
45
Alphabet Triangle
Ques : Print the given
pattern
+91 9548457683
*
***
*
*
Star Triangle
Ulta
HW : Print the given
pattern
+91 9548457683
123
4
123
12
1
+91 9548457683
ABC
DAB
C
A
B
A
+91 9548457683
1
13
135
135
7
+91 9548457683
1
23
456
789
10
Floyd’s Triangle
*Ques: Print the given
pattern
+91 9548457683
1
01
101
010
1
0&1
Triangle
Ques : Print the given
pattern
+91 9548457683
*
*
***
**
Star
Plus
HW : Print the given
pattern
+91 9548457683
*
**** *
******
Hollow
Rectangle
HW : Print the given
pattern
+91 9548457683
** **
*
*
* * *
Star
Cross
*Ques: Print the given
pattern
+91 9548457683
*
**
***
****
Star Triangle
Reverse
HW : Print the given
pattern
+91 9548457683
1
12
123
123
4
Number Triangle
Reverse
HW : Print the given
pattern
A
+91 9548457683
A
B
AB
C AB
CD
Alphabet Triangle
Reverse
HW : Print the given
pattern
+91 9548457683
*****
*
****
Rhombus
*Ques: Print the given
pattern
+91 9548457683
***
******
*
Star
Pyramid
HW : Print the given
pattern
1
+91 9548457683
123
12345
123456
7
Number
Pyramid
HW : Print the given
pattern
A
+91 9548457683
AB
C
ABCDE
ABCDE
F
G
Alphabet
Pyramid
**Ques : Print the given
pattern
+91 9548457683
***
******
* * *
***
*
Star
Diamond
+91 9548457683
Functions
+91 9548457683
What and
Why?
Basic syntax
+91 9548457683
fun(){
// code
}
+91 9548457683
+91 9548457683
main(
return
)
;
Kaam ki baate :
1) main() ek hi baar aata
hai.
2) Starts with main
3) unlimited functions
How functions work : ek ke andar
doosra, doosre ke andar teesra
+91 9548457683
+91 9548457683
Return type : Sum function se
samajte hai
+91 9548457683
Library functions
+91 9548457683
Ques : Combination and
Permutation
+91 9548457683
Ques : Pascal
triangle
+91 9548457683
Scope of variable
+91 9548457683
Formal parameters and Actual
Parameters
+91 9548457683
Ques : Swap 2
numbers
+91 9548457683
Ques : Swap 2
numbers
+91 9548457683
Pass by value & Pass by
reference
+91 9548457683
+91 9548457683
*Pointers
+91 9548457683
*Pointers
+91 9548457683
*Pointers
+91 9548457683
*Pointers
State TRUE or FALSE
+91 9548457683
State TRUE or FALSE
+91 9548457683
return statement.
3) A function m a y contain more than one
return statements.
State TRUE or FALSE
+91 9548457683
+91 9548457683
Recursion
Function calling itself
+91 9548457683
Ques : Print n
to 1
+91 9548457683
+91 9548457683
+91 9548457683
Ques : Print 1
to n
+91 9548457683
Ques : Print 1 to n (after
recursive call)
+91 9548457683
+91 9548457683
Ques : Print sum from 1 to n
(Parameterised)
+91 9548457683
Ques : Print sum from 1 to n (Return
type)
+91 9548457683
+91 9548457683
Ques : Make a function which
calculates the factorial of n using
+91 9548457683
recursion.
Ques : Make a function which
calculates the factorial of n using
+91 9548457683
recursion.
Ques : Make a function which
calculates ‘a’ raised to the power ‘b’
+91 9548457683
using recursion.
*Multiple Calls
Ques : Write a function to calculate the
+91 9548457683
nth fibonacci number using recursion.
+91 9548457683
HW : Power function
(logarithmic)
+91 9548457683
+91 9548457683
Arrays
What is an array?
+91 9548457683
Syntax and Declaration
+91 9548457683
Accessing Elements of Array
+91 9548457683
Printing Output and Taking Input
+91 9548457683
Ques : Are the following array declarations
correct?
+91 9548457683
int a (25) ;
int size = 10, b[size]
;
int c = {0,1,2} ;
Ques : Which element of the array does
this expression reference?
+91 9548457683
num[4]
Memory Allocation in Arrays
+91 9548457683
Passing Array to Functions
+91 9548457683
Ques : Calculate the sum of all the
elements in the given array.
+91 9548457683
Homework : Calculate the product of all the
elements in the given array.
+91 9548457683
Ques : Find the maximum value out of all the
elements in the array.
+91 9548457683
Homework : Find the minimum value out of
all the elements in the array.
+91 9548457683
HW : What is the difference between the 5’s in
these two expressions?
int num[5] ;
num[5] = 11
;
1. first is
particular
element,
second is
type
2. first is
State TRUE or
FALSE :
1. The array int num[26] has twenty-six elements.
+91 9548457683
2.The expression num[1] designates the first
element in the array
3.It is necessary to initialize the array at the
time of declaration.
4.The expression num[27] designates
the twenty-eighth element in the array.
HW : Given an integer n. Create an array
containing squares of all natural numbers
+91 9548457683
till n and print the elements of the array.
Ques : Given an array of integers, change
the value of all odd indexed elements to its
+91 9548457683
second multiple and increment all even
indexed value by 10.
Ques : Count the number of elements in given
array than a given number
greater
x.
+91 9548457683
HW : Find the difference between the sum of
elements at even indices to the sum of elements
at odd indices.
+91 9548457683
Ques : Find the second largest element in
the given Array.
+91 9548457683
Ques : Write a program to copy the
contents of one array into another in the
+91 9548457683
reverse order.
Ques : Write a program to reverse the
array without using any extra array.
+91 9548457683
Homework : If an array arr contains n
elements, then check if the given array is
+91 9548457683
a palindrome or not.
+91 9548457683
2D Arrays
What and
Why?
+91 9548457683
So far we have explored arrays with only one dimension. It is also
possible for arrays to have two or more dimensions. The two
dimensional array is also called a matrix.
int arr[r][c];
+91 9548457683
int arr[4][2] = { { 1234, 56Array
}, { 1256, 43 }, { 1434, 32 }, { 1312,
96 } } ;
+91 9548457683
Ques : Write a program to store 10 at every
index of a 2D matrix with 5 rows and 5 columns.
+91 9548457683
Ques : Write a program to add two
matrices.
+91 9548457683
Ques : Find the maximum element in a
given matrix.
+91 9548457683
HW : Find the minimum element in a
given matrix.
+91 9548457683
HW : Find the sum of all elements in
a given matrix.
+91 9548457683
HW : Find the product of all
elements in a given matrix.
+91 9548457683
HW : Given a matrix ‘a’ of dimension n x m
and 2 coordinates (l1, r1) and (l2, r2). Return
the sum of the rectangle from (l1,r1) to (l2, r2).
+91 9548457683
Ques : Write a program to Print the
transpose of the matrix entered by the
+91 9548457683
user.
+91 9548457683
+91 9548457683
Strings
What are strings
and Why are
+91 9548457683
they used?
Declaration of Strings
+91 9548457683
and taking Input
Indexing of
characters in
+91 9548457683
Strings
Ques : Input a string of length n and
count all the vowels in the given
+91 9548457683
string.
Updation of a
single character
+91 9548457683
in string
Ques : Input a string of size n and
the evenall
Update positions in the string to
character ‘a’. Consider 0-based indexing.
+91 9548457683
Built-in string functions
+91 9548457683
size() / length()
+91 9548457683
push_back() &
append()
+91 9548457683
pop_back() &
clear()
+91 9548457683
+91 9548457683
“+” operator
+91 9548457683
reverse()
Ques : Input a string of even
length and reverse the first half
+91 9548457683
of the string.
Ques : Input a string of length greater than 5
reverse
and the substring from position 2 to 5
using inbuilt functions.
+91 9548457683
+91 9548457683
to_string()
Ques : Return the total number of
digits in a number without using any
+91 9548457683
loop.
Hint : Try using inbuilt to_string()
function.
+91 9548457683
stoi
+91 9548457683
Thank
you!