Programing practicals

Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

Practical 1

1. Introduce general look and feel of Visual studio Community Edition 2022,
especially build menu and Solution Explorer. Also practice Create Project
of type “C++ Console App” using “create new solution”.

Step1-

Installation of Visual Studio:


1. Make sure your computer is ready for Visual Studio:
Check the system requirements. These requirements help you know whether your
computer supports Visual Studio 2022. Check your computer has space.
2. Install Visual Studio:
If you are installing Visual Studio then choose the community edition of Visual Studio for
downloading.

3. Choose workloads:
After the installer is installed, you can use it to customize your installation by
selecting the Desktop Development C++ or sets feature.After you choose the
workload(s) and optional components you want, choose Install. Desktop
Development C++ have the features to compile ,execute and run the programs of C++
language.
● After Visual Studio installation is complete, choose the Launch button to
get started developing with Visual Studio.

Step2-
Creating a new project:
1- Open Visual Studio and choose create a new project.

2- In the list of project templates, select Console App, then select Next.
3- In the Configure your new project dialog box, select the Project name text box, name
your new project , then select Create.
4-The program already written in console app will help you to understand how to write a
program.
Repeat these steps

2.Delete the contents of cpp file. Type the following code into the cpp file of
the project created. Press “Build”>Build Solution to confirm if you typed it
correctly.

Step1-

● Writing a program:
1-Delete the program written in console app.

2- write this program:-

#include <iostream>
using namespace std;
int main()
{
cout << "Salaam Pakistan from the Programmer of CIT!\n";
return 0;
}

● Build:

Press “Build”>Build Solution to confirm if you typed it correctly. If you doesn't type
correctly then it will show you the errors and if you type correctly then it will show you no
issue found.

Step2-

● Making sense of program:

Explain each line of the program by using the single line comment “//”. Comments can
be used to explain the program line by line.

// CIT salam.cpp : This file contains the 'main' function. Program execution begins and ends
there.
//We use #include <iostream>" in C++ to integrate the IOstream library. It allows input and
output operations.
#include <iostream>
using namespace std;
// That does tell the compiler that the symbol namespace is defined in the std namespace.
int main()
//Function defines the entry or the starting point of the C/C++ program code.
//Program is written between "{}".
{
cout << "Salaam Pakistan from the Programmer of CIT!\n";
return 0;
}
//cout<< in C++ is used to display output to the screen.
//'\n' is used to end the line.
// ; it is used to end the statement.
//It is used to indicate the success of your program.

● Debugging and run the program:

Debug the program by “Debug”>”Start Debugging”. After debugging it shows the error
if you have it in your program. If there is no error in the program then it starts running.
● Notice the output:

Always notice the output showing on screen .

Output:
Salaam Pakistan from the Programmer of CIT!

Practical 2:-
Program to add two numbers and display sum.

a. Type the program:-

Write this program correctly in Visual Studio to confirm whether your program is correct
or not.

#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int sum;
a = 18;
b = 24
sum = a + b;
cout << "The sum of "<<a<<" and "<<b<<" is: \n"<<sum<<"\n";
return 0;
}

b. Build the program. Remove any errors if you get them.


Press “Build”>Build Solution to confirm if you typed it correctly. If you don't type correctly
then it will show you the errors and if you type correctly then it will show you no issue
found and this:

Build started at 5:25 PM...


1>------ Build started: Project: sum, Configuration: Debug x64 ------
1>sum.cpp
1>sum.vcxproj -> C:\Users\Student\source\repos\sum\x64\Debug\sum.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 5:25 PM and took 25.763 seconds ==========

c. Run the program. Observe its output. Understand what each line is
doing.

Run the program after build solution. Observe its output after understanding the
program.

Output:-
The sum of 18 and 24 is:
42

d. Write a description of each line, as you understand it, as comments


using // on the right side of each line.

Explaining each line of the program by using the single line comment “//”. Comments
can also be used to explain the program line by line.

// sum.cpp : This file contains the 'main' function. Program execution begins and ends
there.
#include <iostream> //It includes a library/header file for IO.
using namespace std;//Create a namespace having name std.
int main()//A method or function for program execution.
{ //Start the body of the main function.
int a;//Declaring variable a of type int i.e integer.
int b;//Declaring variable b of type int i.e integer.
int sum;//Declaring variable sum of type int i.e integer.
a = 18;//Assigning value 18 to a.
b = 24;//Assigning value 24 to b.
sum = a + b;// Adding a and b to compute sum.
cout << "The sum of " << a << " and " << b << " is: \n" << sum << "\n";
//Showing result or sum of a and b.
return 0;//return 0 to main function indicates success of execution.
}

e. Confirm if you wrote the comments correctly by building the program.


Remove if you receive any error.

Confirm this after adding comments your program is correct and your comments are
correct.you can confirm this by using build solution. If you have any error then remove
it to run your program correctly.

f. Change values of a and b, and run the modified program to confirm if it


still works.

Changing the value of a and b. Now the value of a=12 and b= 5.Confirm this your
program is still working after changing the values of a and b. If the program is working
then write its output after execution.

