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

C++ coding practice

Uploaded by

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

C++ coding practice

Uploaded by

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

Lab # 3

INTRODUCTION TO COMPUTER PROGRAMMING, CS-201


Topic: “Knowing the syntax of data types (int, float,
char and strings), usage of variables, constants and
how to declare the variable as constant.”

BOOK: Deitel & Deitel – C++ How to Program


(10 th Edition)
Chapter 2: 2.6
101 EC
05/06/2024 Total no. of slides = 9
Learning Objectives

After the lesson learners will learn about


Input stream in C++
Constants in C++
usage of variables and constants

Asst. Prof. Dr SALAHUDDIN, School of Humanities and Sciences (S H & S), NUST, PAF Asghar Khan Academey,
Input variable in C++

 Variable cin is predefined to denote an input stream from


the standard input device (the keyboard)
 The extraction operator >> called “get from”.
 Operator >> attempts to extract the next item from the
input stream and store its value in the right operand
variable.

SYNTAX
cin >> Variable >> Variable . . . ;

Asst. Prof. Dr SALAHUDDIN, School of Humanities and Sciences (S H & S), NUST, PAF Asghar Khan Academey,
Input variable in C++

cin statements can be linked together using >>


operator.
These examples yield the same output:

cin >> x;
cin >> y;

cin >> x >> y;

Asst. Prof. Dr SALAHUDDIN, School of Humanities and Sciences (S H & S), NUST, PAF Asghar Khan Academey,
Constants in C++
 Constants in C++ refer to variables with fixed values that
cannot be changed. Once they are defined in the program, they
remain constant throughout the execution of the program. They
are generally stored as read-only tokens in the code segment
of the memory of the program. They can be of any available
data type in C++ such as int, char, string, etc.
 Example
const int myNum = 15; // myNum will always be 15
myNum = 10; // error: assignment of read-only variable
'myNum'

Asst. Prof. Dr SALAHUDDIN, School of Humanities and Sciences (S H & S), NUST, PAF Asghar Khan Academey,
Example # 1: Write a C++ program to find the
perimeter and area of a rectangle.
 Pseudocode describes the
Pseudo Code: distinct steps of an algorithm
in a way that’s easy for
1. C++ program for perimeter & area. anyone with to understand.
 It is the representation of code
2. Initially all variables (Length, width, Perimeter and which can be understood by
Area) will be defined of float type. even a layman with some
school level programming
3. Input values will be obtained from the user by knowledge.

using “cin” function.

4. Formulas of perimeter and area will be defined


after it.

5. Required outputs will be displayed in the end.

Asst. Prof. Dr SALAHUDDIN, School of Humanities and Sciences (S H & S), NUST, PAF Asghar Khan Academey,
Source Code:

Asst. Prof. Dr SALAHUDDIN, School of Humanities and Sciences (S H & S), NUST, PAF Asghar Khan Academey,
OUTPUT:

Asst. Prof. Dr SALAHUDDIN, School of Humanities and Sciences (S H & S), NUST, PAF Asghar Khan Academey,
LAB EXERCISE 3: “Area and Circumference
code”

Q: Create a source code in C++ that is


 asking the user to input the circle's radius
 Calculating the circumference and area of a circle.
 Displaying the outputs.

Take pi = 3.14. Moreover, in built


value of pi can be used by writing
M_PI. Copy the math file “cmath”
in the beginning of the source
code.

Asst. Prof. Dr SALAHUDDIN, School of Humanities and Sciences (S H & S), NUST, PAF Asghar Khan Academey,

You might also like