0% found this document useful (0 votes)
17 views39 pages

Usama Hussnen

The document is a lab report submitted by Usama Hasnain (BSF23005133) for a fundamental programming class. It contains summaries of 5 labs covering topics like IDE installation, data types, variables, operators, if/else statements, and pseudocode. The labs involve writing and running simple C++ programs to demonstrate understanding of basic programming concepts.

Uploaded by

itxgametimes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views39 pages

Usama Hussnen

The document is a lab report submitted by Usama Hasnain (BSF23005133) for a fundamental programming class. It contains summaries of 5 labs covering topics like IDE installation, data types, variables, operators, if/else statements, and pseudocode. The labs involve writing and running simple C++ programs to demonstrate understanding of basic programming concepts.

Uploaded by

itxgametimes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

LAB REPORT OF FUNDAMENTAL PROGRAMMING

Submitted by Usama Hasnain

Submitted To Dr Anwar Sb

Class Bs Cs (1st A)

Shift Morning

Student ID. Bsf23005133


Lab no 1:
Introduction to IDE

IDE (or Integrated Development Environment) is a software application


that provides comprehensive facilities to computer programmers for
software development. It typically includes a code editor, compiler or
interpreter, build automation tools, and debugging features. Some
popular IDEs include Microsoft Visual Studio, Eclipse, and Xcode. Do you
have any particular interests in programming languages

LAB TASK 1.1: Installation of Dev C++


Understanding the program structure of C++ and
writing the first program.
In C++, a program typically consists of several parts:

1. Preprocessor directives (such as #include)

2. Function declarations (like int main())

3. The main function body, which contains statements that perform the
program's tasks

#include<iostream>

using namespace std;

int main()

cout<<"Usama Hasnain "<<endl;

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"

LAB TASK 2.1: Determine size of different data types


short int (short): Usually 2 bytes (16 bits).

long int (long): Usually 4 bytes (32 bits) or 8 bytes (64 bits) depending on the
system.

long long int: Typically 8 bytes (64 bits).

char: Typically 1 byte (8 bits).

float: Typically 4 bytes (32 bits).

double: Usually 8 bytes (64 bits).

long double: Typically 10 bytes or more (80 bits or more).

LAB TASK 2.2: Practice of variable and constants

Variable Declaration and Initialization:

Declare variables of different data types (int, double, char, etc.).

Initialize variables using various methods (direct initialization, copy


initialization, and dynamic allocation).

int age = 25;

double pi = 3.141592;

char grade = 'A';

Constants:

Declare and use constants (const keyword and preprocessor macros).

const int MAX_VALUE = 100;


#define PI 3.141592

int main() {

const double TAX_RATE = 0.08;

Lab 03
Type of Operators
LAB TASK 3.1: Write a program in which all the
arithmetic operators are used.

#include<iostream>

using namespace std;

int main()

int num1,num2;

cout<<"Usama Hasnain "<<endl;

cout<<"bsf23005133"<<endl;

cout<<"enter the first no:"<<endl;

cin>>num1;

cout<<"enter the second no:"<<endl;

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;

LAB TASK 3.2: Write a program in which all the comparison


operators are used

#include <iostream>
using namespace std;

int main() {

double num1, num2;

cout<<"my name is Usama Hasnain "<<endl;

cout<<"my rol no is bsf23005133"<<endl;

cout << "Enter the first number: "<<endl;

cin >> num1;

cout << "Enter the second number: "<<endl;

cin >> num2;

cout << "Comparison Results:" <<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;

cout << num1 << " >= " << num2 << ": " << (num1 >= num2) <<endl;

return 0;

Lab 4
Pseudocode and flowchart
Pseudocode:

Pseudocode is a high-level description of an algorithm that uses natural


language and some programming-like constructs to outline the steps of
a process. It's not tied to any specific programming language, making it
a useful tool for initial algorithm design and communication between
team members.

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.

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.
float mark1, mark2, mark3, mark4, finalGrade;

input("Enter mark 1: ", mark1);

input("Enter mark 2: ", mark2);

input("Enter mark 3: ", mark3);

input("Enter mark 4: ", mark4);

finalGrade = (mark1 + mark2 + mark3 + mark4) / 4.0;

if the final grade is greater than or equal to 50%.

if (finalGrade >= 50) {

print("Final grade is passing.");

} else {

print("Final grade is failing.");

print("Final Grade: ", finalGrade);

Write a programme 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..

#include<iostream>

using namespace std;

int main()

{double mark1,mark2,mark3,mark4;

cout<<"my name is Usama Hasnain"<<endl;

cout<<"my roll no is bsf23005133"<<endl;

cout<<"enter the first mark";

cin>>mark1;

cout<<"enter the second mark";

cin>>mark2;

cout<<"enter the third mark";


cin>>mark3;

cout<<"enter the forth mark";

cin>>mark4;

double average =(mark1+mark2+mark3+mark4)/4.0;

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() {

int num1, num2, num3;

cout<<"Usama Hasnain"<<endl

cout << "Enter the first random number: ";

cin >> num1;

cout << "Enter the second random number: ";

cin >> num2;

cout << "Enter the third random number: ";

cin >> num3;

if (num1 > num2) {

swap(num1, num2);

if (num2 > num3) {

swap(num2, num3);

if (num1 > num2) {

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>

using namespace std;

int main() {

int age, young, old;

cout<<"Usama Hasnain"<<endl;

cout << "Enter the age of the citizen: ";

cin >> age;

if (age < 18) {

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;
}

LAB TASK 5.3: Write a program to check vowels and consonants

#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;

cout << "The maximum number is " << max;

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>

using namespace std;

int main() {

int amount;
cout<<"usman sagheear"<<endl;
cout << "Enter the amount: ";
cin >> amount;

int fiveThousand = amount / 5000;


amount = amount % 5000;
int thousand = amount / 1000;
amount = amount % 1000;
int hundred = amount / 100;
amount = amount % 100;
int fifty = amount / 50;
amount = amount % 50;
int twenty = amount / 20;
amount = amount % 20;
int ten = amount / 10;

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>

using namespace std;

int main() {

int units;

cout<<"Usama Hasnain"<<endl;

cout << "Enter the number of units consumed: ";

cin >> units;

int bill = 0;

if (units <= 100) {

bill = units * 5;

} else if (units <= 200) {

bill = units * 10;

} else {

bill = units * 20;

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>

using namespace std;

int main() {

int num1, num2;

char op;

cout<<"Usama Hasnain"<<endl;

cout << "Enter first integer: "<<endl;

cin >> num1;

cout << "Enter second integer: "<<endl;

cin >> num2;

cout << "Choose an operation (+, -, *, /): ";

cin >> op;

int result;

switch(op) {

case '+':
result = num1 + num2;

break;

case '-':

result = num1 -num2;

break;

case '*':

result = num1*num2;;

break;

case '/':

if(num2 ==0)

printf("Cannot divide by zero");

else

result=num1/num2;

break;

default:

printf("Invalid Operator Entered.");

printf("\nResult is : %d",result);

return(0);

}
Lab no 7

Nested if-else statement

LAB TASK 7.1: Write a program to check username and password.


#include<iostream>
#include<string>
using namespace std;
int main()
{
string excepted_username="myusername";
string excepted_password="mypassword";
string entered_username;
cout<<"Usama Hasnain"<<endl;
cout<<"entered_username" <<endl;
cin>>entered_username;
string entered_password;
cout<<"enter the password"<<endl;
cin>>entered_password;
if(entered_username==excepted_username&&entered_password==exc
epted_password)
{
cout<<"login is succesfull"<<endl;
}
else
{
cout<<"invalid password"<<"try again"<<endl;
}
}

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>

using namespace std;

int main() {

double balance = 0.0;

char choice;

cout<<"my name is Usama Hasnain"<<endl;

cout<<"my roll no is bsf23005133"<<endl;

do {

cout << "Welcome to the ATM interface!" << endl;

cout << "Press B to check account Balance" << endl;

cout << "Press D to cash Deposit" << endl;


cout << "Press W to cash Withdraw" << endl;

cout << "Press X to Exit" << endl;

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 'B':

case 'b':

cout << "Your account balance is: $" << balance << endl;

break;

case 'D':

case 'd':

double depositAmount;

cout << "Enter the amount to deposit: $";

cin >> 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 << "Enter the amount to withdraw: $";

cin >> withdrawAmount;


if (withdrawAmount < 0) {

cout << "Invalid withdrawal amount. Please enter a positive amount." << endl;

} else if (withdrawAmount > balance) {

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;

if (choice != 'X' && choice != 'x') {

cout << "Do you want to perform another transaction? (y/n): ";

cin >> choice;

} while (choice == 'Y' || choice == 'y');

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;
}

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 n, i, fact = 1;

cout<<"Usama Hasnain"<<endl;

cout << "Enter a positive integer: ";

cin >> n;

for (i = 1; i <= n; i++)

fact = fact * i;

cout << "Factorial of " << n << " is: " << fact << endl;

return 0;

LAB TASK 8.4: Write a program which works like a year


calendar
#include <iostream>
#include <iomanip>
using namespace std;
bool isLeapYear(int year)

{
return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
}

int getDaysInMonth(int year, int month) {


if (month == 2) {
return isLeapYear(year) ? 29 : 28;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
} else {
return 31;
}
}

void displayCalendar(int year, int month) {


cout << "Calendar for " << setfill('0') << setw(4) << year << "-" << setw(2) << month
<< ":" << endl;

int daysInMonth = getDaysInMonth(year, month);

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;

cout << "Enter the year: ";


cin >> year;

cout << "Enter the month (1-12): ";


cin >> month;

if (month < 1 || month > 12) {


cout << "Invalid month. Please enter a month between 1 and 12." << endl;
return 1;
}

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;

while (inputUsername != username || inputPassword != password)


{

tries++;
cout << "Incorrect username or password. Please try again." << endl;

cout << "Enter your username: ";


cin >> inputUsername;
cout << "Enter your password: ";
cin >> inputPassword;
}

if (inputUsername == username && inputPassword == password)


{
cout << "Login successful!" << 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>

using namespace std;

int main() {

int n;

cout<<"Usama Hasnain"<<endl;

cout << "Enter the size of the array: ";

cin >> n;

int arr[n];
cout << "Enter the elements of the array: ";

for (int i = 0; i < n; i++) {

cin >> arr[i];

int sum = 0;

for (int i = 0; i < n; i++) {

sum = sum + arr[i];

cout << "The sum of the array elements is: " << sum << endl;

double avg = sum / n;

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>

using namespace std;

int main() {

int n;

cout<<"Usama Hasnain"<<endl;

cout << "Enter the size of the array: ";

cin >> n;
int arr[n];

cout << "Enter the elements of the array: ";

for (int i = 0; i < n; i++) {

cin >> arr[i];

int max = arr[0];

int min = arr[0];

for (int i = 0; i < n; i++) {

if (arr[i] > max) {

max = arr[i];

if (arr[i] < min) {

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;

cout << "Enter the number of rows: "<<endl;

cin >> n;

cout << "Enter the number of columns: "<<endl;

cin >> m;

int arr[n][m];

cout << "Enter the elements of the array: "<<endl;

for (int i = 0; i < n; i++) {

for (int j = 0; j < m; j++) {

cin >> arr[i][j];

cout << "The array is: " << endl;

for (int i = 0; i < n; i++) {

for (int j = 0; j < m; j++) {

cout << arr[i][j] << " ";

cout << endl;

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>

using namespace std;

int add(int a, int b) {

return a + b;

int subtract(int a, int b) {

return a - b;

int multiply(int a, int b) {

return a * b;

float divide(int a, int b) {

return a / b;

}
int main() {

int a, b;

cout<<"Usama Hasnain"<<endl;

cout << "Enter two numbers: "<<endl;

cin >> a >> b;

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>

using namespace std;


void InputArray(int arr[], int n) {

for (int i = 0; i < n; i++) {

cout << "Enter element " << i + 1 << ": ";

cin >> arr[i];

void DisplayArray(int arr[], int n) {

for (int i = 0; i < n; i++) {

cout << arr[i] << " ";

cout << endl;

int main() {

int n;

cout<<"Usama Hasnain"<<endl;

cout << "Enter the size of the array: "<<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 = &num;
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>

using namespace std;

int main()

int a = 10;

int b = 20

int *p1 = &a;

int *p2 = &b;

int temp = *p1;

*p1 = *p2;
*p2 = temp;

cout<<"Usama Hasnain"<<endl;

cout << "a = " << a << endl;

cout << "b = " << b << endl;

return 0;

You might also like