0% found this document useful (0 votes)
19 views16 pages

XII Computer PBA List of FBISE Practicals Solution

The document provides a comprehensive guide on various C++ programming tasks, including installation of IDEs, writing basic programs, and solving mathematical problems. It covers topics such as arithmetic operations, geometry calculations, string comparisons, and matrix operations. Each section includes example code snippets and explanations to assist learners in understanding and implementing C++ concepts.

Uploaded by

hassnain1502007
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)
19 views16 pages

XII Computer PBA List of FBISE Practicals Solution

The document provides a comprehensive guide on various C++ programming tasks, including installation of IDEs, writing basic programs, and solving mathematical problems. It covers topics such as arithmetic operations, geometry calculations, string comparisons, and matrix operations. Each section includes example code snippets and explanations to assist learners in understanding and implementing C++ concepts.

Uploaded by

hassnain1502007
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/ 16

XII Computer PBA List of Practical FBISE Solution

Question 1: Installation and Familiarization


Answer: Install any of your favorite ide (VSCODE , Dev C++ , turbo C++)from Internet
After downloading, install it by Run as administrator then
configure it to use your installed compiler.
Write a Test Program:
Create a simple "Hello, world!" program to ensure everything is working:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World! \n";
return 0;
}
Compile and run
Save the code as filename.cpp
Run and execute the program and check output.
Question 2: Familiarization with IDE of C++
Answer: There are 5 components of IDE of C++
Text editor : Like Word Processor you can create edit save the file
Compiler : Converts the code written in high level language into low level language
Linker : Creates an executable file
Loader: It loads the program into memory and runs it
Debugger: It highlights and removes errors in program
Question 3: Write some programs using: cin , cout , escape sequences Setw
#include <iostream>
#include <iomanip>
int main()
{
// Using cin , cout
int age;
cout << "Enter your age: ";
cin >> age;
cout << "You entered: " << age << endl;

// Using cout with escape sequences


cout << "This is a string with a newline character: \\n" << endl;
cout << "This is a string with a tab character: \\tTabbed text" << endl;
cout << "This is a string with a backslash: \\\\" << endl;
cout << "This is a string with a double quote: \\\"Quoted text\\\"" << endl;
cout << "This is a string with a carriage return: first line\rsecond line" << endl; // overwrites first line

// Using setw
cout << setw(10) << "Name" << setw(10) << "Age" << endl;
cout << setw(10) << "Alice" << setw(10) << 30 << endl;
cout << setw(10) << "Bob" << setw(10) << 25 << endl;
return 0;
}

Prepared By: M.Umair (0310-5670886) Page 1


