Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 56

UNIVERSITY OF EDUCATION LAHORE

(FAISALABAD CAMPUS)
_______________________________________
LAB REPORT
Submitted to: Dr. Muhammad Anwar
Submitted by: Muhammad Awais Mahmood
Course Title: Programming Fundamentals
Roll No: bsf 23005127
Course Code: COMP 1112
Program: BS Computer Science
Semester: 1st Morning B

Page |1
LAB CONTENTS
Lab Description
No
Lab. 1 Introduction to IDE
LAB TASK 1.1: Installation of Dev C++
LAB TASK 1.2: Write a C++ program to print your
name, roll number and class section.
Lab 2 Understanding of datatypes variables and constants
LAB TASK 2.1: Determine size of different data types
LAB TASK 2.2: Practice of variable and constants
Lab 3 Types of Operator
LAB TASK 3.1: Write a program in which all the
arithmetic operators are used.
LAB TASK 3.2: Write a program in which all the
comparison operators are used.
Lab 4 Pseudocode and flowchart
LAB TASK 4.1: Write a pseudocode to determine a
student’s final grade and indicate whether it is passing or
failing based on the 50% marks. The final grade is
calculated as the average of four marks.
LAB TASK 4.2: Write pseudocode that takes input of
three random numbers and writes them all in ascending
order.
LAB TASK 4.3: Write a pseudocode that calculate the
age of citizens and separate young and old citizens
Lab 5 If-else statements
LAB TASK 5.1: Write a program to calculate grades of
students.
LAB TASK 5.2: Write a program to check Even and odd
Numbers.
LAB TASK 5.3: Write a program to check vowels and

Page |2
consonants
LAB TASK 5.4: Write a program that take three numbers
as input from the user and finds maximum umber among
them.
LAB TASK 5.5: Write a program to count the number of
currency notes of 5000,1000,100,50,20, and 10 in any
given amount.
LAB TASK 5.6: Write a program to take consumed
electricity unit as an input and calculate total electricity
bill according to the following conditions:
If number of units are up to 100, the bill will be charged
at the rate of Rs. 5 per unit. If number of units are up to
200, the bill will be charged at the rate of Rs. 10 per unit.
If number of units are above 200, the bill will be charged
at the rate of Rs. 20 per unit.
Lab 6 Switch Statements
LAB TASK 6.1: Write a program to develop a simple
calculator. The program should take inputs of two
integers, select the operator (+, - ,*, /) using switch
statement and display the output after performing the
desired calculation.
Lab 7 Nested if-else statement
LAB TASK 7.1: Write a program to check username and
password.
LAB TASK 7.2: Write a program to design ATM
interface using nested if-else statements to perform the
following transactions. Initialize the balance with zero and
perform the cash deposit and withdrawal transactions
accordingly. After each transaction, the program should
ask the user to perform any other transaction or not. Based
on the user input (y/n) the program either redirects to the
main interface or exit.

Page |3
 Press B to check account Balance
 Press D to cash Deposit
 Press W to cash Withdraw
Lab 8 Loops (while, do-while ,for)
LAB TASK 8.1: Write a program that displays number
from 1 to 10 using while loop.
LAB TASK 8.2: Write the program of Guess the number
game.
LAB TASK 8.3: Write a program which asks user to enter
a positive integer and then calculate the factorial of that
number.
LAB TASK 8.4: Write a program which works like a year
calendar
LAB TASK 8.5: Write a program which takes username
and password three times if incorrect using loop.
Lab 9 Arrays
LAB TASK 9.1: Write a program which takes an array as
an input from the user and also calculate the sum and
average of all the array elements.
LAB TASK 9.2: Write a program that takes an array as an
input form the user and then finds the maximum and
minimum number of the array.
LAB TASK 9.3: Write a program which takes input from
the user in a 2D array and then display that array on
screen.
Lab Functions
10 LAB TASK 10.1: Write a program that inputs two
numbers from the user in the main function and passes
them to different functions to calculate addition,
subtraction, multiplication and division of these numbers.
LAB TASK 10.2: Write a program that takes array as an

