0% found this document useful (0 votes)
10 views15 pages

Lab Manual 3 - PD

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

Lab Manual 3 - PD

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

Programming Fundamentals

Lab Manual 3
Instructor: CLO:
Mr. Samyan Qayyum Wahla • CLO1
Ms. Rabeeya Saleem (GA) Registration Number:
Learning Objectives:
• Installation of VS code ___________________________________________
• Transition of Flow charts to C++ code Name:
• Understanding of Conditional Structures
• Understanding of Repetition Structures ___________________________________________

Guidelines/Instructions:
• Create meaningful variable names. Add comments for readability. Indent each line of your code.
• Plagiarism/Cheating is highly discouraged by assigning 0 to both who tried and one who shared his/her
code.
• Lab work must be evaluated during lab timing and homework is for submission.
• Contact your teacher and GAs during their office hours (mentioned at end of lab.)

Installation of Visual Studio Code:


So far, you have used notepad for
code editing and MinGW gcc
compiler from command prompt for
execution of C++ code. From now
onwards, we will be using visual
studio code which will integrate
code editing and compiler in the
single program. Follow the
following instruction for the
configuration of Visual Studio Code.
1. To install visual code, first
we have to download
installer from there official website link.
Link: https://code.visualstudio.com/download
2. After downloading, double click and run that file. Accept the agreement and click on Next to proceed

Programming Fundamentals Fall 2024 Page 1 of 15


3. Follow the wizard and complete the installation process.

4. To start with VScode, open the folder or start with new folder as shown

5. After successful installation, we have to download some extensions (adds-on) to setup vs code to
workplace with cpp. Open the extension window by
(ctrl+shift+X). These extensions are “C/C++ by
Microsoft” and Code Runner by Jun Han”.

Programming Fundamentals Fall 2024 Page 2 of 15


6. Start with ‘new file’ and save it with cpp extension as ‘test.cpp’. Write a c++ program that will take input
from user and display it to user. To run it, build the ‘test.cpp’ file that will create a ‘test.exe’ file in same
workplace.

7. Open the terminal in vscode with working directory. Run the ‘test.exe’ file use command ‘./test’ that will
display output to user

Programming Fundamentals Fall 2024 Page 3 of 15


Recipe for Problem Analysis:
Main task in development of programs is writing the algorithm for the given problem statement. Usually we
follow the following recipe for writing the C++ code.
1. Understanding of Problem Statement
2. Analyzing the requirements(input, output, computations and test cases).
3. Writing algorithm(Pseudo code/ flow chart)
4. Conversion of algorithm into code of any programming language

You have completed the first three steps, now we will see, how the algorithm can be transformed to the code in
C++ language.

Transition of Flow Charts to C++ Code


Flowcharts involving Selection Symbol:
Am I Square: Take values of height and width of a rectangle from user and print on the screen whether it is
square or not.
Note the corresponding sections of flow charts in C++ code of the same problem.

Flow Chart C++ code


#include<iostream>
using namespace std;

int main(){

int w;
cout<<"Please enter the width of rectangle";
cin>>w;

int h;
cout<<"Please enter the height of rectangle";
cin>>h;

if(h==w)
{
cout<<"Given rectangle is square"<<endl;
}
else
{
cout<<"Given rectangle is not square"<<endl;
}

return 0;
}

Programming Fundamentals Fall 2024 Page 4 of 15


Input Output Format:

PLEASE ENTER THE WIDTH OF RECTANGLE: 5


PLEASE ENTER THE HEIGHT OF RECTANGLE: 6
GIVEN RECTANGLE IS NOT SQUARE

In the above flowcharts, you can see that we have selection symbol in C++ language in the following form, Focus
on syntax with details:

if(condition) As in case of diamond, we have two branches.


{ Similarly in C++, we have two branches.
Statement1;
Statement2; In case of Yes, we execute first set of statements,
…… In case of No, second set of statements is executed.
StatementN;
}
else
{
Statement1;
Statement2;
……
StatementN;
}