Question 4: Write program for problems like: Solving arithmetic problems to (calculate
interest, percentage, average, ratio, grades etc.)
#include <iostream>
using namespace std;
int main()
{
int choice;
double num1, num2, num3;
do {
cout << "\n Arithmetic Problem Solver \n";
cout << "1. Calculate Interest \n";
cout << "2. Calculate Percentage \n";
cout << "3. Calculate Average \n";
cout << "4. Calculate Ratio \n";
cout << "5. Calculate Grade \n";
cout << "6. Exit \n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
{
double principal, rate, time, interest;
cout << "Enter principal amount: ";
cin >> principal;
cout << "Enter interest rate (decimal): ";
cin >> rate;
cout << "Enter time (years): ";
cin >> time;
interest = principal * rate * time;
cout << "Interest: " << interest << endl;
break;
}
case 2:
{
double part, whole, percentage;
cout << "Enter the part: ";
cin >> part;
cout << "Enter the whole: ";
cin >> whole;
percentage = (part / whole) * 100;
cout << "Percentage: " << percentage << "%" << endl;
break;
}
case 3:
{
double a, b, c, average;
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";

Prepared By: M.Umair (0310-5670886) Page 2


cin >> b;
cout << "Enter third number: ";
cin >> c;
average = (a + b + c) / 3;
cout << "Average: " << average << endl;
break;
}
case 4:
{
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << "Ratio: " << num1 << " : " << num2 << endl;
break;
}
case 5:
{
double score;
cout << "Enter score: ";
cin >> score;
if (score >= 90)
{
cout << "Grade: A" << endl;
}
else if (score >= 80)
{
cout << "Grade: B" << endl;
}
else if (score >= 70)
{
cout << "Grade: C" << endl;
}
else if (score >= 60)
{
cout << "Grade: D" << endl;
} else
{
cout << "Grade: F" << endl;
}
break;
}
case 6:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
}
while (choice != 6);

Prepared By: M.Umair (0310-5670886) Page 3


return 0;
}
Question 5: Calculating area / volume / perimeter of some basic geometrical shapes
#include <iostream>
#include <cmath> // For M_PI and pow()
using namespace std;
int main()
{
int choice;
double side, length, width, height, radius, base;
cout << "Geometry Calculator \n";
cout << "1. Calculate Area of a Square \n";
cout << "2. Calculate Area of a Rectangle \n";
cout << "3. Calculate Area of a Triangle \n";
cout << "4. Calculate Area of a Circle \n";
cout << "5. Calculate Volume of a Cube \n";
cout << "6. Calculate Volume of a Rectangular Prism \n";
cout << "7. Calculate Volume of a Cylinder \n";
cout << "8. Calculate Perimeter of a Square \n";
cout << "9. Calculate Perimeter of a Rectangle \n";
cout << "10. Calculate Circumference of a Circle \n";
cout << "11. Exit \n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice)
{
case 1: // Area of Square
cout << "Enter side length: ";
cin >> side;
cout << "Area: " << side * side << endl;
break;
case 2: // Area of Rectangle
cout << "Enter length: ";
cin >> length;
cout << "Enter width: ";
cin >> width;
cout << "Area: " << length * width << endl;
break;
case 3: // Area of Triangle
cout << "Enter base: ";
cin >> base;
cout << "Enter height: ";
cin >> height;
cout << "Area: " << 0.5 * base * height << endl;
break;
case 4: // Area of Circle
cout << "Enter radius: ";
cin >> radius;
cout << "Area: " << M_PI * pow(radius, 2) << endl;

Prepared By: M.Umair (0310-5670886) Page 4


break;
case 5: // Volume of Cube
cout << "Enter side length: ";
cin >> side;
cout << "Volume: " << pow(side, 3) << endl;
break;
case 6: // Volume of Rectangular Prism
cout << "Enter length: ";
cin >> length;
cout << "Enter width: ";
cin >> width;
cout << "Enter height: ";
cin >> height;
cout << "Volume: " << length * width * height << endl;
break;
case 7: // Volume of Cylinder
cout << "Enter radius: ";
cin >> radius;
cout << "Enter height: ";
cin >> height;
cout << "Volume: " << M_PI * pow(radius, 2) * height << endl;
break;
case 8: // Perimeter of Square
cout << "Enter side length: ";
cin >> side;
cout << "Perimeter: " << 4 * side << endl;
break;
case 9: // Perimeter of Rectangle
cout << "Enter length: ";
cin >> length;
cout << "Enter width: ";
cin >> width;
cout << "Perimeter: " << 2 * (length + width) << endl;
break;
case 10: // Circumference of Circle
cout << "Enter radius: ";
cin >> radius;
cout << "Circumference: " << 2 * M_PI * radius << endl;
break;
case 11:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice.\n";
}
return 0;
}
Question 6: Comparing numbers / strings.
#include <iostream>
#include <cstring>

Prepared By: M.Umair (0310-5670886) Page 5


using namespace std;
int main()
{
// Comparing Numbers
int num1, num2;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
if (num1 > num2)
{
cout << num1 << " is greater than " << num2 << endl;
} else if (num1 < num2)
{
cout << num1 << " is less than " << num2 << endl;
} else {
cout << num1 << " is equal to " << num2 << endl;
}
// Comparing Strings
char str1[60], str2[60];
// Consume the newline left in the buffer by the previous cin
cin.ignore();
cout << "Enter the first string: ";
cin.get(str1, 60);
cin.ignore();
cout << "Enter the second string: ";
cin.get(str2,60);
if (str1 == str2)
{
cout << "The strings are equal." << endl;
}
else if (str1 < str2)
{
cout << str1 << " comes before " << str2 << "." << endl;
}
else
{
cout << str1 << " comes after " << str2 << " ." << endl;
}
return 0;
}
Question 7: Solving quadratic equation
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
double a, b, c, discriminant, root1, root2;

Prepared By: M.Umair (0310-5670886) Page 6


cout << "Enter a, b, c: ";
cin >> a >> b >> c;
if(a!=0) {
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
cout << "Roots: " << root1 << ", " << root2 << endl;
} else if (discriminant == 0) {
root1 = -b / (2 * a);
cout << "Root: " << root1 << endl;
} else {
cout << "Complex roots" << endl;
}
}
else
{
cout<<"invalid input ";
}
return 0;
}
Question 8: Finding out the GCD and LCM.
#include <iostream>
using namespace std;
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// Function to calculate LCM using GCD
int lcm(int a, int b)
{
return (a * b) / gcd(a, b);
}
int main()
{
int num1, num2;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
int resultGCD = gcd(num1, num2);
int resultLCM = lcm(num1, num2);
cout << "GCD of " << num1 << " and " << num2 << " is: " << resultGCD << endl;
cout << "LCM of " << num1 << " and " << num2 << " is: " << resultLCM << endl;
return 0;
}

Prepared By: M.Umair (0310-5670886) Page 7


Question 9: Reading a number and find out whether it is a prime or composite
#include <iostream>
using namespace std;
void prime(int a)
{
int b=0,c;
if (a==0||a==1)
{
cout<<"Its neither prime nor composite";
return;
}
for(int i=1;i<=a;i++)
{
if(a%i==0)
{
b++;
}
}
if (b==2)
{
cout<<"It is a prime num";
}
else
{
cout<<"it is composite";
}
}
int main()
{
int number;
cout<<"enter any number: ";
cin>>number;
prime(number);
}
Question 10: Sorting a list of items (numeric / string)
//Comparing strings
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string input;
cout << "Enter a string: ";
getline(cin, input); // Read the entire line, including spaces
sort(input.begin(), input.end());
cout << "Sorted string: " << input << endl;
return 0;
}
//Comparing Numbers

Prepared By: M.Umair (0310-5670886) Page 8


#include <iostream>
#include <algorithm> // for sort function
using namespace std;
int main()
{
int size = 10;
int numbers[size]; // Assuming a maximum of 10 numbers
cout << "Enter numbers:\n";
for (int i = 0; i < size; i++)
{
cin >> numbers[i];
}
sort(numbers, numbers + size);
cout << "Sorted numbers: ";
for (int i = 0; i < size; i++)
{
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}
Question 11: Searching an item out of a list of items (numeric / string)
#include <iostream>
using namespace std;
int main()
{
int size = 3;
int numbers[size]; // Assuming a maximum of 100 numbers
int num;
cout << "Enter numbers :\n";
for (int i = 0; i < size; i++)
{
cin>>numbers[i];
}
int searchNum;
cout << "Enter the number to search: ";
cin >> searchNum;
int found = 0;
for (int i = 0; i < size; i++)
{
if (numbers[i] == searchNum)
{
found = 1;
break;
}
}
if (found)
{
cout << searchNum << " found in the array.\n";
} else {

Prepared By: M.Umair (0310-5670886) Page 9


cout << searchNum << " not found in the array.\n";
}
return 0;
}
Question 12: Generating random numbers for a dice using function.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int rollDice()
{
return (rand() % 6) + 1;
}
int main() {
srand(time(0)); // Seed the random number generator
cout << "You rolled a " << rollDice() << endl;
return 0;
}
Question 13: Finding addition and multiplication of a matrices (Maximum 3 x 3)
#include <iostream>
using namespace std;
int main()
{
int matrix1[3][3], matrix2[3][3], result[3][3];
int rows1, cols1, rows2, cols2;
cout << "Enter dimensions of matrix 1 (rows cols): ";
cin >> rows1 >> cols1;
cout << "Enter elements of matrix 1:\n";
for (int i = 0; i < rows1; i++)
{
for (int j = 0; j < cols1; j++)
{
cin >> matrix1[i][j];
}
}
cout << "Enter dimensions of matrix 2 (rows cols): ";
cin >> rows2 >> cols2;
cout << "Enter elements of matrix 2:\n";
for (int i = 0; i < rows2; i++)
{
for (int j = 0; j < cols2; j++)
{
cin >> matrix2[i][j];
}
}
// Addition
if (rows1 == rows2 && cols1 == cols2)
{
cout << "Matrix Addition:\n";
for (int i = 0; i < rows1; i++)

Prepared By: M.Umair (0310-5670886) Page 10


{
for (int j = 0; j < cols1; j++)
{
result[i][j] = matrix1[i][j] + matrix2[i][j];
cout << result[i][j] << " ";
}
cout << endl;
}
}
else
{
cout << "Matrix addition not possible (dimensions mismatch).\n";
}
// Multiplication
if (cols1 == rows2)
{
cout << "Matrix Multiplication:\n";
for (int i = 0; i < rows1; i++)
{
for (int j = 0; j < cols2; j++)
{
result[i][j] = 0;
for (int k = 0; k < cols1; k++)
{
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
cout << result[i][j] << " ";
}
cout << endl;
}
}
Else
{
cout << "Matrix multiplication not possible (dimensions mismatch).\n";
}
return 0;
}
Question 14:Finding the transpose of a matrix (3 x 3)
#include <iostream>
using namespace std;
int main()
{
int matrix[3][3];
int transpose[3][3];
cout << "Enter the elements of the 3x3 matrix:\n";
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> matrix[i][j];

Prepared By: M.Umair (0310-5670886) Page 11


}
}
// Calculate the transpose
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
transpose[j][i] = matrix[i][j];
}
}
// Display the transpose
cout << "Transpose of the matrix:\n";
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << transpose[i][j] << " ";
}
cout << endl;
}
return 0;
}
Question 15: Generating and summing simple series
#include <iostream>
using namespace std;
int main()
{
int n, sum = 0;
cout << "Enter the value of n: ";
cin >> n;
for (int i = 1; i <= n; i++)
{
sum += i;
}
cout << "Sum of first " << n << " natural numbers: " << sum << endl;
return 0;
}
Question 16: Reversing a given number / string
// (a) Reverse numbers
#include <iostream>
using namespace std;
int main() {
int number, reversedNumber = 0;
cout << "Enter a number: ";
cin >> number;
while (number != 0)
{
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number /= 10;

Prepared By: M.Umair (0310-5670886) Page 12


}
cout << "Reversed number: " << reversedNumber << endl;
return 0;
}
// (b) Reverse String
#include <iostream>
#include <cstring> // For strlen and character manipulation
using namespace std;
int main()
{
char str[100];
cout << "Enter a string: ";
cin.get(str, 100);
cin.ignore();
int length = strlen(str);
// Reverse the string in place
for (int i = 0, j = length - 1; i < j; i++, j--)
{
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
cout << "Reversed string: " << str << endl;
return 0;
}
Section-B
I. Write program for problems like: Finding out a specific day of a week for a given data
using function.
#include<iostream>
using namespace std;
int weekDay(int x)
{
switch(x)
{
case 1:
cout<<"Its monday";
break;
case 2:
cout<<"Its Tuesday";
break;
case 3:
cout<<"Its Wednesday";
break;
case 4:
cout<<"Its Thursday";
break;
case 5:
cout<<"Its Friday";
break;
case 6:

Prepared By: M.Umair (0310-5670886) Page 13


cout<<"Its Saturday";
break;
case 7:
cout<<"Its Sunday";
break;
default:
cout<<"Please enter valid number (1-7)"
}
}
int main()
{
int y;
cout<<"Please enter valid number (1-7)";
cin>>y;
weekDay(y);
return 0;
}

ii. Write a program to sum two and three numbers of different date types.
#include<iostream>
using namespace std;
int addTwoNumbers(int x, float y)
{
float sum=x+y;
return sum;
}
float addThreeNumbers(int x, float y, float z)
{
float sum= x+y+z;
return sum;
}
int main( ) {
int a;
float b, c;
cout<<"Enter 3 values of different data types\nNote: First value should be of integer type and other should
be of floating types:\n";
cin>>a>>b>>c;
cout<<"\nThe sum of two different numbers are"<<addTwoNumbers(a,b);
cout<<"\n\nThe sum of two different numbers are"<<addThreeNumbers(a,b,c);
return 0;
}
iii. Write a programme to display the address and the value of a variable using pointer
#include<iostream>
using namespace std;
int main(){
int var= 3, *pvar;
pvar= &var;
cout<<pvar<<"\n"<<*pvar;
return 0;
}

Prepared By: M.Umair (0310-5670886) Page 14


iv. Write a program to create and display student object with data members as name, age
and class.
#include<iostream>
using namespace std;
class Student{
private:
string name;
int age;
int Class;
public:
void inputData(string n, int a, int c){
name=n;
age=a;
Class=c;
}
void displaystd()
{
cout<<"Name:"<<name <<"\n Age: "<<age<<"\n class: "<<Class;
}
};
int main()

{
Student S1;
S1.inputData("Saim", 17, 7);
S1.displaystd();
return 0;
}
v. Write a program to create and read a data file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// Write to a file
ofstream outFile("data.txt"); // Create or open a file for writing
if (outFile.is_open())
{
outFile << "This is a line of text.\n";
outFile << "Another line with numbers: 123 45.6\n";
outFile.close(); // Close the file
cout << "Data written to file successfully.\n";
}
else
{
cerr << "Unable to open file for writing.\n";
return 1; // Indicate an error
}
// Read from a file

Prepared By: M.Umair (0310-5670886) Page 15


ifstream inFile("data.txt"); // Open the same file for reading
string line;
if (inFile.is_open()) {
cout << "\nReading from file:\n";
while (getline(inFile, line))
{ // Read line by line
cout << line << endl;
}
inFile.close(); // Close the file
}
else
{
cerr << "Unable to open file for reading.\n";
return 1; // Indicate an error
}
return 0;
}

***************************************************

Prepared By: M.Umair (0310-5670886) Page 16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy