Programing practicals
Programing practicals
Programing practicals
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-
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.
#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-
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.
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:
Output:
Salaam Pakistan from the Programmer of CIT!
Practical 2:-
Program to add two numbers and display sum.
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;
}
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
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.
}
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.
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:-
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:
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
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.
}
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.
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.
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
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.
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:-
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
#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
c.Run the program and input numbers 457 and 90 to the program.
Write down the output.
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.
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
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%
○ Marks:
■ Islamiat: 85
■ Pakistan Studies: 80
■ Computer: 82
■ Maths: 84
■ Physics: 88
■ Chemistry: 81
○ Total Marks Obtained: 500
○ Percentage: 83.33%
○ Marks:
■ Islamiat: 75
■ Pakistan Studies: 73
■ Computer: 74
■ Maths: 76
■ Physics: 78
■ Chemistry: 77
○ Total Marks Obtained: 453
○ Percentage: 75.50%
○ Marks:
■ Islamiat: 68
■ Pakistan Studies: 66
■ Computer: 70
■ Maths: 67
■ Physics: 69
■ Chemistry: 65
○ Total Marks Obtained: 405
○ Percentage: 67.50%
○ Marks:
■ Islamiat: 60
■ Pakistan Studies: 59
■ Computer: 62
■ Maths: 63
■ Physics: 61
■ Chemistry: 58
○ Total Marks Obtained: 363
○ Percentage: 60.50%
○ Marks:
■ Islamiat: 55
■ Pakistan Studies: 53
■ Computer: 52
■ Maths: 54
■ Physics: 56
■ Chemistry: 57
○ Total Marks Obtained: 327
○ Percentage: 54.50%
○ Marks:
■ Islamiat: 49
■ Pakistan Studies: 45
■ Computer: 47
■ Maths: 44
■ Physics: 46
■ Chemistry: 48
○ Total Marks Obtained: 279
○ Percentage: 46.50%
○ Marks:
■ Islamiat: 40
■ Pakistan Studies: 39
■ Computer: 41
■ Maths: 38
■ Physics: 36
■ Chemistry: 37
○ Total Marks Obtained: 231
○ Percentage: 38.50%
○ Marks:
■ Islamiat: 34
■ Pakistan Studies: 32
■ Computer: 35
■ Maths: 33
■ Physics: 31
■ Chemistry: 30
○ Total Marks Obtained: 195
○ Percentage: 32.50%
○ Marks:
■ Islamiat: 20
■ Pakistan Studies: 15
■ Computer: 10
■ Maths: 5
■ Physics: 0
■ Chemistry: 5
○ Total Marks Obtained: 55
○ Percentage: 9.17%
Practical 9:-
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;
}
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:-
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:-
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.
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:-
#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:
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
#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;
return 0;
}
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 = ";
● 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;
}