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

Shcspraccs

The document is a certificate for Mr./Ms. Shezan Ali from Aga Khan Higher Secondary School, confirming completion of practical work in Computer Science for the academic year 2024-2025. It includes a list of practical assignments with objectives, dates, and source code examples for various C++ programming tasks, such as swapping numbers, calculating grades, and working with arrays. The document serves as a record of the student's practical skills and programming knowledge.

Uploaded by

northernshezan
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 views49 pages

Shcspraccs

The document is a certificate for Mr./Ms. Shezan Ali from Aga Khan Higher Secondary School, confirming completion of practical work in Computer Science for the academic year 2024-2025. It includes a list of practical assignments with objectives, dates, and source code examples for various C++ programming tasks, such as swapping numbers, calculating grades, and working with arrays. The document serves as a record of the student's practical skills and programming knowledge.

Uploaded by

northernshezan
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

1|Page

2|Page

AGA KHAN HIGHER SECONDARY SCHOOL,


Karachi

Computer Science Practicals XII

Certificate

It is certified that Mr./Ms. SHEZAN ALI of XII Computer Science

has carried out the necessary practical work as prescribed by the Aga

Khan University Examination Board (AKUEB) for the year

2024-2025

Incharge:

________________
Sir. Irfan Abdullah
Lecturer Computer Science
3|Page

Practical No. Date Objective Page No. Sign

01 Aug 22, Write a C++ program to swap 7


2024 two numbers without using the
third variables.
02 Aug 29, Write a C++ program which 8
2024 takes input from user and
shows whether a number is
positive,
negative or zero.

03 Sept 05, Write a program which takes 10


2024 input for 3 numbers and find
the greatest among them.

05 Write a program which takes 11


Sept 12, marks as an input and
2024 calculates the grades of
students based on
marks.
05 Sept 19, Write a program in C++ and
2024 show whether the input 13
character is vowel or consonant
using
switch case.
06 Sept 26, Write a C++ program to
2024 calculate the sum of the first 10 15
positive odd numbers.

07 Oct 03, Write the program to print the


2024 factorial of an input number. 16
4|Page

08 Oct 10, Generate the following pattern 17


2024 by using nested For loop.
**********
**********
**********
**********

09 Oct 10, Print the following format using


2024 nested for loop.

1 18
12
123
1234
12345
123456
1234567
10 Oct 17, Print the following output by
2024 using nested for loop.
20
*
***
*****
*******
*********

11 Oct 17, Print the following output by


2024 using nested for loop. 22

*
**
***
****
*****
5|Page

12 Oct 24, Write a C++ program which


2024 stores numeric values in a one-
dimensional array using for 24
loop
and finds the highest, lowest
and average values.
13 Write a program to make a
Oct 31, 2024 signup interference which takes
user name, password, retype-
password as an input and
compare the password and re- 26
enter password of user for sign
up,using strings library.

14 Nov 07, Write a program to add two


2024 matrices of order up to 4×4
using a two dimensional array 28
and
show the ordered output in third
matrix.
15 Nov 14, Write a program involving use of
2024 user defined function to calculate 32
volume of cylinder,sphere and
cube.

16 Nov 14, Write a program involving user


2024 defined function to calculate
average of given numbers. 35

17 Nov 21, Write a program involving a


2024 user defined function to check
whether the input number is a 37
prime number or not.
6|Page

18 Nov 28, Write a simple program using


2024 referencing and dereference
operator the find the value and
address of each element of an 39
array using pointers. (Hint: Use
for loop)
19 Jan 30, Write a C++ program
2025
in which a class uses 41
both public and private
access specifiers.
20 Feb 06, Write a C++ program to
2025 implement the concept
of multiple inheritance 43
in object oriented
programming.

21 Feb Write a C++ program to


13,2025 read and print employee
45
information using multiple
inheritance.
7|Page

PRACTICAL NUMBER 1
Write a C++ program to swap two numbers without using the third variables.
#include<iostream>
using namespace std;
int main()
{
int a = 5, b = 6;

a = a + b;
b = a - b;
a = a - b;
cout<<"The values of a and b after swapping is a: "<<a<< " b: "<<b;
}

