LP02 - Programming Fundamentals
LP02 - Programming Fundamentals
2
FUNDAMENTALS - I
• Programming
Fundamentals in C+
+
Kartik Mathur
Homework?
Given a list of N integers, find mean,
maximum and minimum value. You
would be given first N, and then N
integers of the list.
Given a number check if it is a member
of
Fibonacci sequence or not
BT – 2: Apples and
Oranges
There are three closed and opaque
cardboard boxes. One is labeled "APPLES",
another is labeled "ORANGES", and the last is
labeled "APPLES AND ORANGES". You know
that the labels are currently misarranged,
such that no box is correctly labeled. You
would like to correctly rearrange these labels.
To accomplish this, you may draw only one
fruit from one of the boxes. Which box do you
choose, and how do you then proceed to
rearrange the labels?
BT – 3: Average
Salary
Three coworkers would like to know their
average salary. However, they are self-
conscious and don't want to tell each other
their own salaries, for fear of either being
ridiculed or getting their houses robbed. How
can they find their average salary, without
disclosing their own salaries?
Time to write our first
program!
8
Boolean - bool
Character - char
Integer – int
Floating Point – float
Double Floating Point – double
Data type
modifiers
Several of the basic types can be
modified
using one or more of these type modifiers
signed
unsigned
short
long
Variables
C++ is strongly typed language, so every
variable must be defined before using it.
type variablelist; // type is the type (eg. int),
varName is the name of the variable
e.g:
i. int sum;
ii. char ch;
iii. float a, b;
Variables when just declared have garbage
value until they are assigned a value for the
first time
We can assign a specific value from the
moment variable is declared, called as
initialization of variable.[float b = 0.0;]
1
8
Basic Operators in a
Expression
Unary [ +, - ]
Arithmetic [ +, -, /, *, % ]
Brackets [ () ]
Assignment [ = ]
Relational [ ==, !=, >,<, >=, <=]
Logical Operators [ &&, | | , ! ]
If
Block
Single If
if (a > 10) {
cout << “Hello!”;
}
If Else
If (a>10) {
cout << “Hello!”;
} else {
cout << “World.”;
}
If .. Else If .. Else
If (a>10 && a <20) {
cout << “Hello!”;
} else if (a >20 && a <30)
{ cout << “Hello
World!”;
} else {
cout << “Welcome to
Coding Blocks”;
}
1
9
Lottery system
Person will win as per following range.
Print "Bike"
Print "KurKure"
Print "Mac"
Print "Car"
While
block
while( condition is true )
{
//do some stuff
}
Lets write code!
Read P,R,T and calculate SI
Find largest of N numbers
Check if a number is prime or not
Write a program to print the following
pattern, n = 4
*
**
***
****
Homework?
Write a program to print the
following
Pattern, n = 5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Homework?
Write a program to print the following
pattern
1
23
45 6
7 8 9 10
Print all prime numbers between 2 to N
2
2
Recap
Program Always starts with main()
{} are used to enclose a block (function,
if, while etc.).
C++ Compiler Ignores whitespace (space,
carriage returns, linefeeds, tabs, vertical
tabs, etc.)
Output using cout
Input using cin
Header Files
Comments (// & /*… */)
Thank
You !