Shcspraccs
Shcspraccs
2|Page
Certificate
has carried out the necessary practical work as prescribed by the Aga
2024-2025
Incharge:
________________
Sir. Irfan Abdullah
Lecturer Computer Science
3|Page
1 18
12
123
1234
12345
123456
1234567
10 Oct 17, Print the following output by
2024 using nested for loop.
20
*
***
*****
*******
*********
*
**
***
****
*****
5|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;
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;
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
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()
{
cout<<"*";
}
cout<<endl;
}
}
Output
18 | P a g e
PRACTICAL NUMBER 9
1
12
123
1234
12345
123456
1234567
Source code:
#include<iostream>
using namespace std;
int main()
{
cout<<j;
}
cout<<endl;
}
}
19 | P a g e
Output
20 | P a g e
PRACTICAL NUMBER 10
*
***
*****
*******
*********
Source code:
#include<iostream>
using namespace std;
int main()
{
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()
{
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;
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];
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() {
if (strcmp(username, validuser) == 0) {
if (strcmp(password, validpass) == 0) {
cout << "You are signed in!" << endl;
} else {
27 | P a g e
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>
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
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;
int main() {
int n;
double num, sum = 0;
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: ";
{
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;
}
int main() {
Rect b1(3, 4), b2(2, 3);
return 0;
}
42 | P a g e
Output
43 | P a g e
Practical number 20
Source code:
#include <iostream>
using namespace std;
class Vehicle {
public:
Vehicle() {
cout << "I am a vehicle!" << 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
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);
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);
void getemployeeinfo() {
cout << "\nEnter employee's basic info:" << endl;
getbasic_info();
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;
int main() {
employee emp;
emp.getemployeeinfo();
emp.printemployeeinfo();
return 0;
}
48 | P a g e
output
49 | P a g e