OUTPUT
8|Page

PRACTICAL NUMBER 2
Write a C++ program which takes input from user and shows whether a number is
positive, negative or zero.

#include<iostream>
using namespace std;
int main()
{
int a;
cout << "Enter any number: ";
cin >> a;
if (a<0){
cout<<"number is negative";
}
else if (a>0) {
cout<<"number is positive";
}
else {
cout<<"number is zero";
}
return 0;
}

OUTPUT
9|Page
10 | P a g e

PRACTICAL NUMBER 3
Write a program which takes input for 3 numbers and find the greatest among
them.

Source code:

#include <iostream>
using namespace std;

int main() {
int num1, num2, num3;

cout << "Enter three numbers: ";


cin >> num1 >> num2 >> num3;

if (num1 >= num2 && num1 >= num3) {


cout << "The greatest number is: " << num1 << endl;
} else if (num2 >= num1 && num2 >= num3) {
cout << "The greatest number is: " << num2 << endl;
} else {
cout << "The greatest number is: " << num3 << endl;
}

return 0;
}
OUTPUT
11 | P a g e

PRACTICAL NUMBER 4
Write a program which takes marks as an input and calculates the grades of
students based on marks.

Source code:

#include <iostream>
using namespace std;

int main() {
int a;
cout << "Enter your marks: ";
cin >> a;

if (a < 0 || a > 100) {


cout << "Invalid";
} else if (a >= 80) {
cout << "A+";
} else if (a >= 70) {
cout << "A";
} else if (a >= 60) {
cout << "B";
} else if (a >= 50) {
cout << "C";
} else if (a >= 40) {
cout << "D";
} else {
cout << "You are failed";
}

return 0;
}
12 | P a g e

OUTPUT
13 | P a g e

PRACTICAL NUMBER 5
Write a program in C++ and show whether the input character is vowel or
consonant using switch case.
Source code:
#include <iostream>
using namespace std;

int main() {
char ch;
cout << "Enter an alphabet: ";
cin >> ch;

switch (ch) {
case 'A': case 'a':
case 'E': case 'e':
case 'I': case 'i':
case 'O': case 'o':
case 'U': case 'u':
cout << "Vowel" << endl;
break;
default:
cout << "Consonant" << endl;
break;
}

return 0;
}

OUTPUT
14 | P a g e
15 | P a g e

PRACTICAL NUMBER 6

Write a C++ program to calculate the sum of the first 10


positive odd numbers.

SOURCE CODE:

#include <iostream>
using namespace std;
int main()
{
int i, sum=0;
for (i=1 ;i<10; i+=2)
{
sum+=i;
}
cout<<"sum: "<<sum;
}

OUTPUT
16 | P a g e

PRACTICAL NUMBER 7
Write the program to print the factorial of an input number.

SOURCE CODE:

#include <iostream>
using namespace std;
int main()
{
int i, index, factorial;
cout<<"enter a number:";
cin>>index;
for(i=1; i<index; i++)
{

factorial=index*i;
}
cout<<"the value of factorial is "<<factorial<<endl;
return 0;
}

OUTPUT
17 | P a g e

PRACTICAL NUMBER 8
Generate the following pattern by using nested For loop.
**********
**********
**********
**********

SOURCE CODE:

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

for(int i=1; i<5; i++)


{
for(int j=1; j<=10; j++)

cout<<"*";
}
cout<<endl;
}
}

Output
18 | P a g e

PRACTICAL NUMBER 9

Print the following format using nested for loop.

1
12
123
1234
12345
123456
1234567

Source code:

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

for(int i=1; i<=7; i++)


{
for(int j=1; j<=i; j++)

cout<<j;
}
cout<<endl;
}
}
19 | P a g e

Output
20 | P a g e

PRACTICAL NUMBER 10

Print the following output by using nested for loop.

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

Source code:
#include<iostream>
using namespace std;
int main()
{

for(int i=1; i<=9; i+=2)


{
for(int j=1; j<=i; j++)

cout<<"*";
}
cout<<endl;
}
}
21 | P a g e

Output
22 | P a g e

PRACTICAL NUMBER 11
Print the following output by using nested for loop.

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