Page |4
input using a function named InputArray () and display the
elements of the array using another function named
DisplayArray().

Lab Pointers
11 LAB TASK 11.1: Write a program to declare an integer
variable and a pointer to that integer. Assign a value to the
integer variable and make the pointer points to it. Print
both the value and the address of the integer variable using
the pointer.
LAB TASK 11.2. Write a program that swaps the values
of two integers using pointers.

Page |5
LAB 1
Introduction to IDE
An integrated development environment is a software application that provides
comprehensive facilities to computer programmers for software development. An IDE
normally consists of at least a source code editor, build automation tools and a debugger.
There are many C++ IDEs i.e. Visual Studio Code, Code:: Blocks, Eclipse, Dev C++ and many
more. But we use Dev C++.

Lab Task 1.1


Installation of IDE
Download the Eclipse installer. The easiest way to install eclipse IDE is to download and run the
installer

1: This is a 9 MB file approx.


2: Double click the executable file.
3: Start the installation by clicking Next button.
4: Choose the destination folder and install it.
5: Once the installation is complete, go to My Computer >
Properties > Advanced System Settings > Advanced Tab
6: Now click on "Environment variables" button > new.
7: Change the system variable name as: PATH.
8: Change the variable value as: C: \Dev-Cpp\bin;
9: Click OK and start Dev C++ to write the program.

Lab Task 1.2


Understanding C++ program structure and writing a first program

#include<iostream>
using namespace std;

Page |6
int main() {
cout<< "Hello World ";
}

Explantion:

1. We include the <iostream> header for input/output operations.


2. In the main function, using namespace std; is used to simplify the
code by allowing us to use cout and endl without the std:: prefix.
3. cout << "Hello, World!" << endl; prints "Hello, World!" followed
by a newline to the console.
4. return 0; indicates successful program execution with an exit
status of 0.

Lab 2:
Understanding of Datatypes, Variables and constants.

Data Types:
Data types define the kind of data that can be stored and
manipulated in a programming language. Common data types in C++
include:

int: Used to store integer values (whole numbers), e.g., 42, -17.
double: Used to store floating-point numbers (real numbers with
decimal points), e.g., 3.14, -0.5.

Page |7
char: Used to store single characters, e.g., 'A', '7', '$'.
bool: Used to store boolean values, which can be either true or
false.
string: Used to store sequences of characters, e.g., "Hello, World!".

Variables:
Variables are used to store and manipulate data in a
program. When you declare a variable, you are essentially reserving a
location in memory to store a value of a specific data type. In C++, you
declare a variable like this:

data_type variable_name;

Constants:
Constants are values that do not change during the
execution of a program. They are typically used for values that should
remain fixed, such as mathematical constants or configuration settings.
In C++, you can declare constants using the constant keyword:

const int MAX_SCORE = 100; // Declares a constant integer named


"MAX_SCORE" with a value of 100.

Lab Task 2.1


Determine size of different data types.
#include<iostream>
using namespace std;

Page |8
int main(){
cout<<"size of int is" <<sizeof(int)<<"bytes."<<endl;
cout<<"size of float is" <<sizeof(float)<<"bytes."<<endl;
cout<<"size of char is" <<sizeof(char)<<"bytes."<<endl;
cout<<"size of long is" <<sizeof(long)<<"bytes."<<endl;
cout<<"size of double" <<sizeof(double)<<"bytes."<<endl;
cout<<"size of short" <<sizeof(short)<<"bytes."<<endl;
cout<<"size of string" <<sizeof(string)<<"bytes."<<endl;
cout<<" BSF23005127.AWAIS MAHMOOD" <<endl;
}
Output:

Lab Task 2.2


Practice of Variables and constants.
#include<iostream>
using namespace std;
int main(){
const int a=15;
// a becomes integer constant variable
const float pi=3.14;
// pi becomes float constant variable
const char alphabet='A';
// A becomes character constant variable

Page |9
cout<<a<<endl<<pi<<endl<<alphabet<<endl;
cout<<"BSF23005127.AWAIS MAHHMOOD";
}
Output:

Lab 3
Types of Operators:
Arithmetic Operators:

These operators are used for mathematical calculations:

● + (Addition): Adds two operands.


● - (Subtraction): Subtracts the right operand from the left operand.
● * (Multiplication): Multiplies two operands.
● / (Division): Divides the left operand by the right operand.
● % (Modulus): Returns the remainder when the left operand is divided by the
right operand.

Comparison Operators:

P a g e | 10
These operators compare two values and return a Boolean result (true or false):

● == (Equal to): Checks if two operands are equal.


● != (Not equal to): Checks if two operands are not equal.
● < (Less than): Checks if the left operand is less than the right operand.
● > (Greater than): Checks if the left operand is greater than the right operand.
● <= (Less than or equal to): Checks if the left operand is less than or equal to
the right operand.
● >= (Greater than or equal to): Checks if the left operand is greater than or
equal to the right operand.

Assignment Operators:

These operators are used to assign values to variables:

● = (Assignment): Assigns the value of the right operand to the left operand.
● += (Add and Assign): Adds the right operand to the left operand and assigns
the result to the left operand (e.g., a += b is equivalent to a = a + b).

Logical Operators:

These operators are used to perform logical operations on Boolean values:

● && (Logical AND): Returns true if both operands are true.


● || (Logical OR): Returns true if at least one operand is true.
● ! (Logical NOT): Returns the opposite of the operand's Boolean value.

P a g e | 11
Lab Task 3.1 :
Write a program in which all the arithmetic operations are used.

#include<iostream>

using namespace std;