Output:-
The sum of 12 and 5 is:
17

Practical 3:-

Program to get sum and average of two numbers.

a. Type the program:-

Write this program correctly in Visual Studio to confirm whether your program is correct
or not.

#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int sum;
float average;
a = 10;
b = 13;
sum = a + b;
average = a;
average = average + b;
average = average / 2;
cout << "The sum is: \n"<<sum<<"\n";
cout << "The average is:\n";
cout << average <<"\n";
return 0;
}
b. Build the program. Remove any errors if you get them.

Press “Build”>Build Solution to confirm if you typed it correctly. If you don't type correctly
then it will show you the errors and if you type correctly then it will show you no issue
found and this:

Build started at 5:46 PM...


1>------ Build started: Project: average and sum, Configuration: Debug x64 ------
1>average and sum.cpp
1>C:\Users\Student\source\repos\average and sum\average and sum.cpp(13,15):
warning C4244: '=': conversion from 'int' to 'float', possible loss of data
1>average and sum.vcxproj -> C:\Users\Student\source\repos\average and
sum\x64\Debug\average and sum.exe
1>Done building project "average and sum.vcxproj".
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 5:46 PM and took 20.349 seconds ==========

c. Run the program. Observe its output. Understand what each line is
doing.

Run the program after build solution. Observe its output after understanding the
program.
Output:-
The sum is:
23
The average is:
11.5

d. Write a description of each line, as you understand it, as comments


using // on the right side of each line.

Explaining each line of the program by using the single line comment “//”. Comments
can also be used to explain the program line by line.

// average and sum.cpp : Program to get sum and average of two numbers.
#include <iostream> //It includes a library/header file for IO.
using namespace std;//Create a namespace having name std.
int main()//A method or function for program execution.
{ //Start the body of the main function.
int a;//Declaring variable a of type int i.e integer.
int b;//Declaring variable b of type int i.e integer.
int sum;//Declaring variable sum of type int i.e integer.
float average;//Declaring variable average of type float.
a = 10;//Assigning value 10 to a.
b = 13;//Assigning value 13 to b.
sum = a + b;// Adding a and b to compute sum
average = a;// Assigning a to average.
average = average + b;// Assigning average+b to average.
average = average / 2; //this is divide operator
cout << "The sum is: \n" << sum << "\n";
//Showing result or sum of a and b.
cout << "The average is:\n";
cout << average << "\n";
//Showing result or average of a and b.
return 0;//return 0 to main function indicates success of execution.
}

e. Confirm if you wrote the comments correctly by building the program.


Remove if you receive any error.
Confirm this after adding comments your program is correct and your comments are
correct.you can confirm this by using build solution. If you have any error then remove
it to run your program correctly.

f. Change values of a and b, such that the output of the program becomes
the following:

Changing the value of a and b. Now the value of a=29 and b= 29 .Confirm this your
program is still working after changing the values of a and b. If the program’s output is
this then your program is correct, if its output is not the same as written then your
program is wrong.

Output:-
The sum is:
58
The average is:
29

g. Change values of a and b, such that the output of the program becomes
the following:

Changing the value of a and b. Now the value of a=4 and b= 4 .Confirm this your
program is still working after changing the values of a and b. If the program’s output is
this then your program is correct, if its output is not the same as written then your
program is wrong.

Output:-
The sum is:
8
The average is:
4

Practical 4

a. Type and build the following program that calculates the result of Tipu.
Remove errors if any.
Writing the program to calculate the result of Tipu.

#include <iostream>
using namespace std;
int main()
{
int physics_marks = 70;
int maths_marks = 55;
int sum = physics_marks + maths_marks;
float average = physics_marks;
average = average + maths_marks;
average = average / 2;
cout << "**********************************\n";
cout << "Total marks are " <<sum<< "\n";
cout << "The average marks are " << average << "\n";
cout << "**********************************\n";
return 0;
}

Building the program to confirm this program is correct. If the program is correct then it
will show you this:

Build:-
Build started at 3:53 PM...
1>------ Build started: Project: Result of Tipu, Configuration: Debug x64 ------
1>Result of Tipu.cpp
1>C:\Users\Student\source\repos\Result of Tipu\Result of Tipu.cpp(7,19): warning
C4244: 'initializing': conversion from 'int' to 'float', possible loss of data
1>Result of Tipu.vcxproj -> C:\Users\Student\source\repos\Result of
Tipu\x64\Debug\Result of Tipu.exe
1>Done building project "Result of Tipu.vcxproj".
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 3:53 PM and took 08.885 seconds ==========

b. Without running the program, calculate what will be the output of this
program.

Calculating the output of a program without executing.


Output:-
**********************************
Total marks are 125
The average marks are 62.5
**********************************

c. Run the program and confirm if your calculation is correct.

After execution of the program we got the same output. So,the output is correct.

d. Tipu has scored 80 marks in Urdu. Modify the program to include marks
of Urdu.

Adding the marks of Urdu in the program and again calculating the sum and average.

