Nexus Learn

Lecture 2: Variables Data Types IO

Lecture Slides CS-307 Programming Fundamentals 6 min read Updated 8 Nov 2026

↓ Download slides (PowerPoint)

Slide 2

Quick Recap of Lecture 1

5 minutes · check homework

Remember

Homework check: largest of three

  • IPO: Input → Process → Output
  • C++ uses a compiler
  • Algorithm → flowchart → code
  • Every program starts at main()
  • Every statement ends with ;
  • START
  • READ A, B, C
  • IF A>B AND A>C
  • PRINT A
  • ELSE IF B>C
  • PRINT B
  • ELSE
  • PRINT C
  • END

Module 0 · Recap

Programming Fundamentals · Lecture 2

Slide 3

Today's Plan

90 minutes · 6 modules

Recap & Homework Check

5 min

Variables: Storing Data in Memory

20 min

Data Types

20 min

Output with cout & Input with cin

20 min

Arithmetic & Complete Programs

20 min

Wrap-up & Homework

5 min

Slide 4

Learning Outcomes (SLOs)

By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)

Declare and initialise variables using valid names

SLO 2.1 → CLO4 · C3

Choose the right data type: int, float, double, char, bool, string

SLO 2.2 → CLO4 · C3

SLO 2.3 → CLO4 · C3

SLO 2.4 → CLO1 · C3

Take input with cin and display output with cout

Write complete programs that input, calculate and display results

  • CLO1 Identify the necessary properties of good problem-solving techniques.
  • CLO4 Describe the syntax and semantics of the C++ programming language.

Slide 5

What Is a Variable?

A named location in memory that stores a value which can change

int

float

char

string

3.45

'A'

"Ali"

age

cgpa

grade

name

Analogy: a variable is a labelled box. The label is the name, the box type is the data type, and what is inside is the value.

Module 1 · Variables

Programming Fundamentals · Lecture 2

Slide 6

Declaring & Initialising Variables

int

marks

=

;

data type

name

assignment

value

end

Declare

Initialise

Assign later

int marks;

int marks = 85;

marks = 90;

Box is created; value is garbage

Create and fill in one step

Old value is replaced

Module 1 · Variables

Programming Fundamentals · Lecture 2

Slide 7

Rules for Naming Variables

  • Letters, digits and underscore ( _ ) only
  • Must not start with a digit
  • No spaces or special symbols ( @ # - $ )
  • Cannot be a keyword (int, return, float …)
  • Case-sensitive: Marks and marks are different

Good habit: use meaningful names — totalMarks, not x

Module 1 · Variables

Programming Fundamentals · Lecture 2

Slide 8

Basic Data Types in C++

The type decides what kind of value the box can hold

Module 2 · Data Types

Programming Fundamentals · Lecture 2

Slide 9

How Much Memory Each Type Uses

Which one should I use?

Counting things

int

Money, CGPA, averages

double

A single letter / grade

char

Yes / no answer

bool

Names, text

string

Module 2 · Data Types

Programming Fundamentals · Lecture 2

Slide 10

Constants

A value that must never change after it is set

constants.cpp

  • Use the const keyword
  • Must be initialised when declared
  • Convention: UPPER_CASE names
  • Protects values from accidental change
  • const double PI = 3.14159;
  • const int MAX_MARKS = 100;
  • PI = 3.2; // ERROR!

Literal: a fixed value written directly in code — 100, 3.14, 'A', "Ali", true

Module 2 · Data Types

Programming Fundamentals · Lecture 2

Slide 11

Output with cout

Chain several items with <<

Escape sequences

output.cpp

  • int age = 19;
  • string name = "Ali";
  • cout << "Name: " << name << endl;
  • cout << "Age: " << age << "\n";

Output

  • Name: Ali
  • Age: 19

Module 3 · Input / Output

Programming Fundamentals · Lecture 2

Slide 12

Input with cin

Now the program can take data from the user

Keyboard

cin >>

age

user types 19

reads the value

stored in variable

input.cpp

Remember the arrows

  • int age;
  • cout << "Enter your age: ";
  • cin >> age;
  • cout << data goes OUT to screen
  • cin >> data goes IN to variable

Module 3 · Input / Output

Programming Fundamentals · Lecture 2

Slide 13

Program: Add Two Numbers

The algorithm from Lecture 1, now in real C++

Output

sum.cpp

  • #include <iostream>
  • using namespace std;
  • int main() {
  • int a, b, sum;
  • cout << "Enter first number: ";
  • cin >> a;
  • cout << "Enter second number: ";
  • cin >> b;
  • sum = a + b;
  • cout << "Sum = " << sum << endl;
  • return 0;
  • }
  • Enter first number: 7
  • Enter second number: 5
  • Sum = 12

Map to IPO

  • Input: cin >> a, b
  • Process: sum = a + b
  • Output: cout << sum

Module 3 · Input / Output

Programming Fundamentals · Lecture 2

Slide 14

Tracing the Program in Memory

Watch the boxes change line by line

? = garbage value. Never use a variable before giving it a value.

Module 3 · Input / Output

Programming Fundamentals · Lecture 2

Slide 15

Arithmetic Operators

+

-

*

/

%

Add

Subtract

Multiply

Divide

Remainder

7 + 2 → 9

7 - 2 → 5

7 * 2 → 14

7 / 2 → 3 (!)

7 % 2 → 1

Integer division trap

  • int / int gives an int — the decimal part is thrown away.
  • 7 / 2 = 3 but 7.0 / 2 = 3.5

Module 4 · Arithmetic & Programs

Programming Fundamentals · Lecture 2

Slide 16

Program: Average of Three Marks

Putting it all together

Output

average.cpp

  • int main() {
  • int m1, m2, m3;
  • cout << "Enter 3 marks: ";
  • cin >> m1 >> m2 >> m3;
  • int total = m1 + m2 + m3;
  • double avg = total / 3.0;
  • cout << "Total: " << total << endl;
  • cout << "Average: " << avg << endl;
  • return 0;
  • }
  • Enter 3 marks: 70 85 90
  • Total: 245
  • Average: 81.6667

Why 3.0 and not 3?

total / 3 would give 81 (integer division). Dividing by 3.0 keeps the decimal part.

Module 4 · Arithmetic & Programs

Programming Fundamentals · Lecture 2

Slide 17

Common Mistakes

Spot them before the compiler does

!

Wrong arrow direction

!

Using before assigning

cin << age;

int x; cout << x;

cin uses >>, cout uses <<

Prints a garbage value

!

Integer division

!

Wrong quotes

double a = 5 / 2;

char g = "A";

Gives 2, not 2.5

char needs single quotes 'A'

Module 4 · Arithmetic & Programs

Programming Fundamentals · Lecture 2

Slide 18

CLASS ACTIVITY · 10 MIN · IN PAIRS

Student Info Card

Write a program that inputs a student's roll number (int), grade (char) and CGPA (double), then prints them neatly with labels.

Sample output

Bonus

  • Roll No : 1024
  • Grade : A
  • CGPA : 3.45

Use \t to align the columns, and add a const for the university name.

Slide 19

Quick Recap

Answer aloud

Q1

What is a variable?

Q2

Which data type would you use for a CGPA? For a grade letter?

Q3

What is the difference between cout << and cin >>?

Q4

What is the result of 7 / 2 in C++? And 7 % 2?

Slide 20

Homework

Input length and width; print the area and perimeter of a rectangle

Input temperature in Celsius; print it in Fahrenheit (F = C × 9/5 + 32)

Input a number of seconds; print it as hours, minutes and seconds (use / and %)

Input two numbers; swap their values using a third variable and print them

Next lecture: Operators in depth — precedence, increment/decrement, type casting

← All CS-307 material