int main(){

int a=15;

int b=25;

cout<<"a+b="<<a+b<<endl;

cout<<"a-b="<<a-b<<endl;

cout<<"a*b="<<a*b<<endl;

cout<<"a/b="<<a/b<<endl;

cout<<"a%b="<<a%b<<endl;

cout<<"BSF23005127.AWAIS MAHHHMOOD";

P a g e | 12
Output:

Working:

This C++ program calculates and displays the sum, difference, product,
quotient and remainder of two integer variables ‘a’ and ‘b’. Which are
initialized with the values ‘15’ and ‘25’ respectively.

Lab Task 3.2:


Write a C++ program which should take input of two numbers and perform
all comparison operations.

#include<iostream>

using namespace std;

int main(){

P a g e | 13
int a=78;

int b=90;

bool result;

result=(a<b);

cout<<"a<b is "<<boolalpha<< result<<endl;

result=(a>b);

cout<<"a>b is "<<boolalpha<< result<<endl;

result=(a==b);

cout<<"a==b is "<<boolalpha<< result<<endl;

result=(a!=b);

cout<<"a!=b is "<<boolalpha<< result<<endl;

cout<<"BSF23005127.AWAIS MAHMOOD";

Output:

P a g e | 14
Working:

This program compares two integer variables ‘a’ and ‘b’ using relational
operators(<, >, == and !=) and it displays the result in boolean format with
appropriate messages in ‘true’ or ‘false’.

Lab. 4
Pseudocode
● Pseudocode is an informal high-level description of a procedure to perform a
specific task.

P a g e | 15
● It is used to sketch out the structure of the program before coding the actual
program.
● Pseudocode are written in natural language and combination of
programming languages.
● Pseudocode is intended for human reading rather than machine reading
● Pseudocode does not have a specific syntax like any of the programming
languages.

Flowchart
● Flowchart is a graphical representation of the sequence of operations in a
program.
● It shows the steps in the form of various kinds of boxes/symbols and their
order by connecting them with arrows.
● Shows logic of a program.
● Focused on individual steps and their interconnections.
● Presents flow control from one action to the next

Lab Task 4.1:


A pseudocode to determine a student’s final grade and indicate whether it is
passing or failing based on the 50% marks. The final grade is calculated as the
average of four marks.
Step 1 : Start
Step 2: Take 4 integer inputs for the different 4 subject like
math, english, physics and chemistry.
Step 3: Then calculate the grade based upon the average of
four marks:-
grade = ( math + english + physics + chemistry ) / 4
Step 4: if the value of the grade is more than 40, it will
print pass otherwise it shows fail.
Step 5: END

P a g e | 16
Lab Task 4.2:
Write a pseudocode that takes input of three random numbers and writes
them all in ascending order.

1.Start
2. Read three random numbers: num1, num2, num3
3. Set min_num = min(num1, num2, num3)
4.Set max_num = max(num1, num2, num3)
5.Set mid_num = num1 + num2 + num3 - min_num - max_num
6. Print "Numbers in ascending order:"
7. Print min_num
8. Print mid_num
9. Print max_num
10. End

Lab Task 4.3

Write a pseudocode that calculate the age of citizens and separate


young and old citizens.

1. Start.
2. Take birth_year from user.
3. Take current_year from user.
4. Then current_year-birth_year to get age.
5. If age is more than 50, it will print senior citizen otherwise it will print
normal citizen.
6. End.

P a g e | 17
Lab. 5
If-else Statements
An "if-else" statement allows you to execute specific blocks of code
depending on whether a certain condition (or set of conditions) is true or false.
It's like making decisions in your program based on the values of variables or the
outcome of logical expressions.

Lab Task 5.1:


Write a C++ program to calculate the grades of the students.

#include<iostream>

using namespace std;

int main(){

int x;

cout<<"Enter the marks:";

cin>>x;

if(x>90 && x<=100)

cout<<"Grade is A."<<endl;

P a g e | 18
}

else if(x>80 && x<=90)

cout<<"Grade is B."<<endl;

else if(x>70 && x<=80)

cout<<"Grade is C."<<endl;

else if(x>60 && x<=70)

cout<<"Grade is D."<<endl;

else if(x>50 && x<=60)

P a g e | 19
{

cout<<"Grade is E."<<endl;

else if(x>0 && x<50)

cout<<"Grade is F."<<endl;

else

cout<<"Invalid marks entered"<<endl;

cout<<"BSF23005127.Awais Mahmood";

P a g e | 20
Output:

Explanation
This program takes a numerical grade as input, calculates the corresponding
letter grade using if-else statements, and then displays the letter grade to the
user.
Lab Task 5.2
Write a program to check Even and Odd Numbers.

#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter any number:";
cin>>num;
if(num%2==0)
{
cout<<"Number is even."<<endl;
}

P a g e | 21
else
{
cout<<"Number is odd."<<endl;
}
cout<<"BSF23005127.AWAIS MAHMOOD";
}

Output:

Working:
This C++ program prompts the user to enter an number and then checks if it's a
‘Even’ or a ‘Odd’ number, displaying the result accordingly using ‘if-else’
statement.

Lab Task 5.3


Write a program to check vowels and constants.
#include<iostream>
using namespace std;
int main(){
char alphabet;
cout<<"\n \n \n \n \t \t \t Enter any alphabet:";

P a g e | 22
cin>>alphabet;
if(alphabet=='a'||alphabet=='A'||alphabet=='e'||
alphabet=='E'||alphabet=='i'||alphabet=='I'||alphabet=='o'||
alphabet=='O'||alphabet=='u'||alphabet=='U')
{
cout<<"\t \t \t Given alphabet is vowel. \n \n \n \n";
}
else
{
cout<<"\t \t \t Given alphabet is constant:"<<endl;
}
cout<<"Bst23005127.AWAIS MAHMOOD";
}

Output:

Working:
P a g e | 23
This C++ program prompts the user to enter an alphabet and then checks if it's a
‘vowel’ or a ‘consonant’, displaying the result accordingly using ‘if-else’
statement.

Lab Task 5.4


Write a program that take three numbers as input from the user and
finds maximum umber among them.

#include<iostream>
using namespace std;
int main(){
int num1, num2, num3;
cout<<"Enter any three numbers:";
cin>>num1>>num2>>num3;
if(num1>=num2 && num1>num3)
{
cout<<"Maximum number is "<<num1<<endl;
}
else if(num2>=num1 && num2>=num3)
{
cout<<"Maximum number is "<<num2<<endl;
}
else
{
cout <<"Maximum number is "<<num3<<endl;
}
cout<<"BSF23005127.AWAIS MAHMOOD";
}

P a g e | 24
Output:

Working:
This C++ program takes three numbers as input and determines the maximum
among them. After the user enters the three numbers, the program compares
them using if-else statements to find the largest number. Finally, it outputs the
maximum number.

Lab Task 5.5


Write a program to count the total number of currency notes of
5000,1000,100,50,20, and 10 in a given amount.

#include<iostream>
using namespace std;
int main(){
int amount,notes[7]={5000, 1000, 100, 50, 20, 10};
cout<<"BSF23005127.AWAIS MAHMOOD"<<endl;
cout<<"Enter the total amount: ";
cin>>amount;
for(int i=0;i<7;++i)
{

P a g e | 25
int count=amount/notes[i];
amount%=notes[i];
cout<<notes[i]<<"notes:"<<count<<endl;
}
}

Output:

Working:
This C++ program takes a user-input amount and calculates the number of 5000,
1000, 500, 100, 50, 20, and 10 notes needed to form that amount. It then
displays the count of each denomination.

Lab Task 5.6


Write a program in c++ to input electricity unit consumed and calculate total
electricity bill accoding to the given conditions:
 If number of units are up to 100, the bill will be charged at the rate of Rs.
5 per unit.

P a g e | 26
 If number of units are up to 200, the bill will be charged at the rate of Rs.
10 per unit.
 If number of units are above 200, the bill will be charged at the rate of
Rs. 20 per unit.

#include<iostream>

using namespace std;

int main(){

int unit_consumed;

double rate;

cout<<"Enter consumed units:";

cin>>unit_consumed;

if(unit_consumed<=100)

rate=5;

else if(unit_consumed<=200)

P a g e | 27
{

rate=10;

else

rate=20;

double bill=unit_consumed*rate;

cout<<"Your Electricity Bill is: Rs."<<bill<<endl;

cout<<"BSF23005127.AWAIS MAHMOOD";

Output:

P a g e | 28
Working:
This C++ program calculates the total electricity bill based on the number of units
consumed. It applies different rates for different ranges of units: Rs. 5 per unit for
up to 100 units, Rs. 10 per unit for up to 200 units, and Rs. 20 per unit for any
additional units above 200. The program then displays the total bill.

Lab 6
LAB TASK 6.1
Write a program to develop a simple calculator. The program should take
inputs of two integers, select the operator (+, - ,*, /) using switch statement and
display the output after performing the desired calculation .
#include<iostream>
using namespace std;
int main(){
double num1, num2;
char operation;
cout<<"\n \n \t \t \t \t
________________________"<<endl<<endl;
cout<<"\t \t \t \t WELCOME TO MY CALCULATOR "<<endl;

P a g e | 29
cout<<"\t \t \t \t ________________________"<<endl<<endl;
cout<<"\n \t \t \t \t Enter the value of number 1: ";
cin>>num1;
cout<<"\t \t \t \t Enter any operation(+,-,*,/): ";
cin>>operation;
cout<<"\t \t \t \t Enter the value of number 2: ";
cin>>num2;
double result;
switch(operation)
{
case'+':
cout<<"\t \t \t \t Result: "<<num1+num2<<endl;
break;
case'-':
cout<<"\t \t \t \t Result: "<<num1-num2<<endl;
break;
case'*':
cout<<"\t \t \t \t Result: "<<num1*num2<<endl;
break;
case'/':
cout<<"\t \t \t \t Result: "<<num1/num2<<endl;
break;
default:
cout<<"\t \t \t \t MATH ERROR"<<endl;
}
cout<<"BSF23005127.AWAIS MAHMOOD";
}

Output:
P a g e | 30
Working:
This C++ program functions as a simple calculator using ‘switch statement’,
where the user inputs two numbers and an arithmetic operation (+,-,*,/) , and
the program computes and displays the result.

Lab 7
Lab Task 7.1
Write a program to check username and password.
#include<iostream>
using namespace std;
int main()
{
string username;
string password;
cout<<"Enter your username:";
cin>>username;
cout<<"Password:";

P a g e | 31
cin>>password;
if(username=="waisy1005")
{
if(password=="awais786")
{
cout<<"Login Successful";
}
else
{
cout<<"Incorrect Password";
}
}
else
{
cout<<"Incorrect username and password";
}
}

Output:

Working:

P a g e | 32
The program asks for a username and password. If the username is "waisy1005"
and the password is "awais786", it prints "Login Successful". Otherwise, it
displays "Incorrect password" if the username is correct, and "incorrect
username and password" if the username and password are wrong.

Lab Task 7.2


Write a program to design ATM interface using nested if else.

#include<iostream>
using namespace std;
int main(){
char choice;
double amount;
double balance;
balance=0.0;
do
{
cout<<"To check the account balance press B:"<<endl;
cout<<"To deposit account balance press D:"<<endl;
cout<<"To withdraw account balance press W:"<<endl;
cin>>choice;
if(choice=='B'||choice=='b')
{
cout<<"Your account balance is "<<balance<<endl;
}
else if(choice=='D'||choice=='d')
{
cout<<"Enter your amount you want to
deposit:"<<endl;

P a g e | 33
cin>>amount;
if(amount<0)
{
cout<<"Invalid amount."<<endl;
}
else
{
balance+=amount;
cout<<"Deposit of "<<amount<<"
successful."<<endl;
cout<<"Your account balance is
"<<balance<<endl;
}
}
else if(choice=='W'||choice=='w')
{
cout<<"Enter amount you want to withdraw:"<<endl;
cin>>amount;
if(amount<=0)
{
cout<<"Invalid amount."<<endl;
}
else if(amount>balance)
{
cout<<"insufficient amount. your balance is
"<<balance<<endl;
}
else
{
balance-=amount;

P a g e | 34
cout<<"withdraw of "<<amount<<"
successful."<<endl;
cout<<"Your new account balance is
"<<balance<<endl;
}
}
else
{
cout<<"Invalid choice. Please try again."<<endl;
}
cout<<"If you want to perform another transaction?
(y/n):"<<endl;
cin>>choice;
cout<<endl;
}
while(choice=='Y'||choice=='y');
cout<<"Thank You for using our ATM. Good Bye!"<<endl;
cout<<"BSF23005127.AWAIS MAHMOOD";
}

Output:

P a g e | 35
Working:
This C++ program simulates an ATM transaction system. Users can check balance,
deposit, and withdraw money. It repeats transactions until the user opts to exit,
displaying appropriate messages based on user input.

Lab 8
Lab Task 8.1
Write a program that displays number from 1 to 10 using while loop.

#include<iostream>
using namespace std;
int main()
{
int x;

P a g e | 36
x=1;
while(x<=10)
{
cout<<x<<endl;
x++;
}
cout<<"End";
}

Output:

Working:
This C++ program uses a ‘while’ loop to print numbers from ‘1’ to ‘10’ and then
displays "End" at the end.

Lab Task 8.2


Write the program of Guess the number game.

#include<iostream>

P a g e | 37
using namespace std;
int main()
{
int x,y;
cout<<"Guess number:";
cin>>x;
for(y=47; x!=y; cin>>x)
{
if(x<y)
{
cout<<"Enter higher number:";
}
else
{
cout<<"Enter lower number:";
}
}
cout<<"Correct Number!"<<endl;
cout<<"BSF23005127.AWAIS MAHMOOD";
}

Output:

P a g e | 38
Working:
This C++ program lets the user guess a number. It prompts the user to input a
number and provides hints whether the guessed number is higher or lower than
the target number (47 in this case) until the correct number is guessed. Once the
correct number is guessed, it displays "Correct Number!" and terminates.

Lab Task 8.3


Write a program which asks user to enter a positive integer and then calculate
the factorial of that number.

#include<iostream>
using namespace std;
int main()
{
int a;
long long factorial=1;
cout<<"Enter a positive integer: ";
cin>>a;

P a g e | 39
if(a<0)
{
cout<<"Error! Factorial is not defined for negative
numbers.";
}
else
{
for(int i=1; i<=a; ++i)
{
factorial*=i;
}
cout<<"Factorial of "<<a<<" = "<<factorial<<endl;
}
cout<<"BSF23005127.AWAIS MAHMOOD";
}

Output:

Working:
This C++ program takes a positive integer input from the user and calculates its
factorial using a “for” loop. The factorial is the product of all positive integers

P a g e | 40
from 1 up to the input number. If the input is negative, it displays an error
message. Finally, it prints the calculated factorial.

Lab Task 8.4


Write a program which works like a year calendar.

#include<iostream>
using namespace std;
int main ()
{
int month,week,day;
cout<<"BSF23005127.AWAIS MAHMOOD"<<endl;
for (month=1; month<=12; month++)
{
cout<<"\n Month"<<month;
for (week=1; week<=4; week++)
{
cout<<"\n Week"<<week;
for (day=1; day<=7; day++)
{
cout<<" Day"<<day;
}
}
}
}

Output:

P a g e | 41
Working:
This C++ program uses ‘nested loops’ to display the days of the week for each
week in every month from 1 to 12.

LAB TASK 8.5:


Write a program which takes username and password three times if incorrect
using loop.

#include<iostream>
using namespace std;
int main()
{
string username,password;
int attempts= 3;
while (attempts>=0)

P a g e | 42
{
cout<<"Enter username: ";
cin>>username;
cout<<"Enter password: ";
cin>>password;
if (username=="waisy1005")
{
if(password=="awais786")
{
cout<<"Login successful.";
break;
}
else
{
cout<<"wrong password.";
}
}
else
{
cout<<"wrong username & password."<<endl;
attempts--;
}
}
cout<<"BSF223005127.AWAIS MAHMOOD";
}

P a g e | 43
Output:

Working:
This C++ program allows users three login attempts. If the correct username
("shahzaib") and password ("shahzaib786") are entered, it displays "Login
successful." Users have three chances to enter the correct credentials, after
which the program exits.

Lab 9
Lab Task 9.1
Write a program which takes an array as an input from the user and then
arrange that array in ascending and descending order.

#include<iostream>
using namespace std;
int main ()
{
int arr[10];
int min,max;
cout<<"Enter 10 values: ";

P a g e | 44
for (int i=0; i<10; i++)
{
cin>>arr[i];
}
min=arr[0];
max=arr[0];
for (int i=1; i<10; i++)
{
if (arr[i]>max)
{
max=arr[i];
}
if (arr[i]<min)
{
min=arr[i];
}
}
cout<<"Maximun value: "<<max<<endl;
cout<<"Minimum value: "<<min<<endl;
cout<<"BSF23005127.Awais Mahmood";
}

Output:

P a g e | 45
Working:
The program takes 10 integers as input and finds the minimum and maximum
values using a loop. It then prints the minimum and maximum values.

Lab Task 9.2


Write a program which takes an array as an input from the user and also
calculate the sum and average of all the array elements.

#include<iostream>
using namespace std;
int main ()
{
int arr[10];
int sum;
float avg;
cout<<"Enter 10 numbers:";
for (int i=0; i<10; i++)
{
cin>>arr[i];

P a g e | 46
sum=sum+arr[i];
}
cout<<"The sum is "<<sum<<".\n";
avg=sum/10;
cout<<"The avg is "<<avg<<"."<<endl;
cout<<"BSF23005127.AWAIS MAHHMOOD";
}

Output:

Working:
This C++ program takes 10 numbers as input, calculates their sum, and then
computes and displays their average using ‘Array’.

Lab Task 9.3

P a g e | 47
Write a program which takes input from the user in a 2D array and then display
that array on screen.
#include <iostream>
using namespace std;
int main()
{
cout<<"Awais Mahmood"<<endl;
cout<<"Enter values"<<endl;
int arr[2][4];
for( int i=0;i<2;i++)
{
for(int j=0;j<4;j++){
cin>>arr[i][j];
}
}
for( int i=0;i<2;i++)
{
for(int j=0;j<4;j++){
cout<<arr[i][j]<<" \t";
}
cout<<endl;
}
cout<<"Awais Mahmood"<<endl;
return 0;
}

Output:

P a g e | 48
Lab 10
Lab Task 10.1
Write a program that inputs two numbers from the user in the main function
and passes them to different functions to calculate addition, subtraction,
multiplication and division of these numbers.

#include<iostream>
using namespace std;
int add(int x,int y)
{
int sum=x+y;
return sum;
}
int sub(int x, int y)
{
int sub=x-y;

P a g e | 49
return x-y;
}
float divided(float x, float y)
{
if(y!=0)
float divided= x/y;
return x/y;
}
int mul(int x, int y)
{
int mul= x*y;
return x*y;
int main ()
{
int x,y;
cout<<"Awais Mahmood"<<endl;
cout<<"enter first number"<<endl;
cin>>x;
cout<<"enter second number"<<endl;
cin>>y;
cout<<"Sum of given number :"<<add(x,y)<<endl;
cout<<"Sub of given number :"<<sub(x,y)<<endl;
cout<<"Division of given number :"<<divided(x,y)<<endl;
cout<<"Multiplication of given number :"<<mul(x,y)<<endl;
cout<<"Awais Mahmood"<<endl;
}

Output:

P a g e | 50
Lab Task 10.2
Write a program that takes array as an input using a function named InputArray
() and display the elements of the array using another function named
DisplayArray().

#include <iostream>
using namespace std;
int ar[3][5];

void input()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
cin >> ar[i][j];
}
}
}

P a g e | 51
void output()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
cout << ar[i][j] << "\t ";
}
cout << endl;
}
}
int main()
{
cout<<"Enter 15 integer"<<endl;
cout<<"Awais Mahmood";
input();
output();
return 0;
}