#include <iostream>
using namespace std;
int main(){
int physics_marks = 70;
int maths_marks = 55;
int urdu_marks=80
int sum = physics_marks + maths_marks+urdu_marks ;
float average = physics_marks;
average = average + maths_marks;
average = average +urdu_marks;
average = average / 3;
cout << "**********************************\n”;
cout << “Total marks are "<<sum<<"\n";
cout << "The average marks are "<< average <<"\n";
cout << "**********************************\n”;
return 0;
}

Output:-
**********************************
Total marks are 205
The average marks are 68.3333
**********************************
e. Tipu studies at Central Model School Samanabad. Modify the program
such that a beautiful result card is printed on the screen. Hint: Add Tipu’s
full name and school name and city. You can also add borders.

Adding the name of Tipu,name of school of Tipu and name of city.After this editing the
program to get the beautiful result card as an output of the program.

#include <iostream>
using namespace std;
int main()
{
int physics_marks = 70;
int maths_marks = 55;
int urdu_marks = 80;
int sum;
float average;
sum = physics_marks + maths_marks + urdu_marks;
average = sum / 3;
cout << "**********************************\n";
cout << "* RESULT CARD *\n";
cout << "**********************************\n";
cout << "* Name: \t\tTipu*\n";
cout << "* School: \t\tCentral Model School *\n";
cout << "* City: \t\tSamanabad*\n";
cout << "**********************************\n";
cout << "* Physics: \t\t" << physics_marks << "*\n";
cout << "* Maths: \t\t" << maths_marks << "*\n";
cout << "* Urdu: \t\t" << urdu_marks << "*\n";
cout << "**********************************\n";
cout << "* Total Marks: \t\t" << sum << "*\n";
cout << "* Average Marks: \t" << average << "t5r4*\n";
cout << "**********************************\n";

return 0;
}

Output:-
**********************************
* RESULT CARD *
**********************************
* Name: Tipu*
* School: Central Model School*
* City: Samanabad*
**********************************
* Physics: 70*
* Maths: 55*
* Urdu: 80*
**********************************
* Total Marks: 205*
* Average Marks: 68*
**********************************

Practical 5:-

a.Type the following program. Compile and remove errors. Run the
program and write down what it is doing using comments.

Write this program in Visual studio/Dev C++ to compile and run the program. Adding the
comments to explain each line of the program.

#include <iostream> //It includes a library/header file for IO


using namespace std;//Create a namespace having name std.
int main()//A method or function for program execution.
{ //Start the body of the main function.
int islamiat_marks = 70; //Declaring variable a of type int and assigning the value
int pakstudies_marks = 55; //Declaring variable a of type int and assigning the value
int computer_marks = 70; //Declaring variable a of type int and assigning the value
int maths_marks = 55; //Declaring variable a of type int and assigning the value
int physics_marks = 70; //Declaring variable a of type int and assigning the value
int chemisty_marks = 55; //Declaring variable a of type int and assigning the value
int Total = 100 * 6; //Declaring variable a of type int and assigning the value
int marks = chemisty_marks+ physics_marks + maths_marks;
marks = marks + computer_marks + pakstudies_marks;
marks = marks + islamiat_marks;
// Adding all subjects marks to compute total marks gain in all subjects.
float percentage = (float) marks / Total*100.0;
// Convert marks to float for accurate division and multiply by 100 for percentage
cout << "**********************************\n”;
// Print a line for visual separation in output
cout << “Tipu has scored "<<percentage<<"% marks with division = ";
//Showing percentage of marks.
// Determine the division based on the percentage obtained
if ( percentage > 80 )// Check if percentage is greater than 80
{
cout << “1st”; // If true, print "1st" division
}
if ( percentage <= 80 && percentage > 65 )// Check if percentage is between 65 and 80
{
cout << “2nd”; If true, print "2nd" division
}
if ( percentage <= 65 && percentage > 33 )// Check if percentage is between 33 and 65
{
cout << “3rd”; // If true, print "3rd" division
}
if ( percentage <= 33 )// Check if percentage is 33 or below
{
cout << “FAIL”; // If true, print "FAIL"
}
return 0;
}

Press “Build”>Build Solution to confirm if you typed it correctly. If you don't type correctly
then it will show you the errors and if you type correctly then it will show you no issue
found.Run the program after build solution to get the output of the program.

Output:-
**********************************
Tipu has scored 62.5 % marks with division = 3rd

b. Change marks such that the division is changed:-

Change the marks for any subject to see how it affects the division. Changing the marks
of subjects:

islamiat_marks =90
pakstudies_marks = 85
computer_marks = 92
maths_marks = 100
physics_marks = 95
chemistry_marks = 75
Now the division is:1st

Again change the marks for any subject to see how it affects the division. Now the
marks of subjects:

islamiat_marks =39
pakstudies_marks = 33
computer_marks = 32
maths_marks = 31
physics_marks = 30
chemistry_marks = 25
Now the division is:FAIL

