PF Lab Manual 2023
PF Lab Manual 2023
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 GA
? ?
No No
IS GC
B> C No
?
Yes
GB
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
GC
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:
M.ABDULLAH REHMAN 3
PROGRAMMING FUNDAMENTALS
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
No
Cookies After
Print Total
Second Child If Z=3 Cookies=X
Algorithm:
Z=Y-(Y/2) Yes
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’
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.
M.ABDULLAH REHMAN 9
PROGRAMMING FUNDAMENTALS
36: $ 37: %
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: ;
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: }
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;
return 0;
}
M.ABDULLAH REHMAN 14
PROGRAMMING FUNDAMENTALS
Output:
#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;
totalGrade += grade;
studentCount++;
}
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);
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>
int main() {
int numStudents = 10;
int totalGrade = 0;
M.ABDULLAH REHMAN 21
PROGRAMMING FUNDAMENTALS
totalGrade += grade;
}
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;
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;
M.ABDULLAH REHMAN 26
PROGRAMMING FUNDAMENTALS
Lab 07
Switch Function
#include<iostream>
using namespace std;
int main()
{
int 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>
int main() {
double num1, num2;
char operation;
M.ABDULLAH REHMAN 28
PROGRAMMING FUNDAMENTALS
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
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>
M.ABDULLAH REHMAN 36
PROGRAMMING FUNDAMENTALS
Output :
Task 2
#include <iostream>
int main()
{
int intVar = 10;
M.ABDULLAH REHMAN 37
PROGRAMMING FUNDAMENTALS
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
return 0;
}
Output:
M.ABDULLAH REHMAN 41
PROGRAMMING FUNDAMENTALS
TASK 3
#include <iostream>
int main() {
const int size = 5;
int numbers[size];
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();
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
return 0;
}
Output :
M.ABDULLAH REHMAN 48
PROGRAMMING FUNDAMENTALS
THE END
<--------------------------------------------------------------------------------------------->
M.ABDULLAH REHMAN 49