In case of flowcharts, It is compulsory to have both branches, one for YES and second for NO. But in C++, it is
optional to have path for NO. so we can choose to write the first part only.

if(condition)
{
Statement1;
Statement2;
……
StatementN;
}

Lets change the above problem statement to the #include<iostream>


following: using namespace std;
Take values of height and width of a rectangle
from user and print on the screen whether it is int main(){
square. In case, the given rectangle is not square,
do not show any output. int w;
cout<<"Please enter the width of rectangle";
cin>>w;

Now the C++ code for the given problem int h;


statement can be written as: cout<<"Please enter the height of rectangle";
cin>>h;

if(h==w)
Programming Fundamentals Fall 2024 Page 5 of 15
{
cout<<"Given rectangle is square"<<endl;
}
return 0;
}

Input Output Format:

PLEASE ENTER THE WIDTH OF RECTANGLE: 5


PLEASE ENTER THE HEIGHT OF RECTANGLE: 5
GIVEN RECTANGLE IS SQUARE

Flowcharts involving Loop Symbol:


Sum of Integers upto N: Write a program that takes input N from user and calculate sum of integers upto N.
#include<iostream>
using namespace std;
int main(){
int N;
cout<<"Enter value of N";
cin>>N;

int sum = 0;

int x = 1;

while(x<=N)
{

sum = sum + x;

x= x+1;

cout<<sum;

return 0;
}

Programming Fundamentals Fall 2024 Page 6 of 15


Input Output Format:

ENTER VALUE OF N: 6
SUM OF INTEGER UPTO N IS: 21

Note the corresponding code for the condition involving loop. For the repeating condition(loop), following syntax
is used.
Important Note: Condition in the diamond in raptor is inversed in the C++ code.
In raptor we have used x>N, but in C++, x<=N condition is used.

while(condition)
{
Statement1;
Statement2;
……
StatementN;
}
Flowcharts involving Selection and Loop Symbol Both:
#include<iostream>
using namespace std;

int main(){

int N;
cout<<"Enter value of N";
cin>>N;

int sum = 0;

int x = 1;

while(x<=N)
{
if(x%2==0)
{
sum = sum + x;
}

x= x+1;
}

cout<<sum;

return 0;
}

Programming Fundamentals Fall 2024 Page 7 of 15


Input Output Format:

ENTER VALUE OF N: 6
SUM OF EVEN INTEGER IS: 12

We can have Loop within selection, selection within loop, Loop within loop, selection within selection, all of
these cases are known as Nested Statements.
Lets Dive in:
Convert following flowcharts to C++ code.
Greatest of the three: Take 3 numbers from the user, and print the number greatest of the three.

Input Output Format:

ENTER FIRST NUMBERS: 12


ENTER SECOND NUMBERS: 30
ENTER THIRS NUMBERS: 6
LARGEST NUMBER: 30

Programming Fundamentals Fall 2024 Page 8 of 15


Factorial of Number: Take a number N from user and calculate its factorial.

Input Output Format:

ENTER A POSITIVE INTEGER: 6


FACTORIAL OF 6 = 720

Digits of the Number N: Prompt user to input number N and display digits on separate line.

Programming Fundamentals Fall 2024 Page 9 of 15


Input Output Format:

ENTER A NUMBER TO SEPARATE IT'S DIGITS: 12594


49521

Maximum integer: Prompt user to enter multiple integers, and calculate maximum integer.

Input Output Format:

ENTER NUMBER: 12
ENTER NUMBER: 6
ENTER NUMBER: 18
ENTER NUMBER: 20
ENTER NUMBER: -1
MAXIMUM INTERGER IS: 20

Note: Develop logic on paper and then start writing your code. There is no formula involved in any of the
following questions. Logic creation is the task of the programmer. If you are having trouble developing it, use
paper and think how you will solve the problem without computer. Enlist the steps and make sure the sequence
of steps takes you to the solution of the problem. Once you have finalized the steps, translate your steps into
C++. Also, do not develop logic with friends or see their code. This will not help in exercising your brain.
Eventually, it will make you lose your confidence of developing programs on your own. More practice, better
results.

Programming Fundamentals Fall 2024 Page 10 of 15


Lab Tasks:
1. Write a program that inputs three digits and displays all possible combinations of these digits

ENTER THREE DIGIT INPUT: 0 9 5


095
059
905
950
509
590

2. Senior salesperson is paid Rs. 400 a week, and a junior salesperson is paid Rs. 275 a week. Write a program
that accepts as input a salesperson’s status in the character variable status. IF status is ‘s’ or ‘S’ the senior
person’s salary should be displayed; if status is ‘j’ or ‘J’, the junior person’s salary should be displayed,
otherwise display error message.

ENTER YOUR STATUS


ENTER 'S' OR 'S' FOR SENIOR PERSON'S SALARY
ENTER 'J' OR 'J' FOR JUNIOR PERSON'S SALARY.
J
YOUR SALARY IS RS.275 PER WEEK.

3. In a right triangle, the square of the length of one side is equal to the sum of the squares of the lengths of
the other two sides. Write a program that prompts the user to enter the lengths of three sides of a triangle
and then outputs a message indicating whether the triangle is a right triangle.

ENTER SIDE-1 LENGTH OF TRIANGLE: 3


ENTER SIDE-2 LENGTH OF TRIANGLE: 4
ENTER SIDE-3 LENGTH OF TRIANGLE: 5
GIVEN TRIANGLE IS RIGTH-ANGLE TRIANGLE

4. Write a C++ program that finds the sum of the first n positive integers using while loop. For example, if user enters
3 then output will be 1+2+3 = 6

ENTER POSITIVE INTEGER NUMBER: 50


SUM IS: 1275

5. Air quality index is the measurement of the quality of air we are breathing in. If it is more than a certain
limit, it becomes hazardous for health. AQI for Lahore in certain locations is in critical zone. Write a C++
program which takes location name as input and tells the user about the AQI of that location. You must
also inform him about the quality of the air i.e. good, bad, hazardous etc. You must look on internet about

Programming Fundamentals Fall 2024 Page 11 of 15


the AQIs for different locations. You will be using following locations for finding AQIs i.e. Model town,
Johar town, Iqbal town, Shahdara, UET, GT Road, Samanabad, Defence Phase I, Askari X, Bahria Town
and Shalamar.

ENTER LOCATION NAME: UET


AQI FOR UET IS 220 HAZARDOUS

6. Write a C++ program to print the multiplication table of a number ‘n’ up to 10 rows using while loop. For example,
if user enters 5 then you must print table of 5 up to 10 rows i.e. 5 x 1 = 5 …. 5 x 10 = 50

ENTER INTEGER: 6
6*1=6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60

7. Write a C++ program to print all the even numbers from 1 to certain number entered by user. For example, if user
enters 10 then output will be 2, 4, 6, 8, 10

ENTER INTEGER: 27
2,4,6,8,10,12,14,16,18,20,22,24,26

8. Write a C++ program to count total digits in each integer using while loop. For example, if user enters 5678 then
output will be 4.

ENTER INTEGER: 589645


NUMBER OF DIGITS ARE: 6

9. Write C++ program to calculate the factorial of number and ask user to enter Y if he wants to try another number
or N if he wants to terminate the program. For example, if user enters 4 then output of the program will be 4*3*2*1
= 24 and user will be asked to type “Y/N”. If user enters Y, then you will again ask for ‘n’ and recalculate the
factorial. You will keep on doing it until users enters ‘N’. If user enters ‘N’, program should exit

Programming Fundamentals Fall 2024 Page 12 of 15


ENTER FACTORIAL OF NUMBER: 4
COMPUTED FACTORIAL IS: 24
DID YOU WANT TO CALCULATE AGAIN (Y/N) ? : Y
ENTER FACTORIAL OF NUMBER: 6
COMPUTED FACTORIAL IS: 720
DID YOU WANT TO CALCULATE AGAIN (Y/N) ? : N
SYSTEM STOP!

10. Write a C program to find Highest Common Factor (HCF/ GCD) of two numbers. For example, if user enters 4 and
64 then greatest common divisor (GCD) for the given values will be 4.

ENTER FIRST NUMBER: 98


ENTER SECOND NUMBER: 56
GCD IS: 14

Home Tasks:
1. Write a program that mimics a calculator. The program should take as input two integers and the operation
to be performed. It should then output the numbers, the operator, and the result. You must provide support
for at least 15 operators (+, -, /, *, &&, ||, &, |, % etc.) For division, if the denominator is zero, output an
appropriate message. Some sample output: Input will be 3, 4 and operator = 1 (+), output will 3 + 4 = 7

2. Write this program using if/else

WELCOME TO CALCULATOR
ENTER OPERATOR (+, -, *, /, %, &&, ||, >>, <<, !=, ==, <=, >= ): *
ENTER FIRST NUMBER: 6
ENTER SECOND NUMBER: 8
CALCULATED VALUE IS: 48

3. Write a program to find Roots of Quadratic Equations. For a quadratic equation ax2+bx+c = 0 (where a,
b and c are coefficients), it's roots is given by following the formula.
Programming Fundamentals Fall 2024 Page 13 of 15
The term b2-4ac is known as the discriminant of a quadratic equation. The discriminant tells the nature of
the roots.
o If discriminant is greater than 0, the roots are real and different.
o If discriminant is equal to 0, the roots are real and equal.
o If discriminant is less than 0, the roots are complex and different.

ENTER COEFFICIENTS A: 4
ENTER COEFFICIENTS B: 5
ENTER COEFFICIENTS C: 1
ROOTS ARE REAL AND DIFFERENT.
X1 = -0.25
X2 = -1

4. Write a program which asks the user about his/her registration number and tells him about its validity.
There are three possible formats for registration number: 2020-CS-1, 2020-R/2019-CS-1 and 2020-CD-
CS-1. Roll number can be a number between 1 to 260 and year can be anything between 2015 to 2019. If
user enters any registration number other than this format, then a proper error message should be shown
to the user. If registration number is according to the format, you should also convey the user that this is
a valid registration number. For example: if user types 2020CS53, your program should generate a proper
error message, explaining that he did not write in proper format.

ENTER YOUR REGISTRATION NUMBER: 2020-CS-100


2020-CS-100 IS VALID REGISTRATION NUMBER

5. Write a C++ program to find Least Common Multiple (LCM) of two numbers

ENTER FIRST NUMBER: 2


ENTER SECOND NUMBER: 5
LCM IS: 10

6. Write a C++ program to check whether a number is prime number or not. For example, if user enters 2
then output of the program is “Prime”. If user enters 1024 then output is “Not Prime”

ENTER NUMBER: 1024


1024 IS NOT A PRIME NUMBER

Programming Fundamentals Fall 2024 Page 14 of 15


7. Write a C++ program to print all prime numbers between 1 to n. For example, if user enters n = 10 then
output of the program will be “2,3 , 5, 7”

ENTER NUMBER FOR PRIME NUMBER LIST: 20


PRIME NUMBER ARE: 2,3,5,7,11,13,17,19

8. Write a C++ program to find sum of all prime numbers between 1 to n. For example, if user enters n = 10
then output of the program will be 2+3+5+7 = 17

ENTER NUMBER: 30
SUM OF PRIME NUMBER IS: 129

9. Write a C++ program to find all prime factors of a number

ENTER NUMBER: 28
PRIME FACTORS ARE: 2,7

10. Write a C++ program, which takes ‘n’ as input and print a pattern with n lines using while loops. For
example, if user enters 6 then output of the program is shown below:
*
**
***
****
*****
******

ENTER NUMBER: 5
*
**
***
****
*****

Programming Fundamentals Fall 2024 Page 15 of 15

You might also like