c. Put a breakpoint in line # 4. Start debugging. Use the step over option to
run the program step by step. Notice that the program does not display
anything until you run the first cout line in stepped over.
1-Set Breakpoint:
Click ‘Debug’ next click ‘Windows’ and click ‘Breakpoint’ to add
breakpoints in the program. Navigate to the line where you want to set the breakpoint.
Click in the left margin next to the line number, or place the cursor on the line and press
F9. A red dot appears, indicating the breakpoint is set.

2-Run the Program in Debug Mode:


Run your program in debug mode with F5 to pause
at the breakpoint. Press F10 to use Step Over, executing the current line and moving to
the next. This speeds up debugging by allowing you to navigate code without diving into
functions, helping you focus on the main logic and observe how each line affects the
program’s state.
3-Add Watches:
Add watches for all relevant variables like islamiat_marks,
pakstudies_marks, computer_marks, maths_marks, physics_marks, chemistry_marks,
Total, marks, and percentage.This allows you to see how each variable changes as the
program executes.

Before step over:-

After step over:-


Practical 6:-
a.In Gilgit Baltistan province, the board has approved a new scheme using
grades. Change the following program so that the scheme.

Editing the following program to add grades according to the new scheme of Gilgit
Baltistan board of Tipu.

#include <iostream>
using namespace std;
int main()
{
int islamiat_marks = 70;
int pakstudies_marks = 55;
int computer_marks =70;
int maths_marks = 55;
int physics_marks = 70;
int chemistry_marks = 55;
int Total = 100 * 6;
int marks = chemistry_marks + physics_marks + maths_marks + computer_marks +
pakstudies_marks + islamiat_marks;
float percentage = (float)marks / Total * 100.0;
cout << "**********************************\n";
cout << "Tipu has scored " << percentage << "% marks with grade = ";
if (percentage >= 90) {
cout << "A+";
}
if (percentage >= 80 && percentage < 90) {
cout << "A";
}
if (percentage >= 72 && percentage < 80) {
cout << "A-";
}
if (percentage >= 65 && percentage < 72) {
cout << "B+";
}
if (percentage >= 58 && percentage < 65) {
cout << "B";
}
if (percentage >= 51 && percentage < 58) {
cout << "B-";
}
if (percentage >= 43 && percentage < 51) {
cout << "C+";
}
if (percentage >= 38 && percentage < 43) {
cout << "C";
}
if (percentage >= 30 && percentage < 38) {
cout << "D";
}
if (percentage < 30) {
cout << "FAIL";
}
return 0;
}

Run the program to confirm your program is calculating the grade correctly.

Output:-
**********************************
Tipu has scored 66.6667% marks with grade = B

b. Using breakpoints, run the program again. Add watch to all variables
including Total and islamiat_marks etc. Notice how each value changes at
some point. Tell whether the program runs each line as you expected.

1-Set Breakpoint:
Click ‘Debug’ next click ‘Windows’ and click ‘Breakpoint’ to add
breakpoints in the program. Navigate to the line where you want to set the breakpoint.
Click in the left margin next to the line number, or place the cursor on the line and press
F9. A red dot appears, indicating the breakpoint is set.
2-Run the Program in Debug Mode:
Run your program in debug mode with F5 to pause
at the breakpoint. Press F10 to use Step Over, executing the current line and moving to
the next. This speeds up debugging by allowing you to navigate code without diving into
functions, helping you focus on the main logic and observe how each line affects the
program’s state.

3-Add Watches:
Add watches for all relevant variables like islamiat_marks,
pakstudies_marks, computer_marks, maths_marks, physics_marks, chemistry_marks,
Total, marks, and percentage.This allows you to see how each variable changes as the
program executes.
Before step over:-

After step over:-


c. Make 10 copies of the program file above. Modify marks of subjects in
each copy, such that each of the given grades in the table is shown by
exactly one program. Use step by step debugging to correct your logic if
needed.

1-Understanding the Structure:


The program calculates the total marks from several
subjects and computes the percentage based on those marks.The percentage is then
used to determine the grade through a series of conditional checks.

2-Changing Marks for Each Grade:


To demonstrate each grade, you can set the marks
for the subjects so that the total marks will yield the desired percentage for each
grade.For instance, to achieve a grade of A+, you will need to set the marks such that
the total marks divided by the total possible marks gives a percentage of 90 or above.

3-Changing marks for getting change divisions in output:

● Grade A+ (90 and above):

int islamiat_marks = 95;


int pakstudies_marks = 92;
int computer_marks = 90;
int maths_marks = 93;
int physics_marks = 91;
int chemistry_marks = 94;

● Grade A (80 to 89):

int islamiat_marks = 85;


int pakstudies_marks = 80;
int computer_marks = 88;
int maths_marks = 82;
int physics_marks = 84;
int chemistry_marks = 81;

● Grade A- (72 to 79):

int islamiat_marks = 75;


int pakstudies_marks = 73;
int computer_marks = 76;
int maths_marks = 74;
int physics_marks = 78;
int chemistry_marks = 77;

● Grade B+ (65 to 71):

int islamiat_marks = 68;


int pakstudies_marks = 66;
int computer_marks = 70;
int maths_marks = 67;
int physics_marks = 69;
int chemistry_marks = 65;

