Lab Manual 3 - PD
Lab Manual 3 - PD
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.)
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”.
7. Open the terminal in vscode with working directory. Run the ‘test.exe’ file use command ‘./test’ that will
display output to user
You have completed the first three steps, now we will see, how the algorithm can be transformed to the code in
C++ language.
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;
}
In the above flowcharts, you can see that we have selection symbol in C++ language in the following form, Focus
on syntax with details:
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;
}
if(h==w)
Programming Fundamentals Fall 2024 Page 5 of 15
{
cout<<"Given rectangle is square"<<endl;
}
return 0;
}
int sum = 0;
int x = 1;
while(x<=N)
{
sum = sum + x;
x= x+1;
cout<<sum;
return 0;
}
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;
}
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.
Digits of the Number N: Prompt user to input number N and display digits on separate line.
Maximum integer: Prompt user to enter multiple integers, and calculate maximum integer.
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.
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.
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.
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
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
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.
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
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.
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
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.
5. Write a C++ program to find Least Common Multiple (LCM) of two numbers
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”
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
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
*
**
***
****
*****