0% found this document useful (0 votes)
14 views22 pages

LP02 - Programming Fundamentals

The document covers fundamental programming concepts in C++, including identifiers, keywords, data types, and basic operators. It presents various homework exercises and programming challenges, such as calculating mean values, checking Fibonacci membership, and implementing a lottery system. Additionally, it introduces control structures like if statements and while loops, along with examples of simple C++ programs.

Uploaded by

abhishek.09ab0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views22 pages

LP02 - Programming Fundamentals

The document covers fundamental programming concepts in C++, including identifiers, keywords, data types, and basic operators. It presents various homework exercises and programming challenges, such as calculating mean values, checking Fibonacci membership, and implementing a lottery system. Additionally, it introduces control structures like if statements and while loops, along with examples of simple C++ programs.

Uploaded by

abhishek.09ab0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Lecture-

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

Program to print “Hello


World”
#include <iostream>
using namespace
std; int main() {
cout << “Hello
world!”;
return 0;
}
Identifiers
 A C++ identifier is a name used to identify
a variable, function, class, module, or any
other user-defined item
 An identifier starts with a letter A to Z or a
to z or an underscore (_) followed by zero or
more letters, underscores, and digits (0 to 9)
Keyword
s Some reserve words which cannot be
used as identifiers
 These are basically part of the
grammar representing the language
 E.g: if, while, return, namespace, etc.
Data
types
 As we know we need variables to store
information.
 We might want to store information of
various types in a variable like
character, whole numbers, integers,
floating point, boolean etc.
 Based on the data type of a variable, th
e operating system allocates memory
and interprets the combination of 0s
and 1s in that memory
Primitive Data Types

 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 [ &&, | | , ! ]

PS - 1: Relational Operators and


Logical Operators always Evaluate to
0 or 1
PS – 2: For logical evaluation any non-zero
value is true.
1
9

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.

For, n>30 and n<50, person will win a “Bike”


For, n>100 and n<150, person will win a “Kurkure”
For, n>200 and n<400, person will win a “Macbook”
For, n>400 and n<500, person will win a “Car”
For all other values of n, print “Happy Birthday”
1
Lottery system 9

If the number is between 30 and 50 (inclusive):

Print "Bike"

If it's between 30 and 40 → Print "Splendor"

Else → Print "Hero"

If the number is between 100 and 110:

Print "KurKure"

If it's between 100 and 105 → Print "Normal Kurkure"

Else → Print "Red Kurkure"

If the number is between 321 and 421:

Print "Mac"

If it's between 321 and 389 → Print "M1 mac"

Else → Print "M2 mac"

If the number is between 500 and 721:

Print "Car"

If it's between 500 and 600 → Print "Thar"

Else → Print "Creta"

For any other number, print "Happy Birthday!!"


2
0

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 !

You might also like