● Grade B (58 to 64):

int islamiat_marks = 60;


int pakstudies_marks = 62;
int computer_marks = 59;
int maths_marks = 63;
int physics_marks = 61;
int chemistry_marks = 58;
● Grade B- (51 to 57):

int islamiat_marks = 54;


int pakstudies_marks = 52;
int computer_marks = 53;
int maths_marks = 55;
int physics_marks = 56;
int chemistry_marks = 51;

● Grade C+ (43 to 50):

int islamiat_marks = 45;


int pakstudies_marks = 44;
int computer_marks = 46;
int maths_marks = 48;
int physics_marks = 50;
int chemistry_marks = 43;

● Grade C (38 to 42):

int islamiat_marks = 39;


int pakstudies_marks = 41;
int computer_marks = 40;
int maths_marks = 38;
int physics_marks = 42;
int chemistry_marks = 39;

● Grade D (30 to 37):

int islamiat_marks = 35;


int pakstudies_marks = 30;
int computer_marks = 31;
int maths_marks = 32;
int physics_marks = 34;
int chemistry_marks = 33;

● Grade FAIL (below 30):

int islamiat_marks = 25;


int pakstudies_marks = 20;
int computer_marks = 15;
int maths_marks = 28;
int physics_marks = 22;
int chemistry_marks = 27;

4-Debugging:
After adjusting the marks, compile and run the program.Set breakpoints to
check the values of total marks and the calculated percentage.Ensure the output
corresponds to the expected grade based on the marks you've set.

Practical 7:-
Comparison of Two Variables

a. Program Code

Here’s the complete C++ program that compares two integers:

#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout << "Enter integer A: ";
cin >> num1;
cout << "Enter integer B: ";
cin >> num2;
if (num1 > num2)
{
cout << "A is greater than B" << endl;
}
if (num1 < num2)
{
cout << "B is greater than A" << endl;
}
if (num1 == num2)
{
cout << "A and B are equal" << endl;
}
return 0;
}

b. Program Description

● #include <iostream>: This line includes the input-output stream library


necessary for using cout and cin.
● using namespace std;: This allows the use of standard library functions without
needing the std:: prefix.
● int main() : This defines the main entry point of the program.
● int num1, num2;: Two integer variables are declared to hold user input.
● cout: It is used to print the statement as an output.
● cin: It is used to assign the value to a variable at the time of execution.
● Checking the comparison between two variables through an if-else statement.
● Comparison Logic:The program compares the two integers and prints whether
A is greater than B, B is greater than A, or if they are equal
● return 0;: This indicates successful completion of the program.

c.Run the program and input numbers 457 and 90 to the program.
Write down the output.

a. Program Output for Input Values 457 and 90


When you run the program and input 457 for num1 and 90 for num2, the output will be:

b. Testing with Different Values


You can input different values for num1 and num2 to see how the output changes. Here
are some examples:

1. Input Set 1:

○ Values: A = 10, B = 20

2. Input Set 2:

○ Values: A = 45, B = 45

3. Input Set 3:

○ Values: A = 99, B = 50
Practical 8:-
Change the logic of your previous program in which you are calculating the
grades.

a. Change to the program that calculates student grades you created in one of
your previous labs. Change the logic as follows, the program should now take
input from the user for marks of different subjects and then display grade in a
nice format:
#include <iostream>
using namespace std;
int main()
{
int islamiat_marks;
int pakstudies_marks;
int computer_marks;
int maths_marks;
int physics_marks;
int chemistry_marks;
cout << "Enter marks for Islamiat: ";
cin >> islamiat_marks;
cout << "Enter marks for Pakistan Studies: ";
cin >> pakstudies_marks;
cout << "Enter marks for Computer: ";
cin >> computer_marks;
cout << "Enter marks for Maths: ";
cin >> maths_marks;
cout << "Enter marks for Physics: ";
cin >> physics_marks;
cout << "Enter marks for Chemistry: ";
cin >> chemistry_marks;
int Total = 100 * 6;
int marks = chemistry_marks + physics_marks + maths_marks + computer_marks +
pakstudies_marks + islamiat_marks;
float percentage = (float)marks / Total * 100.0;
cout << "**********************************\n";
cout << "Tipu has scored " << percentage << "% marks with grade = ";
if (percentage >= 90) {
cout << "A+";
}
if (percentage >= 80 && percentage < 90) {
cout << "A";
}
if (percentage >= 72 && percentage < 80) {
cout << "A-";
}
if (percentage >= 65 && percentage < 72) {
cout << "B+";
}
if (percentage >= 58 && percentage < 65) {
cout << "B";
}
if (percentage >= 51 && percentage < 58) {
cout << "B-";
}
if (percentage >= 43 && percentage < 51) {
cout << "C+";
}
if (percentage >= 38 && percentage < 43) {
cout << "C";
}
if (percentage >= 30 && percentage < 38) {
cout << "D";
}
if (percentage < 30) {
cout << "FAIL";
}
return 0;
}

b. Run the modified grading program several times with different values of marks and
observe that the grade is calculated correctly.

To test the modified grading program, you can run it multiple times with various values
for the marks of different subjects. Below are some sample inputs and their expected
outputs to help you observe how the grade is calculated correctly.

Running the program several times with different values

1.Test Case 1

● Input:
○ Islamiat: 95
○ Pakistan Studies: 88
○ Computer: 92
○ Maths: 90
○ Physics: 85
○ Chemistry: 94
2.Test Case 2

● Input:
○ Islamiat: 78
○ Pakistan Studies: 75
○ Computer: 80
○ Maths: 70
○ Physics: 72
○ Chemistry: 65

3.Test Case 3

● Input:
○ Islamiat: 60
○ Pakistan Studies: 50
○ Computer: 55
○ Maths: 58
○ Physics: 65
○ Chemistry: 62

4.Test Case 4
● Input:
○ Islamiat: 40
○ Pakistan Studies: 45
○ Computer: 35
○ Maths: 50
○ Physics: 42
○ Chemistry: 38

Test Case 5

● Input:
○ Islamiat: 25
○ Pakistan Studies: 20
○ Computer: 15
○ Maths: 30
○ Physics: 10
○ Chemistry: 5

c. Input Values for Each Grade:-


Inputting different values at the time of execution to get each grade.

1. Grade A+ (≥ 90%)

○ Marks:
■ Islamiat: 95
■ Pakistan Studies: 90
■ Computer: 92
■ Maths: 94
■ Physics: 91
■ Chemistry: 93
○ Total Marks Obtained: 555
○ Percentage: 92.50%

2. Grade A (80% - 89.99%)

○ Marks:
■ Islamiat: 85
■ Pakistan Studies: 80
■ Computer: 82
■ Maths: 84
■ Physics: 88
■ Chemistry: 81
○ Total Marks Obtained: 500
○ Percentage: 83.33%

3. Grade A- (72% - 79.99%)

○ Marks:
■ Islamiat: 75
■ Pakistan Studies: 73
■ Computer: 74
■ Maths: 76
■ Physics: 78
■ Chemistry: 77
○ Total Marks Obtained: 453
○ Percentage: 75.50%

4. Grade B+ (65% - 71.99%)

○ Marks:
■ Islamiat: 68
■ Pakistan Studies: 66
■ Computer: 70
■ Maths: 67
■ Physics: 69
■ Chemistry: 65
○ Total Marks Obtained: 405
○ Percentage: 67.50%

5. Grade B (58% - 64.99%)

○ Marks:
■ Islamiat: 60
■ Pakistan Studies: 59
■ Computer: 62
■ Maths: 63
■ Physics: 61
■ Chemistry: 58
○ Total Marks Obtained: 363
○ Percentage: 60.50%

6. Grade B- (51% - 57.99%)

○ Marks:
■ Islamiat: 55
■ Pakistan Studies: 53
■ Computer: 52
■ Maths: 54
■ Physics: 56
■ Chemistry: 57
○ Total Marks Obtained: 327
○ Percentage: 54.50%

7. Grade C+ (43% - 50.99%)

○ Marks:
■ Islamiat: 49
■ Pakistan Studies: 45
■ Computer: 47
■ Maths: 44
■ Physics: 46
■ Chemistry: 48
○ Total Marks Obtained: 279
○ Percentage: 46.50%

8. Grade C (38% - 42.99%)

○ Marks:
■ Islamiat: 40
■ Pakistan Studies: 39
■ Computer: 41
■ Maths: 38
■ Physics: 36
■ Chemistry: 37
○ Total Marks Obtained: 231
○ Percentage: 38.50%

9. Grade D (30% - 37.99%)

○ Marks:
■ Islamiat: 34
■ Pakistan Studies: 32
■ Computer: 35
■ Maths: 33
■ Physics: 31
■ Chemistry: 30
○ Total Marks Obtained: 195
○ Percentage: 32.50%

10. Grade FAIL (< 30%)

○ Marks:
■ Islamiat: 20
■ Pakistan Studies: 15
■ Computer: 10
■ Maths: 5
■ Physics: 0
■ Chemistry: 5
○ Total Marks Obtained: 55
○ Percentage: 9.17%

Practical 9:-

a.Type and build the following program.

Write the following program and build it to confirm the program is correct or not.
#include <iostream>
using namespace std;
int main()
{
int num, i; // Prompt user for input
cout << "Enter an integer: ";
cin >> num; // Print table header
cout << "Multiplication Table of " << num << endl;
cout << "-------------------------" << endl;
// Loop from 1 to 20 using while loop
i = 1;
while (i <= 20)
{
cout << num << " x " << i << " = " << num * i << endl;
i++; // Increment counter inside the loop
}
return 0;
}

There is no error in the program after building it.

a.Run the program with different inputs, understand it, and note down the results.
Especially note the effect of while. Use debugging breakpoints and watches to
confirm your understanding.

Step 1:-

Running the program with different values:


Test the program with various inputs. These are some sets:

1- Input: 5

Output:

Enter an integer: 5
Multiplication Table of 5
-------------------------
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
5 x 11 = 55
5 x 12 = 60
5 x 13 = 65
5 x 14 = 70
5 x 15 = 75
5 x 16 = 80
5 x 17 = 85
5 x 18 = 90
5 x 19 = 95
5 x 20 = 100
2- Input:7:-

Output:-

Enter an integer: 7
Multiplication Table of 7
-------------------------
7x1=7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
7 x 11 = 77
7 x 12 = 84
7 x 13 = 91
7 x 14 = 98
7 x 15 = 105
7 x 16 = 112
7 x 17 = 119
7 x 18 = 126
7 x 19 = 133
7 x 20 = 140

Step 2:-

1- Understanding the while Loop


● The while loop continues to execute as long as the condition i <= 20 is true.
● Each iteration prints a line of the multiplication table and increments i by 1.
● The loop stops once i becomes 21, ensuring that the table is printed from 1 to 20.

2-Using Debugging Breakpoints and Watches:-

a.Set a Breakpoint: Click next to the line with while (i <= 20) to set a breakpoint.

b.Run the Debugger: Start debugging the program. It will pause at the breakpoint.

c.Watch Variables: Monitor the values of num, i, and the output generated in each
iteration of the loop.

● Before the loop: i should start at 1.


● During each iteration: Observe how i increments and how the multiplication
result is calculated.

c.This program always prints the table up till 20. Modify the program such
that it asks for the table length e.g. 20, and then print the table till that
number. Edit the program for beautiful formatt of output

Step 1:-

1- Write the program and run it after debugging.

#include <iostream>
using namespace std;

int main() {
int num, length, i;
cout << "Enter an integer: ";
cin >> num;
cout << "Enter the table length: ";
cin >> length;
cout << "Multiplication Table of " << num << " up to " << length << endl;
cout << "------------------------------" << endl;
i = 1;
while (i <= length) {
cout << num << " x " << i << " = " << num * i << endl;
i++;
}
return 0;
}

You can run this program with various inputs. Here is the example:

Input: 5, Length: 10:-

Output:-
Enter an integer: 5
Enter the table length: 10
Multiplication Table of 5 up to 10
------------------------------
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

2- Editing the program for nice output:-


Adding this ”|” edit the program to get a nice output as a nice format.

#include <iostream>
using namespace std;
int main() {
int num, length, i;
cout << "------------------------------" << endl;
cout << "Enter an integer: ";
cin >> num;
cout << "------------------------------" << endl;
cout << "Enter the table length: ";
cin >> length;
cout << "------------------------------" << endl;
cout << "Multiplication Table of " << num << " up to " << length << endl;
cout << "------------------------------" << endl;
i = 1;
while (i <= length) {
cout <<"|" << num << " | x |" << i << " | = | " << num * i << "|" << endl;
i++;
}
return 0;
}

Practical 10
a. Type and build the following program
#include <iostream>
using namespace std;
int main() {
float tipuHeight, sultanHeight;
// Prompt user for input
cout << "Enter Tipu's height: ";
cin >> tipuHeight;
cout << "Enter Sultan's height: ";
cin >> sultanHeight;
// Compare heights
if (tipuHeight > sultanHeight) {
cout << "Tipu is taller than Sultan." << endl;
}
else if (tipuHeight < sultanHeight) {
cout << "Sultan is taller than Tipu." << endl;
}
else {
cout << "Tipu and Sultan have the same height." << endl;
}
return 0;
}

a. Notice use of else. Notice how it reduces the code and still achieves
the correct result.

b. Convert grading program such that it also uses else with every if.
Confirm if the output is still correct for different values.

#include <iostream>
using namespace std;

int main() {
int islamiat_marks = 70;
int pakstudies_marks = 55;
int computer_marks = 70;
int maths_marks = 55;
int physics_marks = 70;
int chemistry_marks = 55;

int Total = 100 * 6;


int marks = chemistry_marks + physics_marks + maths_marks +
computer_marks + pakstudies_marks + islamiat_marks;
float percentage = (float)marks / Total * 100.0;

cout << "**********************************\n";


cout << "Tipu has scored " << percentage << "% marks with grade = ";

if (percentage >= 90) {


cout << "A+";
} else if (percentage >= 80) {
cout << "A";
} else if (percentage >= 72) {
cout << "A-";
} else if (percentage >= 65) {
cout << "B+";
} else if (percentage >= 58) {
cout << "B";
} else if (percentage >= 51) {
cout << "B-";
} else if (percentage >= 43) {
cout << "C+";
} else if (percentage >= 38) {
cout << "C";
} else if (percentage >= 30) {
cout << "D";
} else {
cout << "FAIL";
}

return 0;
}

b. Revise the program you coded in previous part as follows, check if


the user inputs each subject’s marks correctly. Each mark should be
between 0 and 100, i.e. it cannot be negative or greater than 100. If the
input is incorrect, display “wrong input” message and exit the
program. Confirm if the working of your program is correct.