Source code:
#include<iostream>
using namespace std;
int main()
{

for(int i=1; i<=5; i++)


{
for(int j=1; j<=i; j++)

cout<<"*";
}
cout<<endl;
}
}
23 | P a g e

Output
24 | P a g e

PRACTICAL NUMBER 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.

Source code:
#include <iostream>
using namespace std;

int main() {
int a[10], max, low, sum = 0;
double average;

cout << "Enter number of elements (up to 10): ";


int n;
cin >> n;
cin>>a[0];

max =a[0];
low=a[0];
sum = a[0];
cout<<"now enter those elements:";
for (int i = 1; i < n; i++) {
cin >> a[i];
sum += a[i];

if (a[i] > max) {


max = a[i];
}

if (a[i] < low) {


low = a[i];
}
}

average = sum / (double)n;

cout << "The maximum value is: " << max << endl;
cout << "The lowest value is: " << low << endl;
25 | P a g e

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

return 0;
}

Output
26 | P a g e

Practical number 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.

Source code:

#include <iostream>
#include <cstring>
using namespace std;

int main() {

char validuser[] = "user123";


char validpass[] = "pass123";
char username[50];
char password[50];

cout << "Enter a username: ";


cin >> username;
cout << "Enter a password: ";
cin >> password;

if (strcmp(username, validuser) == 0) {

if (strcmp(password, validpass) == 0) {
cout << "You are signed in!" << endl;
} else {
27 | P a g e

cout << "Incorrect password." << endl;


}
} else {

cout << "Incorrect username." << endl;


}

return 0;
}
}

Output
28 | P a g e

Practical number 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.

Source 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;
29 | P a g e

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;
}
}
30 | P a g e

Output
31 | P a g e
32 | P a g e

Practical number 15
Write a program involving use of user defined function to calculate volume of
cylinder,sphere and cube.

Source code:
#include <iostream>
#include <cmath>

using namespace std;

double volumeOfCylinder(double radius, double height) {


return M_PI * radius * radius * height;
}

double volumeOfSphere(double radius) {


return (4.0 / 3.0) * M_PI * radius * radius * radius;
}

double volumeOfCube(double side) {


return side * side * side;
}