Output:

P a g e | 52
Lab 11
Lab Task 11.1
Write a program to declare an integer variable and a pointer to that integer.
Assign a value to the integer variable and make the pointer points to it. Print
both the value and the address of the integer variable using the pointer.

#include <iostream>
using namespace std;
int main() {
int num = 10;
int *ptr; // Declare a pointer
ptr = &num; // Assign the address of 'number' to the
pointer

cout << "Value of number: " << num << endl;

P a g e | 53
cout << "Address of number: " << &num << endl;
cout << "Value of pointer (address of number): " << ptr
<< endl;
cout << "Dereferenced pointer (value at the address): "
<< *ptr << endl;
cout << "Awais Mahmood";
}

Output:

Lab Task 11.2


Write a program that swaps the values of two integers using
pointers.

#include <iostream>
using namespace std;
int main() {
// Declare two integers
int num1 = 5, num2 = 10;

cout << "Before swapping:" <<endl;

P a g e | 54
cout << "num1 = " << num1 <<endl;
cout << "num2 = " << num2 << endl;

// Declare pointers and swap values


int* ptr1 = &num1;
int* ptr2 = &num2;

// Swap values using pointers


int temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;

cout << "After swapping:" << endl;


cout << "num1 = " << num1 <<endl;
cout << "num2 = " << num2 <<endl;
cout << "Awais Mahmood";
}

Output:

P a g e | 55
P a g e | 56

You might also like