Input for Each Subject: The program prompts the user to enter marks for each of the
six subjects.
Validation Check: After each input, the program checks if the entered marks are
between 0 and 100. If any marks are out of this range, it prints "wrong input" and exits.

#include <iostream>
using namespace std;
int main()
{
int islamiat_marks, pakstudies_marks, computer_marks;
int maths_marks, physics_marks, chemistry_marks;
int marks, Total;
cout << "Enter marks for Islamiat: ";
cin >> islamiat_marks;
cout << "Enter marks for Pakistan Studies: ";
cin >> pakstudies_marks;
cout << "Enter marks for Computer: ";
cin >> computer_marks;
cout << "Enter marks for Maths: ";
cin >> maths_marks;
cout << "Enter marks for Physics: ";
cin >> physics_marks;
cout << "Enter marks for Chemistry: ";
cin >> chemistry_marks;
Total = 100 * 6;
marks = islamiat_marks + pakstudies_marks + computer_marks + maths_marks
+ physics_marks + chemistry_marks;
float percentage = (float)marks / Total * 100.0;
cout << "**********************************\n";
cout << "Check if the user inputs each subject marks correctly\n";
if ((islamiat_marks >= 0 && islamiat_marks <= 100) && (pakstudies_marks >=0
&& pakstudies_marks <= 100) && (computer_marks >= 0 && computer_marks <= 100)
&& (maths_marks >= 0 && maths_marks <= 100) && (physics_marks >= 0 &&
physics_marks <= 100) && (chemistry_marks >= 0 && chemistry_marks <= 100))
{
cout << "**********************************\n";
cout << "Tipu has scored " << percentage << "% marks with grade = ";

if (percentage >= 90) {


cout << "A+";
}
else if (percentage >= 80) {
cout << "A";
}
else if (percentage >= 72) {
cout << "A-";
}
else if (percentage >= 65) {
cout << "B+";
}
else if (percentage >= 58) {
cout << "B";
}
else if (percentage >= 51) {
cout << "B-";
}
else if (percentage >= 43) {
cout << "C+";
}
else if (percentage >= 38) {
cout << "C";
}
else if (percentage >= 30) {
cout << "D";
}
else {
cout << "FAIL";
}
}
else {
cout << "wrong input\n";
}
return 0;
}

● Run the program and enter valid marks (e.g., 85, 90, etc.) to see if it calculates
the percentage and grade correctly.
● Enter invalid marks (e.g., -10, 110, etc.) to ensure it correctly displays "wrong
input" and exits the program.

Outputs:-

Practical 11:-
a. Write a program that takes an integer as input from the user and prints
squares of all integers from 1 to the input. For example if the user enters 5,
the output will print squares of all integers from 1 to 5 i.e. “1, 4, 9, 16, 25”.
#include <iostream>
using namespace std;

int main() {
int n;
cout << "Enter an integer: ";
cin >> n;
for (int i = 1; i <= n; ++i) {
int square = i * i;
cout << square;
if (i < n)
{
cout << ", ";
}
}
cout << endl;
return 0;
}

b. Write a program that takes an integer as input and prints all the integers
from 0 to the input, for example if the input is 5 the output should be “0, 1,
2, 3, 4, 5”.

#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter an integer: ";
cin >> n;
// Print integers from 0 to n
for (int i = 0; i <= n; i++)
{
cout << i; // Print the integer
// Print a comma after each number except the last one
if (i < n)
{
cout << ", ";
}
}
cout << endl; // New line at the end
return 0;
}

c. Write a program that takes an integer as input and prints all the integers
from the input to 0, for example if the input is 5 the output should be “5, 4,
3, 2, 1, 0”.

#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter an integer: ";
cin >> n;
// Print integers from 0 to n
for (int i = n; i >= 0; i--)
{
cout << i; // Print the integer
// Print a comma after each number except the last one
if (i > 0)
{
cout << ", ";
}
}
cout << endl; // New line at the end
return 0;
}

d. Write a program that takes an integer as input and prints from 0 to the
number and then back to 0. For example if the input is 5 the output should
be “0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0”.

#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter an integer: ";
cin >> n;
for (int i = 0; i <= n; i++)
{
cout <<", "<< i;
}
for (int i = n; i >= 0; i--)
{
cout <<", "<< i;}
cout << endl;
return 0;
}

e. Write a program that takes an integer as input and prints all the integers
from 100 to the input. For example, if the user enters 45, the program prints
integers from 100 to 45. If the user enters 138, the program prints integers
from 100 to 138. Test your program by providing different inputs that are
less than and greater than 100. What happens to the program when you
provide input 100?

#include <iostream>
using namespace std;

int main() {
int n;
cout << "Enter an integer: ";
cin >> n;
// Print integers from 0 to n
for (int i = 0; i <= n; i++) {
if (i > 0) {
cout << ", "; // Print a comma before each number except the first
}
cout << i; // Print the integer
}
// Print integers from n-1 back down to 0
for (int i = n - 1; i >= 0; i--) {
cout << ", " << i; // Print the integer with a comma
}
cout << endl; // New line at the end
return 0;
}

You might also like