Computer Science Practicals - XII
Sn Object Date Signature
o:
1 Write a C++ program to swap two numbers Aug 22, 2024
without using the third variable.
2 Write a C++ program which takes input Aug 29, 2024
from the user and shows whether a number
is positive, negative or zero.
3 Write a program which takes input for 3 Sept 05, 2024
numbers and find the greatest among them.
4 Write a program which takes marks as an Sept 12, 2024
input and calculates the grades of
students based on marks.
5 Write a program in C++ and show whether Sept 19, 2024
the input character is vowel or consonant
using switch case.
6 Write a C++ program to calculate the sum Sept 26, 2024
of the first 10 positive odd numbers.
7 Write the program to print the factorial Oct 03, 2024
of an input number.
8 Generate the following pattern by using Oct 10, 2024
nested For loop.
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
9 Print the following format using nested Oct 10, 2024
for loop.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
10 Print the following output by using Oct 17, 2024
nested for loop.
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
11 Print the following output by using Oct 17, 2024
nested for loop.
*
* *
* * *
* * * *
* * * * *
12 Write a C++ program which stores numeric Oct 24, 2024
values in a one-dimensional array
using a for loop and finds the highest,
lowest and average values.
13 Write a program to make a signup Oct 31, 2024
interference which takes user name,
password, retype-password as an input and
compares the password and re-enter
password of the user for sign up, using
strings library.
14 Write a program to add two matrices of Nov 07, 2024
order up to 4×4 using a two dimensional
array and show the ordered output in the
third matrix.
15 Write a program involving use of user Nov 14, 2024
defined function to calculate volume of
cylinder, sphere and cube.
16 Write a program involving user defined Nov 14, 2024
functions to calculate the average of
given numbers.
17 Write a program involving a user defined Nov 21, 2024
function to check whether the input
number is a prime number or not.
18 Write a simple program using a reference Nov 28, 2024
and dereference operator to find the
value and address of each element of an
array using pointers. (Hint: Use for
loop)
19 Write a C++ program in which a class uses Jan 30, 2025
both public and private access
specifiers.
20 Write a C++ program to implement the Feb 06, 2025
concept of multiple
inheritance in object oriented
programming.
21 Write a C++ program to read and print Feb 13, 2025
employee information using multiple
inheritance.
Practical 01:
Write a C++ program to swap two numbers without using the third variable.
Code:
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
cout << "Before Swapping: \n" << "a = " << a << ", " << "b = " << b;
a *= b;
b = a / b;
a = a / b;
cout << "\nAfter Swapping: \n" << "a = " << a << ", " << "b = " << b;
}
Result
Practical 02:
Write a C++ program which takes input from the user and shows whether a number is
positive, negative or zero.
Code:
#include <iostream>
using namespace std;
int main() {
int a;
cout << "Enter a number: ";
cin >> a;
if (a > 0)
cout << a << " is a positive number.";
else if (a < 0)
cout << a << " is a negative number.";
else
cout << a << " is zero.";
}
Practical 03:
Write a program which takes input for 3 numbers and find the greatest among them.
Result:
#include <iostream>
using namespace std;
int main()
{
int a,b,c,largest;
cout<<"enter 3 numbers one by one"<<endl;
cout<<"enter 1st number"<<endl;
cin>>a;
cout<<"enter 1st number"<<endl;
cin>>b;
cout<<"enter 1st number"<<endl;
cin>>c;
a=largest;
if(b>largest)
{
largest=b;
}
if(c>largest)
{
largest=c;
}
cout<<largest;
return 0;
}
PRACTICAL 4.
Write a program which takes marks as an input and calculates the grades of
students based on
marks.
Code:
#include<iostream>
using namespace std;
#include<conio.h>
int main() {
int Sub, Marks,
T_Marks=650; float
Percentage;
char Grade;
cout<<"Enter Marks for Mathematics (Out of
100): "; cin>>Sub;
Marks=Sub;
cout<<"Enter Marks for Physics (Out of 100):
"; cin>>Sub;
Marks+=Sub;
cout<<"Enter Marks for Urdu (Out of 100): ";
cin>>Sub;
Marks+=Sub;
cout<<"Enter Marks for Computer Sciences (Out of
100): "; cin>>Sub;
Marks+=Sub;
cout<<"Enter Marks for English (Out of 100):
"; cin>>Sub;
Marks+=Sub;
cout<<"Enter Marks for Islamiat (Out of 75):
"; cin>>Sub;
Marks+=Sub;
cout<<"Enter Marks for Pakistan Studies (Out of 75):
"; cin>>Sub;
Marks+=Sub;
Percentage=(float) 100*Marks/T_Marks;
if(Percentage>=80) {
Grade='A';
}
else if(Percentage>=70 && Percentage<80) {
Grade='B';
}
else if(Percentage>=60 && Percentage<70) {
Grade='C';
}
else if(Percentage>=50 && Percentage<60) {
Grade='D';
}
else {
Grade='F';
}
Grade='A';
}
else if(Percentage>=70 && Percentage<80) {
Grade='B';
}
else if(Percentage>=60 && Percentage<70) {
Grade='C';
}
else if(Percentage>=50 && Percentage<60) {
Grade='D';
}
else {
Grade='F';
cout<<"Your Percentage is: "<<Percentage<<endl
<<"Your Grade is: "<<Grade;
getch();
return 0;
}
PRACTICAL 5.
Write a program in C++ and show whether the input character is vowel or
consonant using
switch case.
PRACTICAL 6.
Write a C++ program to calculate the sum of the first 10 positive odd numbers.
PRACTICAL 7.
Write the program to print the factorial of an input number.
PRACTICAL 8.
Generate the following pattern by using nested For loop.
**********
**********
**********
**********
#include <iostream>
using namespace std;
int main() {
int i,j,k,a;
j=10;
a=4;
for(k=1;k<=a;k++){
for(i=1;i<=j;i++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
PRACTICAL 9.
Print the following format using nested for loop.
1
12
123
1234
12345
123456
1234567
#include <iostream>
using namespace std;
int main() {
int i,j,k,a;
j=1;
a=7;
for(k=1;k<=a;k++){
for(i=1;i<=j;i++)
{
cout<<i;
}
cout<<endl;
j++;
}
return 0;
}
PRACTICAL 10.
Print the following output by using nested for loop.
*
***
*****
*******
*********
#include <iostream>
using namespace std;
int main() {
int i,j,k,a;
j=1;
a=9;
for(k=1;k<=a;k++){
for(i=1;i<=j;i++)
{
if(j%2==1) { cout<<"*";}
}
cout<<endl;
j++;
}
return 0;
}
PRACTICAL 11.
Print the following output by using nested for loop.
*
**
***
****
*****
#include <iostream>
using namespace std;
int main() {
int i,j,n;
n=5;
for(i=1;i<=n;i++){
for(j=1;j<=(n-i);j++)
{
cout<<" ";
}
for(j=1;j<=i;j++){
cout<<"* ";
}
cout<<endl;
}
return 0;
}
PRACTICAL 12.
Write a C++ program which stores numeric values in a one-dimensional array
using for loop
and finds the highest, lowest and average values.
#include <iostream>
using namespace std;
int main() {
int k[10], a, i, j, s = 0;
float avg;
for (i = 0; i < 10; i++) {
cout << "Enter the " << i + 1 << " element: ";
cin >> k[i];
s += k[i];
}
for (i = 0; i < 10; i++) {
for (j = i; j < 10; j++) {
if (k[i] > k[j]) {
a = k[i];
k[i] = k[j];
k[j] = a;
}
}
}
cout << "The maximum number is: " << k[9];
cout << "\nThe minimum number is: " << k[0];
avg = (float)s / 10.0;
cout << "\nAverage of all values is: " << avg;
}
PRACTICAL 13.
Write a program to make a signup interference which takes user name, password,
retype-
password as an input and compare the password and re-enter password of user for
sign up,
using strings library.
#include <string>
#include <iostream>
using namespace std;
int main() {
char k1[15], k2[15], u[15];
cout << "Enter Username: ";
cin.get(u, 15);
cin.ignore();
cout << "Enter Password: ";
cin.get(k1, 15);
cin.ignore();
cout << "Confirm Password: ";
cin.get(k2, 15);
if (strcmp(k1, k2) == 0) {
cout << "Login Successful!";
} else {
cout << "Password do not match, Login Unsuccessful!";
}
}
PRACTICAL 14.
Write a program to add two matrices of order up to 4×4 using a two dimensional
array and
show the ordered output in third matrix.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int k1[4][4], k2[4][4], s[4][4], i, j, k;
cout << "Matrix A: " << endl;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
cout << "Enter an element in row " << i + 1 << " column " << j + 1 << ": ";
cin >> k1[i][j];
}
}
cout << "Matrix B: " << endl;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
cout << "Enter an element in row " << i + 1 << " column " << j + 1 << ": ";
cin >> k2[i][j];
}
}
cout << "The sum of matrix A and B: " << endl;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
s[i][j] = k1[i][j] + k2[i][j];
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
cout << setw(3) << s[i][j];
}
cout << endl;
}
cout << endl;
}
PRACTICAL 15.
Write a program involving use of user defined function to calculate volume of
cylinder,
sphere and cube.
Here's the code from the last file you provided:
#include <iostream>
using namespace std;
float sphere(float r) {
float V = (4.0/3.0) * 3.142 * (r*r*r);
return V;
}
float cylinder(float r, float h) {
float V = 3.142 * (r*r) * h;
return V;
}
float cube(float l) {
float V = l*l*l;
return V;
}
int main() {
int r1, r2, H, l, V1, V2, V3;
cout << "Enter radius of sphere: ";
cin >> r1;
V1 = sphere(r1);
cout << "\nVolume of sphere: " << V1;
cout << "\n\nEnter radius of cylinder: ";
cin >> r2;
cout << "Enter height of cylinder: ";
cin >> H;
V2 = cylinder(r2, H);
cout << "\nVolume of cylinder: " << V2;
cout << "\n\nEnter length of cube: ";
cin >> l;
V3 = cube(l);
cout << "\nVolume of cube: " << V3;
}
PRACTICAL 16.
Write a program involving user defined function to calculate average of given
numbers.
Code:
#include<iostream>
using namespace std;
#include<conio.h>
void Avg(int &n) {
int sum = 0, arr[n]; float
avg; for (int i=0; i<n; i++) {
cout<<"Enter the value number "<<i+1<<": ";
cin>>arr[i];
sum = sum + arr[i];
}
avg = (float) sum/n;
cout<<"The average of "<<n<<" numbers is: "<<avg;
}
main() {
int n;
cout<<"Enter how many numbers you want to take an average of:
"; cin>>n;
Avg(n);
getch();
exit(0);
}
PRACTICAL 17.
Write a program involving a user defined function to check whether the input
number is a
prime number or not.
PRACTICAL 18.
Write a simple program using referencing and dereference operator to find the
value and
address of each element of an array using pointers. (Hint: Use for loop)
Code:
#include<iostream>
#include<conio.h>
using namespace
std;
int main() {
int n1, *ptr1, n2, *ptr2;
cout<<"How many elements do you wanna have in array
01? "; cin>>n1;
int arr1[n1];
cout<<"How many elements do you wanna have in array
02? "; cin>>n2;
int arr2[n2];
for(int i=0; i<n1; i++) {
cout<<"Enter element "<<i+1<<" of Array 01:
"; cin>>arr1[i];
}
for(int i=0; i<n2; i++) {
cout<<"Enter element "<<i+1<<" of Array 02:
"; cin>>arr2[i];
}
cout<<"To find out each of the address and value of your
arrays, press any key...";
getch();
ptr1=arr1;
ptr2=arr2;
cout<<endl<<endl<<"In Array 01, we have:"<<endl;
for(int i=0; i<n1; i++) {
cout<<"\tAddress of element "<<i+1<<" is: "<<ptr1<<endl;
cout<<"\tValue of element "<<i+1<<" is: "<<*ptr1<<endl;
++ptr1;
}
cout<<endl<<endl<<"In Array 02, we have: "<<endl;
for(int i=0; i<n2; i++) {
cout<<"\tAddress of element "<<i+1<<" is: "<<ptr2<<endl;
cout<<"\tValue of element "<<i+1<<" is: "<<*ptr2<<endl;
++ptr2;
}
getch(); return 0;
}
PRACTICAL 19.
Write a C++ program in which a class uses both public and private access
specifiers.
Code:
#include<iostream>
using namespace std;
class Human {
private:
int Salary;
string Title;
public:
int Age;
string Name;
Human(int s, string t)
{ Salary = s;
Title = t;
}
void getStats() {
system("cls");
cout<<"Human Name: "<<Title<<". "<<Name<<endl
<<Name<<"\'s Age: "<<Age<<endl
<<Name<<"\'s Salary: "<<Salary<<endl;
}
};
int main() {
string n, t;
int A, s;
cout<<"Enter the Name of the human: ";
cin>>n;
cout<<"Enter Title (Mr, Mrs, Miss, Dr, etc):
"; cin>>t;
cout<<"Enter Salary: ";
cin>>s;
Human H1(s, t);
cout<<"Enter Age:
"; cin>>H1.Age;
H1.Name = n;
H1.getStats();
return 0;
}
before
after
PRACTICAL 20.
Write a C++ program to implement the concept of multiple inheritance in object
oriented
programming.
Code:
#include<iostream>
using namespace std;
class Animal {
public:
Animal() {
cout<<"I am an Animal"<<endl;
}
};
class Mammal {
public:
Mammal() {
cout<<"I am a Mammal\n";
}
};
class Whales : public Animal, public Mammal {
public:
Whales() {
cout<<"A whale is an Animal and a Mammal\t-\n";
}
};
int main() {
cout<<"Animal initialize:
"; Animal A;
cout<<endl<<"Mammal initialize:
"; Mammal M;
cout<<endl<<"Whales initialize:\
n"; Whales BlueWhale;
return 0;
}
PRACTICAL 21.
Write a C++ program to read and print employee information using multiple
inheritance.
#include<iostream>
using namespace std;
class EmpBasic {
public:
string EmpName,
Designation; int EmpId=0,
Salary;
};
class EmpPriv {
private:
string Contact, Performance;
public:
int Code = 1234;
void SetEmppriv(string c, string p)
{ Contact = c;
Performance = p;
}
void GetEmppriv(int c)
{ if(c == Code) {
cout<<"\tContact: "<<Contact<<endl;
cout<<"\tPerformance:
"<<Performance<<endl;
}
else {
cout<<"Invalid Code"<<endl
<<"Private Information Access Denied"<<endl;
}
}
};
class Employee : protected EmpBasic, protected EmpPriv { public:
void SetEmployeeInfo()
{ string c, p;
cout<<"Enter Employee Name: ";
getline(cin, EmpName);
cout<<"Enter Employee ID: ";
cin>>EmpId;
cin.ignore(1);
cout<<"Enter Employee Designation:
"; getline(cin, Designation);
cout<<"Enter Employee Salary: ";
cin>>Salary;
cin.ignore(1);
cout<<"Enter Employee Contact: ";
getline(cin, c);
cout<<"Enter Employee Performance: ";
getline(cin, p);
SetEmppriv(c, p);
}
void GetEmployeeInfo()
{ if(EmpId != 0) {
cout<<endl<<endl<<"The Data of Employee: "<<EmpId<<" is:
"<<endl;
cout<<"\tEmployee Name: "<<EmpName<<endl
<<"\tEmployee ID: "<<EmpId<<endl
<<"\tEmployee Designation: "<<Designation<<endl
<<"\tEmployee Salary: "<<Salary<<endl; cout<<"\
nEnter the Code required to get the private
information:
"; int code;
cin>>code;
cin.ignore(1);
GetEmppriv(code);
cout<<endl<<endl;
}
else {
cout<<"This Employee Does not exist in the database...";
}
}
};
int main() {
Employee emp1, emp2;
emp1.SetEmployeeInfo();
emp1.GetEmployeeInfo();
emp2.SetEmployeeInfo();
emp2.GetEmployeeInfo();
return 0;
}
Output: