0% found this document useful (0 votes)
44 views49 pages

PF Lab Manual 2023

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

PF Lab Manual 2023

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

PROGRAMMING FUNDAMENTALS

Lab 01
Flowcharts And Algorithms
Flowchart 01
Problem: How the greatest of the three given numbers can be obtained?

START

INPUT A, B, C

IS Yes IS Yes
A>B A>C GA
? ?
No No
IS GC
B> C No

?
Yes
GB

PRINTG

STOP

Algorithm:
Step 1. INPUT TO A, B, C
(Accept three numbers for A, B, and C)
Step 2. IF A > B
THEN IF A > C
THEN G  A
(G holds the desired number) ELSE
G  C END-
IF
ELSE
IF B > C
THEN G  B ELSE
GC

M.ABDULLAH REHMAN 1
PROGRAMMING FUNDAMENTALS

END-IF

END-IF
Step 3. PRINT “THE GREATEST OF THE GIVEN NUMBERS IS”, G
Step 4. STOP
Flowchart 02
Problem: Determine whether a given number is even or odd.

Algorithm:
Step 1. INPUT TO A
Step 2. COMPUTE R
Remainder of (A/2)
Step 3. IF R = 0
THEN PRINT “It is an even number.”
ELSE
PRINT “It is an odd number.”
END-IF
Step 4. STOP

M.ABDULLAH REHMAN 2
PROGRAMMING FUNDAMENTALS

Flowchart 03
Problem: Arrange the numbers X, Y, Z in descending order

Algorithm:

Step 1: Input the value for variables X, Y, Z.

Step 2: Read the integers X, Y, Z.

M.ABDULLAH REHMAN 3
PROGRAMMING FUNDAMENTALS

Step 3: Check if x>y, x>z, y>z

Step 4: Print the possible order.

Flowchart 04

M.ABDULLAH REHMAN 4
PROGRAMMING FUNDAMENTALS

Lab 02
Flowchart 05
Problem: Mama had just filled the cookie jar when the 3 children went to bed. That
night one child woke up, ate half of the remaining cookies, and went back to bed. Still
later, the third child woke up, ate half of the remaining cookies, leaving 3 cookies in the
jar. How many cookies were in the jar to begin with?

Start

Initial Number of
Cookies=X
Total Cookies=X

Cookies After First Child End


Y=X-(X/2)

No

Cookies After
Print Total
Second Child If Z=3 Cookies=X
Algorithm:
Z=Y-(Y/2) Yes

Step-1: Set a variable to represent the number of cookies


M.ABDULLAH REHMAN 5
PROGRAMMING FUNDAMENTALS

Step-2: Subtract the number of cookies eaten by the first child. Set jar to half of its value.
Step-3: Subtract the number of cookies eaten by the second child. Set jar to half of its value
Step-4: If jar is equal to 3, output the value of jar as the number of cookies
Step-5: If jar is not equal to 3 then go to step 3.
Step-6: End
Pseudocode:
Display “Enter the number of children: “
Read <number of children>
Display “Enter the number of cookies remaining: “
Read <cookies remaining>
<original cookies> = <cookies remaining>
While (<number of children> > 0)
<original cookies> = <original cookies> X 2
<number of children> = <number of children> - 1
End While
Display “Original number of cookies = “, <original cookies>

Flowchart 06
Problem: Brian bought a belt for $9 and a shirt that cost 4 times as much as the belt. He then
had $10. How much money did Brian have before he bought the belt and shirt?

M.ABDULLAH REHMAN 6
PROGRAMMING FUNDAMENTALS

Start

Belt=$9
Shirt=(4 x $9=$36)

(sum-$9-$36)

End
Assign result to ‘sum’

Previous sum - $10 Print “Sum”

Algorithm:
Step-1: Start
Step-2: Set the variables belt cost = 9, shirt cost = 4*belt cost remaining money = 10.
Step-3: Calculate the total cost of the shirt by multiplying the belt cost by 4 and assign it to the
shirt cost.
Step-4: Subtract the total cost of the belt and the shirt from the remaining money and assign it
to remaining money.
Step-5: Display the value of remaining money as the result.

M.ABDULLAH REHMAN 7
PROGRAMMING FUNDAMENTALS

Step-6: End
Pseudocode:
Display “Enter the price of the first item: “
Read <item 1 price>
Display “Enter the multiplier: “
Read <multiplier>
Display “Enter the amount left after shopping: “
Read <amount left>
<item2 price> = <multiplier> X <item1 price>
<start amount> = <item1 price> + <item2 price> +
<amount left>
Display “The starting amount was “, <start amount>

M.ABDULLAH REHMAN 8
PROGRAMMING FUNDAMENTALS

Lab 03
Task 01: Write A list of all ASCII codes from 0 to 127.

0: NUL (Null) 1: SOH (Start of Heading)

2: STX (Start of Text) 3: ETX (End of Text)

4: EOT (End of Transmission) 5: ENQ (Enquiry)

6: ACK (Acknowledgment) 7: BEL (Bell)

8: BS (Backspace) 9: TAB (Horizontal Tab)

10: LF (Line Feed) 11: VT (Vertical Tab)

12: FF (Form Feed) 13: CR (Carriage Return)

14: SO (Shift Out) 15: SI (Shift In)

16: DLE (Data Link Escape) 17: DC1 (Device Control 1)

18: DC2 (Device Control 2) 19: DC3 (Device Control 3)

20: DC4 (Device Control 4) 21: NAK (Negative Acknowledgment)

22: SYN (Synchronous Idle) 23: ETB (End of Transmission Block)

24: CAN (Cancel) 25: EM (End of Medium)

26: SUB (Substitute) 27: ESC (Escape)

28: FS (File Separator) 29: GS (Group Separator)

30: RS (Record Separator) 31: US (Unit Separator)

32: Space 33: !

34: " 35: #

M.ABDULLAH REHMAN 9
PROGRAMMING FUNDAMENTALS

36: $ 37: %

38: & 39: '

40: ( 41: )

42: * 43: +

44: , 45: -

46: . 47: /

48: 0 49: 1

50: 2 51: 3

52: 4 53: 5

54: 6 55: 7

56: 8 57: 9

58: : 59: ;

60: < 61: =

62: > 63: ?

64: @ 65: A

66: B 67: C

68: D 69: E

70: F 71: G

72: H 73: I

74: J 75: K

M.ABDULLAH REHMAN 10
PROGRAMMING FUNDAMENTALS

76: L 77: M

78: N 79: O

80: P 81: Q

82: R 83: S

84: T 85: U

86: V 87: W

88: X 89: Y

90: Z 91: [

92: \ 93: ]

94: ^ 95: _

96: ` 97: a

98: b 99: c

100: d 101: e

102: f 103: g

104: h 105: i

106: j 107: k

108: l 109: m

110: n 111: o

112: p 113: q

114: r 115: s

M.ABDULLAH REHMAN 11
PROGRAMMING FUNDAMENTALS

116: t 117: u

118: v 119: w

120: x 121: y

122: z 123: {

124: | 125: }

126: ~ 127: DEL (Delete)

Task 02: Write a program that calculates the average exam grade for a class of 10 students.
Step 1: Open Dev C++
Step 2: Go to file and create a new source file
Step 3: Then write the codes of program

M.ABDULLAH REHMAN 12
PROGRAMMING FUNDAMENTALS

Step 4: After write the code compile the file and save the file with any name
Step 5: Then run the program
Step 6: Now input all the values one by one

Result:

M.ABDULLAH REHMAN 13
PROGRAMMING FUNDAMENTALS

Lab 04
Task 01: Create a program to find the class average and take the input from user.

#include <iostream>
using namespace std;
int main() {
int numStudents, counter, grade, total, average;
total = 0;
counter = 1;

cout<<"Enter the number of students: ";


cin>> numStudents;

while (counter <= numStudents) {


cout << "Enter a grade: ";
cin >> grade;
total = total + grade;
counter = counter + 1;
}

average = total / numStudents;


cout << "Class average is: " << average << endl;

return 0;
}

M.ABDULLAH REHMAN 14
PROGRAMMING FUNDAMENTALS

Output:

Task 02: Create a program to find the area in floating value.

#include <iostream>
using namespace std;
#define PI 3.14159
int main()
{
float radius = 3.0;
float area;
area = PI * radius * radius;
cout << "The area is " << area << "." << endl;
return 0;
}

M.ABDULLAH REHMAN 15
PROGRAMMING FUNDAMENTALS

Output:

Task 03: Create a program to find the inches and feet from fathoms.
#include <iostream>
using namespace std;
int main() {
float inches, feet, fathoms;
cout << "Enter the depth in fathoms: ";
cin >> fathoms;
feet = 6 * fathoms;
inches = 12 * feet;
cout << "Its depth at sea: " << std::endl;
cout << fathoms << " fathoms" << std::endl;
cout << feet << " feet" << std::endl;
cout << inches << " inches" << std::endl;
return 0;
}

M.ABDULLAH REHMAN 16
PROGRAMMING FUNDAMENTALS

Output:

Task 04 :Create a program to find a value defined by the user and check the value is
‘even’ or ‘odd’ using if_else statement

#include <iostream>
using namespace std;
int main ()
{
int a;
cout <<"Enter a number:\n";
cin>>a;
a=a%2;
if(a==0)
cout<<"Number is even";
else
cout<<"Number is odd";
return 0;
}

M.ABDULLAH REHMAN 17
PROGRAMMING FUNDAMENTALS

Output:

M.ABDULLAH REHMAN 18
PROGRAMMING FUNDAMENTALS

Lab 05
Task 01: Write a program that calculates the average exam grade for a class of 10 students.
Exam grade defined by the user using while loop.

#include <iostream>
using namespace std;
int main() {
int numStudents = 10;
int grade, totalGrade = 0;
int studentCount = 0;

while (studentCount < numStudents) {


cout << "Enter the grade for student " << studentCount + 1 << ": ";
cin >> grade;

totalGrade += grade;
studentCount++;
}

double averageGrade = static_cast<double>(totalGrade) / numStudents;


cout << "The average exam grade for the class is: " << averageGrade << endl;

return 0;
}

M.ABDULLAH REHMAN 19
PROGRAMMING FUNDAMENTALS

Output:

Task 02: Write a program that calculates the average exam grade for a class of 10 students.
Exam grade defined by the user using do-while loop.

#include <iostream>
using namespace std;
int main() {
int numStudents = 10;
int grade, totalGrade = 0;
int studentCount = 0;

do {
cout << "Enter the grade for student " << studentCount + 1 << ": ";
cin >> grade;

totalGrade += grade;

M.ABDULLAH REHMAN 20
PROGRAMMING FUNDAMENTALS

studentCount++;
} while (studentCount < numStudents);

double averageGrade = static_cast<double>(totalGrade) / numStudents;


cout << "The average exam grade for the class is: " << averageGrade << endl;

return 0;
}
Output:

Task 03: Write a program that calculates the average exam grade for a class of 10 students.
Exam grade defined by the user using for loop.

#include <iostream>

using namespace std;

int main() {
int numStudents = 10;
int totalGrade = 0;

M.ABDULLAH REHMAN 21
PROGRAMMING FUNDAMENTALS

for (int studentCount = 0; studentCount < numStudents; studentCount++) {


int grade;
cout << "Enter the grade for student " << studentCount + 1 << ": ";
cin >> grade;

totalGrade += grade;
}

double averageGrade = static_cast<double>(totalGrade) / numStudents;


cout << "The average exam grade for the class is: " << averageGrade << endl;

return 0;
}

Output:

Task 04: Write a program to make a right angle triangle(*) using Nested loops

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

M.ABDULLAH REHMAN 22
PROGRAMMING FUNDAMENTALS

cout << "Enter the number of rows for the right angle triangle: ";
cin >> rows;
for (int i = 1; i <= rows; ++i) {
for (int j = 1; j <= i; ++j) {
cout << "* ";
}
cout << endl;
}
return 0;
}

Output:

M.ABDULLAH REHMAN 23
PROGRAMMING FUNDAMENTALS

Lab 06
If, If else, If else if
Task 1: Write a program to get age from the user if age is greater than 18 then print(“You can
vote”).(Using If Statement)
#include<iostream>
using namespace std;
int main()
{
int age;
cout << "Enter Your Age: ";
cin >> age;
if (age > 18)
{
cout << "You can vote";
}
return 0;
}
Output:

Task 2: Write a program to get age from the user if age is greater than 18 then print(“You can
vote”)Otherwise print(“Maybe Next Time)”.(Using If else Statement)
#include<iostream>
using namespace std;
M.ABDULLAH REHMAN 24
PROGRAMMING FUNDAMENTALS

int main()
{
int age;
cout << "Enter Your Age: ";
cin >> age;

if (age > 18)


{
cout << "You can vote";
}
else
{
cout << "Maybe Next Time";
}
return 0;
}
Output:

Task 3: Write a program to get a number(0 to 6) from the user and print week day.(Using If else
if Statement)
#include <iostream>
using namespace std;

int main() {

M.ABDULLAH REHMAN 25
PROGRAMMING FUNDAMENTALS

int num;

cout << "Enter a number (0 to 6): ";


cin >> num;
if (num == 0) {
cout << "Sunday";
} else if (num == 1) {
cout << "Monday";
} else if (num == 2) {
cout << "Tuesday";
} else if (num == 3) {
cout << "Wednesday";
} else if (num == 4) {
cout << "Thursday";
} else if (num == 5) {
cout << "Friday";
} else if (num == 6) {
cout << "Saturday";
} else {
cout << "Invalid input!";
}
return 0;
}
Output:

M.ABDULLAH REHMAN 26
PROGRAMMING FUNDAMENTALS

Lab 07
Switch Function

Task 1: Create a Program to print day of week name using switch...case.

#include<iostream>
using namespace std;

int main()
{
int day;

cout << "Enter a number: ";


cin >> day;

switch(day)
{
case 0:cout << "Sunday\n";
break;
case 1:cout << "Monday\n";
break;
case 2:cout << "Tuesday\n";
break;
case 3:cout << "Wednesday\n";
break;
case 4:cout << "Thursday\n";

M.ABDULLAH REHMAN 27
PROGRAMMING FUNDAMENTALS

break;
case 5:cout << "Friday\n";
break;
case 6:cout << "Saturday\n";
break;
default:cout << "Error-- Invalid Day\n";
break;
}

return 0;
}
Output:

Task 2: Using switch function to get two numbers from the user and get a arithmetic
function(+,-,/,*) then perform it.
#include <iostream>

using namespace std;

int main() {
double num1, num2;
char operation;

M.ABDULLAH REHMAN 28
PROGRAMMING FUNDAMENTALS

cout << "Enter two numbers: ";


cin >> num1 >> num2;

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


cin >> operation;

switch (operation) {
case'+':
cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
break;
case'-':
cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
break;
case'*':
cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;
break;
case'/':
if (num2 == 0) {
cout << "Error: Division by zero" << endl;
} else {
cout << num1 << " / " << num2 << " = " << num1 / num2 << endl;
}
break;
default:
cout << "Invalid operation" << endl;
}

M.ABDULLAH REHMAN 29
PROGRAMMING FUNDAMENTALS

return 0;
}
Output:

M.ABDULLAH REHMAN 30
PROGRAMMING FUNDAMENTALS

Lab 08
Task-1
Sample Programmer-Defined Function
Program:

#include <iostream>
void printMessage();
int main()
{ printMessage();
return 0;
}
void printMessage()
{ std::cout << "A message for you:\n\n";
std::cout << "Have a nice day!\n";
}
Output:

Task-2
Using Parameters

M.ABDULLAH REHMAN 31
PROGRAMMING FUNDAMENTALS

Program:
#include <iostream>
void printMessage(int counter);
int main()
{
int num;
std::cout << "Enter an integer: ";
std::cin >> num;

printMessage(num);

return 0;
}
void printMessage(int counter)
{
for (int i = 0; i < counter; i++)
{
std::cout << "Have a nice day!\n";
}
Output:

M.ABDULLAH REHMAN 32
PROGRAMMING FUNDAMENTALS

Task-3:
Using AverageTwo

Program:

#include <iostream>
float averageTwo(int num1, int num2);
int main()
{
float ave;
int value1 = 5, value2 = 8;
ave = averageTwo(value1, value2);
std::cout << "The average of " << value1 << " and " << value2 << " is " << ave << std::endl;

return 0;
}
float averageTwo(int num1, int num2)
{
float average;
M.ABDULLAH REHMAN 33
PROGRAMMING FUNDAMENTALS

average = (num1 + num2) / 2.0;


return average;
}
Output:

Task-4:
Sample Program

Program:

#include <iostream>
int funcA(int a, int b, int c);
int main()
{
int x = 3, y = 4, z = 5;
int result;
result = funcA(x, y, z);
std::cout << "result = " << result << std::endl;
result = funcA(6 + 4, y + 2, 9);
std::cout << "result = " << result << std::endl;
return 0;
}
int funcA(int a, int b, int c)
M.ABDULLAH REHMAN 34
PROGRAMMING FUNDAMENTALS

{
std::cout << "a = " << a << " b = " << b << " c = " << c << std::endl;
return (a + b + c);
}

Output:

M.ABDULLAH REHMAN 35
PROGRAMMING FUNDAMENTALS

Lab 9
Pointers

Task 1
#include <iostream>

// Function prototype for swap


void swap(int *, int *);
int main()
{
int x = 5, y = 7;

// calling swap with reference parameters


swap(&x, &y);
std::cout << "\n x is now " << x << " and y is now " << y << '\n';
return 0; // Added return statement for main
}

M.ABDULLAH REHMAN 36
PROGRAMMING FUNDAMENTALS

// swap function is defined here using dereferencing operator '*'


void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

Output :

Task 2
#include <iostream>
int main()
{
int intVar = 10;

M.ABDULLAH REHMAN 37
PROGRAMMING FUNDAMENTALS

int* intPtr; // intPtr is a pointer


intPtr = &intVar;

std::cout << "\nLocation of intVar: " << &intVar;


std::cout << "\nContents of intVar: " << intVar;
std::cout << "\nLocation of intPtr: " << &intPtr;
std::cout << "\nContents of intPtr: " << intPtr;
std::cout << "\nThe value that intPtr points to: " << *intPtr;

return 0;
}
Output :

M.ABDULLAH REHMAN 38
PROGRAMMING FUNDAMENTALS

LAB 10
Arrays
Task 1
#include <iostream>
int main() {
// Declare an array of integers with size 5
int numbers[5];
// Initialize the array elements
for (int i = 0; i < 5; ++i) {
numbers[i] = i * 2;
}
// Access and print the array elements
std::cout << "Array elements: ";
for (int i = 0; i < 5; ++i) {
std::cout << numbers[i] << " ";
}
return 0;
}
M.ABDULLAH REHMAN 39
PROGRAMMING FUNDAMENTALS

Output:

Task 2
#include <iostream>
int main() {
const int size = 5;
int numbers[size] = {1, 2, 3, 4, 5}; // Initializing array elements

// Calculate the sum of array elements


int sum = 0;
for (int i = 0; i < size; ++i) {
sum += numbers[i];
}
// Print the array elements and the sum
std::cout << "Array elements: ";
for (int i = 0; i < size; ++i) {
std::cout << numbers[i] << " ";
}
std::cout << "\nSum of array elements: " << sum << std::endl;
M.ABDULLAH REHMAN 40
PROGRAMMING FUNDAMENTALS

return 0;
}

Output:

M.ABDULLAH REHMAN 41
PROGRAMMING FUNDAMENTALS

TASK 3
#include <iostream>
int main() {
const int size = 5;
int numbers[size];

// Ask the user to enter five numbers


std::cout << "Enter five numbers:\n";
for (int i = 0; i < size; ++i) {
std::cout << "Number " << i + 1 << ": ";
std::cin >> numbers[i];
}

// Print the numbers in reverse order


std::cout << "Numbers in reverse order: ";
for (int i = size - 1; i >= 0; --i) {
std::cout << numbers[i] << " ";
}

return 0;
}

M.ABDULLAH REHMAN 42
PROGRAMMING FUNDAMENTALS

Output :

M.ABDULLAH REHMAN 43
PROGRAMMING FUNDAMENTALS

Lab 11
File handling
Task 1
#include <fstream>
using namespace std; // Add this line to use std::ofstream and std::ios

int main(void)
{
ofstream outFile("file1.txt", ios::out);
outFile << "That's new!\n";
outFile.close();

return 0; // Corrected to lowercase 'return'


}

Output :

M.ABDULLAH REHMAN 44
PROGRAMMING FUNDAMENTALS

Task 2
Using get () and put()
#include <iostream>
#include <fstream>
int main()
{
std::fstream File("test_file", std::ios::out | std::ios::in | std::ios::binary);
char ch;
ch = 'o';
File.put(ch); // put the content of ch to the file
File.seekg(std::ios::beg); // go to the beginning of the file
File.get(ch); // read one character
std::cout << ch << std::endl; // display it
File.close();

return 0;
}
Output :

M.ABDULLAH REHMAN 45
PROGRAMMING FUNDAMENTALS

Task 3
#include <fstream>
using namespace std;

int main(void)
{
ofstream outFile("fout.txt");
outFile << "Hello World!";
outFile.close();

return 0;
}

Output :

M.ABDULLAH REHMAN 46
PROGRAMMING FUNDAMENTALS

Task 4
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Writing to file
ofstream OutFile("my_file.txt");
OutFile << "Hello " << 5 << endl;
OutFile.close();

int number;
char dummy[15];

M.ABDULLAH REHMAN 47
PROGRAMMING FUNDAMENTALS

// Reading from file


ifstream InFile("my_file.txt");
InFile >> dummy >> number;
InFile.seekg(0);
InFile.getline(dummy, sizeof(dummy));
InFile.close();

return 0;
}

Output :

M.ABDULLAH REHMAN 48
PROGRAMMING FUNDAMENTALS

THE END
<--------------------------------------------------------------------------------------------->

M.ABDULLAH REHMAN 49

You might also like