Usama Hussnen
Usama Hussnen
Submitted To Dr Anwar Sb
Class Bs Cs (1st A)
Shift Morning
3. The main function body, which contains statements that perform the
program's tasks
#include<iostream>
int main()
cout<<"hello word"<<endl;
}
Lab no 2
Understanding of Datatypes, Variables and Constants
Datatype
Datatypes define the type of data that a variable can hold. They help
the computer understand how to interpret and manipulate the data.
Common datatypes include:
Integer (int): Used to store whole numbers, both positive and negative,
without a decimal point.
Floating-point (float): Used to store numbers with decimal points.
String (str): Used to store text or a sequence of characters.
Boolean (bool): Used to store true or false values.
Character (char): Used to store a single character, often in languages
like C and C++.
Array: A collection of values of the same datatype.
Object: A complex data type that can hold both data (attributes) and
functions (methods).
Different programming languages may have additional datatypes or
variations, but these are the most common ones.
Variable
Variables are used to store and manage data in a program. They serve
as named containers for values of various datatypes. When you declare
a variable, you specify its datatype, and the computer allocates
memory accordingly. You can then assign values to variables, update
them, and use them in calculations. Variable names are case-sensitive
in most programming languages and must adhere to naming rules (e.g.,
no spaces, start with a letter or underscore).
Constant:
Constants are values that do not change during the program's
execution. C++ provides two types of constants:
Literal Constants: These are constants that are directly written
into the code. For example:
Integer constant: 42
Floating-point constant: 3.14
Character constant: 'A'
String constant: "Hello, World"
long int (long): Usually 4 bytes (32 bits) or 8 bytes (64 bits) depending on the
system.
double pi = 3.141592;
Constants:
int main() {
Lab 03
Type of Operators
LAB TASK 3.1: Write a program in which all the
arithmetic operators are used.
#include<iostream>
int main()
int num1,num2;
cout<<"bsf23005133"<<endl;
cin>>num1;
cin>>num2;
double sum=num1+num2;
double difference=num1-num2;
double product=num1*num2;
double division=num1/num2;
cout<<num1<<"+"<<num2<<"="<<sum<<endl;
cout<<num1<<"-"<<num2<<"="<<difference<<endl;
cout<<num1<<"*"<<num2<<"="<<product<<endl;
cout<<num1<<"/"<<num2<<"="<<division<<endl;
#include <iostream>
using namespace std;
int main() {
cout << num1 << " == " << num2 << ": " << (num1 == num2) <<endl;
cout << num1 << " != " << num2 << ": " << (num1 != num2) <<endl;
cout << num1 << " < " << num2 << ": " << (num1 < num2) <<endl;
cout << num1 << " > " << num2 << ": " << (num1 > num2) <<endl;
cout << num1 << " <= " << num2 << ": " << (num1 <= num2) <<endl;
cout << num1 << " >= " << num2 << ": " << (num1 >= num2) <<endl;
return 0;
Lab 4
Pseudocode and flowchart
Pseudocode:
Flowchart:
A flowchart is a visual representation of an algorithm that uses
standardized symbols to represent different operations and decisions. It
provides a clear and visual way to understand the flow and structure of
a process or algorithm.
} else {
#include<iostream>
int main()
{double mark1,mark2,mark3,mark4;
cin>>mark1;
cin>>mark2;
cin>>mark4;
if(average>=50)
cout<<"final grade:"<<average<<"(passing)"<<endl;
else
cout<<"final grade:"<<average<<"failing"<<endl;
LAB TASK 4.2: Write pseudocode that takes input of three random
numbers and writes them all in ascending order.
#include <iostream>
using namespace std;
int main() {
cout<<"Usama Hasnain"<<endl
swap(num1, num2);
swap(num2, num3);
swap(num1, num2);
cout << "Numbers in ascending order: " << num1 << " " << num2 << " " << num3 <<
endl;
return 0;
}
LAB TASK 4.3: Write a pseudocode that calculate the age of citizens and
separate young and old citizens
#include <iostream>
int main() {
cout<<"Usama Hasnain"<<endl;
young = age;
} else {
old = age;
cout << "The young citizen is " << young << endl;
cout << "The old citizen is " << old << endl;
return 0;
}
Lab 5
If-else statements
LAB TASK 5.1: Write a program to calculate grades of students.
#include <iostream>
using namespace std;
int main() {
double numericalGrade;
cout<<"my name is Usama Hasnain "<<endl;
cout<<"my roll no is bsf23005133"<<endl;
cout << "Enter the numerical grade: ";
cin >> numericalGrade;
char letterGrade;
if (numericalGrade >= 90.0) {
letterGrade = 'A';
} else if (numericalGrade >= 80.0) {
letterGrade = 'B';
} else if (numericalGrade >= 70.0) {
letterGrade = 'C';
} else if (numericalGrade >= 60.0) {
letterGrade = 'D';
} else {
letterGrade = 'F';
}
cout << "Letter Grade: " << letterGrade << std::endl;
return 0;
}
LAB TASK 5.2: Write a program to check Even and odd Numbers.
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Usama Hasnain "<<endl;
cout<<"roll no is : bsf23005133"<<endl;
cout << "Enter a number: ";
cin >> n;
if (n % 2 == 0)
cout << "The number is even";
else
cout << "The number is odd";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
char ch;
cout<<"Usama Hasnain"<<endl;
cout << "Enter a character: ";
cin >> ch;
switch (ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
cout << "The character is a vowel";
break;
default:
cout << "The character is a consonant";
break;
}
return 0;
}
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 a, b, c;
cout<<"Usama Hasnain"<<endl;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
int max = a;
if (b > max)
max = b;
if (c > max)
max = c;
return 0;
}
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.
#include <iostream>
int main() {
int amount;
cout<<"usman sagheear"<<endl;
cout << "Enter the amount: ";
cin >> amount;
cout << "Number of 5000-rupee notes: " << fiveThousand << endl;
cout << "Number of 1000-rupee notes: " << thousand << endl;
cout << "Number of 100-rupee notes: " << hundred << endl;
cout << "Number of 50-rupee notes: " << fifty << endl;
cout << "Number of 20-rupee notes: " << twenty << endl;
cout << "Number of 10-rupee notes: " << ten << endl;
return 0;
}
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.
#include <iostream>
int main() {
int units;
cout<<"Usama Hasnain"<<endl;
int bill = 0;
bill = units * 5;
} else {
cout << "The total bill amount is Rs." << bill << endl;
return 0;
}
Lab no 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.
#include <iostream>
int main() {
char op;
cout<<"Usama Hasnain"<<endl;
int result;
switch(op) {
case '+':
result = num1 + num2;
break;
case '-':
break;
case '*':
result = num1*num2;;
break;
case '/':
if(num2 ==0)
else
result=num1/num2;
break;
default:
printf("\nResult is : %d",result);
return(0);
}
Lab no 7
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.
Press B to check account Balance
Press D to cash Deposit
Press W to cash Withdraw
#include <iostream>
int main() {
char choice;
do {
switch (choice) {
case 'B':
case 'b':
cout << "Your account balance is: $" << balance << endl;
break;
case 'D':
case 'd':
double depositAmount;
if (depositAmount < 0) {
cout << "Invalid deposit amount. Please enter a positive amount." << endl;
} else {
balance += depositAmount;
cout << "Deposit successful. Your new balance is: $" << balance << endl;
break;
case 'W':
case 'w':
double withdrawAmount;
cout << "Invalid withdrawal amount. Please enter a positive amount." << endl;
cout << "Insufficient funds. Your balance is: $" << balance << endl;
} else {
balance -= withdrawAmount;
cout << "Withdrawal successful. Your new balance is: $" << balance << endl;
break;
case 'X':
case 'x':
cout << "Exiting the ATM interface. Thank you for using our services!" << endl;
break;
default:
cout << "Invalid choice. Please enter a valid option." << endl;
break;
cout << "Do you want to perform another transaction? (y/n): ";
return 0;
}
Loops (while, do-while ,for)
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 number=1;
cout<<"Usama Hasnain"<<endl;
while(number<=10)
{
cout<<number<<""<<endl;
number++;
}
}
LAB TASK 8.2: Write the program of Guess the number game.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int secretNumber, guess, chances = 5;
srand(time(0));
secretNumber = rand() % 100 + 1;
cout<<"Usama Hasnain"<<endl;
cout << "Guess the number between 1 to 100" << endl;
while (chances > 0)
{
cin >> guess;
if (guess == secretNumber)
{
cout << "You guessed the number correctly! " << endl;
break;
}
else if (guess > secretNumber)
{
cout << "Your guess is too high! " << endl;
}
else
{
cout << "Your guess is too low! " << endl;
}
chances--;
}
if (chances == 0)
{
cout << "You ran out of chances! The secret number is " << secretNumber <<
endl;
}
return 0;
}
int n, i, fact = 1;
cout<<"Usama Hasnain"<<endl;
cin >> n;
fact = fact * i;
cout << "Factorial of " << n << " is: " << fact << endl;
return 0;
{
return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
}
int startDay = 1;
cout << "Sun Mon Tue Wed Thu Fri Sat" << endl;
int day = 1;
for (int i = 0; i < 6; ++i) {
for (int j = 0; j < 7; ++j) {
if ((i == 0 && j < startDay) || day > daysInMonth) {
cout << " ";
} else {
cout << setw(3) << day << " ";
++day;
}
}
cout << endl;
}
}
int main() {
int year, month;
cout<<"Usama Hasnain"<<endl;
displayCalendar(year, month);
return 0;
}
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 = "admin";
string password = "password";
string inputUsername;
string inputPassword;
cout<<"Usama Hasnain"<<endl;
cout << "Enter your username: ";
cin >> inputUsername;
cout << "Enter your password: ";
cin >> inputPassword;
int tries = 0;
tries++;
cout << "Incorrect username or password. Please try again." << endl;
return 0;
}
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.
#include <iostream>
int main() {
int n;
cout<<"Usama Hasnain"<<endl;
cin >> n;
int arr[n];
cout << "Enter the elements of the array: ";
int sum = 0;
cout << "The sum of the array elements is: " << sum << endl;
cout << "The average of the array elements is: " << avg << endl;
return 0;
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.
#include <iostream>
int main() {
int n;
cout<<"Usama Hasnain"<<endl;
cin >> n;
int arr[n];
max = arr[i];
min = arr[i];
cout << "The maximum element in the array is " << max << endl;
cout << "The minimum element in the array is " << min << endl;
return 0;
LAB TASK 9.3: 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() {
int n, m;
cout<<"Usama Hasnain"<<endl;
cin >> n;
cin >> m;
int arr[n][m];
return 0;
}
Lab 10 :Functions
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>
return a + b;
return a - b;
return a * b;
return a / b;
}
int main() {
int a, b;
cout<<"Usama Hasnain"<<endl;
cout << "The sum of " << a << " and " << b << " is " << add(a, b) << endl;
cout << "The difference of " << a << " and " << b << " is " << subtract(a, b) << endl;
cout << "The product of " << a << " and " << b << " is " << multiply(a, b) << endl;
cout << "The division of " << a << " and " << b << " is " << divide(a, b) << endl;
return 0;
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>
int main() {
int n;
cout<<"Usama Hasnain"<<endl;
cin >> n;
int arr[n];
InputArray(arr, n);
DisplayArray(arr, n);
return 0;
}
Lab no 11
Pointers
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;
ptr = #
cout<<"Usama Hasnain"<<endl;
cout << "Value of number: " << num << endl;
cout << "Address of number: " << &num << endl;
cout << "Value of pointer (address of number): " << ptr << endl;
cout << "Dereferenced pointer (value at the address): " << *ptr << endl;
}
LAB TASK 11.2. Write a program that swaps the values of two integers using
pointers.
#include <iostream>
int main()
int a = 10;
int b = 20
*p1 = *p2;
*p2 = temp;
cout<<"Usama Hasnain"<<endl;
return 0;