int main() {
int choice;
cout << "Choose the shape to calculate volume:" << endl;
cout << "1. Cylinder" << endl;
cout << "2. Sphere" << endl;
cout << "3. Cube" << endl;
cout << "Enter your choice (1/2/3): ";
cin >> choice;

if (choice == 1) {
double radius, height;
33 | P a g e

cout << "Enter radius of the cylinder: ";


cin >> radius;
cout << "Enter height of the cylinder: ";
cin >> height;
if (radius > 0 && height > 0) {
cout << "Volume of the cylinder: " << volumeOfCylinder(radius,
height) << endl;
} else {
cout << "Invalid input. Radius and height must be positive
numbers." << endl;
}
} else if (choice == 2) {
double radius;
cout << "Enter radius of the sphere: ";
cin >> radius;
if (radius > 0) {
cout << "Volume of the sphere: " << volumeOfSphere(radius) <<
endl;
} else {
cout << "Invalid input. Radius must be positive." << endl;
}
} else if (choice == 3) {
double side;
cout << "Enter side length of the cube: ";
cin >> side;
if (side > 0) {
cout << "Volume of the cube: " << volumeOfCube(side) << endl;
} else {
cout << "Invalid input. Side length must be positive." << endl;
}
} else {
cout << "Invalid choice!" << endl;
}

return 0;
34 | P a g e

Output
35 | P a g e

Practical number 16
Write a program involving user defined function to calculate average of
given numbers.

Source code:
#include <iostream>
using namespace std;

double calculateAverage(double sum, int count) {


return sum / count;
}

int main() {
int n;
double num, sum = 0;

cout << "How many numbers? ";


cin >> n;

cout << "Enter the numbers: ";


for (int i = 0; i < n; i++) {
cin >> num;
sum += num;
}

double average = calculateAverage(sum, n);

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

return 0;
}
36 | P a g e

36 | P a g e 3 6 | P a g e

Output
37 | P a g e

Practical number 17
Write a program involving a user defined function to check whether
the input number is a prime number or not.

Source code:

#include<iostream>
using namespace std;
void primeno();
int main()
{ primeno();
return 0;
}
void primeno()
{
int number, j, flag = 0;
cout << "Enter any integer and press enter to check: ";

cin >> number;


for(j = 2; j <= number/2; ++j)

{
if(number % j == 0)
{ flag = 1;
break;
}
}
if (flag == 1)
{ cout << number << " is not a prime number.";
}
else
{ cout << number << " is a prime number.";
}
}
38 | P a g e

output
39 | P a g e

Practical number 18
Write a simple program using referencing and dereference operator the find the
value and address of each element of an array using pointers. (Hint: Use for loop)

Source code:
#include <iostream>
using namespace std;
int main() {
int k1[5], i, j;
for (i=0;i<5;i++)
{
cout << "Enter " << i+1 << " element: "; cin >> k1[i];
}
int *ptr; ptr = k1;
for (i=0;i<5;i++) {
cout << "The address of element " << i+1 << " is: " << ptr;
cout << "\nThe value of element " << i+1 << " is: " << *ptr;
ptr++;
cout << endl; }
}
40 | P a g e

Output
41 | P a g e

Practical number 19
Write a C++ program in which a class uses both public
and private access specifiers.

Source code:
#include <iostream>
using namespace std;

class Rect {
private:
int len, bred;
public:
Rect(int x, int y) {
len = x;
bred = y;
}

// Function to calculate area


int area() {
return len * bred;
}
};

int main() {
Rect b1(3, 4), b2(2, 3);

cout << "Area of box 1: " << b1.area() << endl;


cout << "Area of box 2: " << b2.area() << endl;

return 0;
}
42 | P a g e

Output
43 | P a g e

Practical number 20

Write a C++ program to implement the concept of multiple


inheritance in object oriented programming.

Source code:
#include <iostream>
using namespace std;

class Vehicle {
public:
Vehicle() {
cout << "I am a vehicle!" << endl;
}
};

class Car: public Vehicle {


public:
Car() {
cout << "I am a car!" << endl;
}
};

class Bus: public Vehicle {


public:
Bus() {
cout << "I am a bus!" << endl;
}
};

int main() {
Car mustang;
cout << endl;
Bus doubledecker;
44 | P a g e

return 0;
}

Output
45 | P a g e

Practical number 21

Write a C++ program to read and print employee


information using multiple inheritance.

Source code:
#include <iostream>
using namespace std;

class basicinfo {
protected:
char name[30];
int emp_id;
char gender[10];

public:
void getbasic_info() {
cout << "Enter name: ";
cin.ignore();
cin.get(name, 30);

cout << "Enter employee ID: ";


cin >> emp_id;
46 | P a g e

cout << "Enter gender: ";


cin.ignore();
cin.get(gender, 10);
}
};

class deptinfo {
protected:
char dept_name[30];
char assigned_work[30];
int time2complete;

public:
void Get_deptinfo() {
cout << "Enter department name: ";
cin.ignore();
cin.get(dept_name, 30);

cout << "Enter assigned work: ";


cin.ignore();
cin.get(assigned_work, 30);

cout << "Enter time in hours to complete work: ";


cin >> time2complete;
}
};

class employee : public basicinfo, public deptinfo {


public:
47 | P a g e

void getemployeeinfo() {
cout << "\nEnter employee's basic info:" << endl;
getbasic_info();

cout << "\nEnter employee's department info:" << endl;


Get_deptinfo();
}

void printemployeeinfo() {
cout << "\nEmployee's information is..." << endl;
cout << "Basic Information: " << endl;
cout << "Name: " << name << endl;
cout << "Employee ID: " << emp_id << endl;
cout << "Gender: " << gender << endl;

cout << "Department info: " << endl;


cout << "Department Name: " << dept_name << endl;
cout << "Assigned work: " << assigned_work << endl;
cout << "Time to complete work: " << time2complete << "
hours" << endl;
}
};

int main() {
employee emp;
emp.getemployeeinfo();
emp.printemployeeinfo();
return 0;
}
48 | P a g e

output
49 | P a g e

You might also like