0% found this document useful (0 votes)
85 views

C++ Basics Code

The document contains summaries of 14 programming practical exercises involving basic C++ concepts like functions, loops, arrays, and matrices. The practicals cover writing programs that use conditionals like if/else, switch statements, different loops (for, while, do-while), defining and calling functions, passing arrays to functions, and performing operations on matrices and strings. Sample programs and outputs are provided for each practical.

Uploaded by

zeusbaqi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

C++ Basics Code

The document contains summaries of 14 programming practical exercises involving basic C++ concepts like functions, loops, arrays, and matrices. The practicals cover writing programs that use conditionals like if/else, switch statements, different loops (for, while, do-while), defining and calling functions, passing arrays to functions, and performing operations on matrices and strings. Sample programs and outputs are provided for each practical.

Uploaded by

zeusbaqi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Portfolio

Computer science
HSSC II
ATTA UL BAQI MANSOOR
CP2
3367

1
Practical no1: Write a program for each if, if-else and
else-if.
1. If (statement)
#include<iostream>

using namespace std;

int main() {
int number;

cout << "Enter a number: ";


cin >> number;

if (number > 0) {
cout << "The number is positive." << endl;
}

return 0;
}

Output:

2
2. If-else (statement)
#include<iostream>

using namespace std;

int main() {
int number;

cout << "Enter a number: ";


cin >> number;

if (number > 0) {
cout << "The number is positive." << endl;
} else {
cout << "The number is non-positive." << endl;
}

return 0;
}

3
Output:

4
3. Else-if(statement)
#include<iostream>
using namespace std;

int main() {
int number;

cout << "Enter a number: ";


cin >> number;

if (number > 0) {
cout << "The number is positive." << endl;
} else if (number < 0) {
cout << "The number is negative." << endl;
} else {
cout << "The number is zero." << endl;
}

return 0;
}

Output:

5
Practical no2: Write a program using nested if
statement.
#include<iostream>
using namespace std;
int main() {
int age;
char gender;

cout << "Enter your age: ";


cin >> age;

if (age >= 18) {


cout << "Are you male or female? (m/f): ";
cin >> gender;

if (gender == 'm') {
cout << "You are an adult male." << endl;
} else if (gender == 'f') {
cout << "You are an adult female." << endl;
} else {
cout << "Invalid gender entry." << endl;
}
} else {
cout << "You are a minor." << endl;
}
return 0;

6
}

Output:

Practical no3: Write a program using switch


statement.
#include<iostream>

using namespace std;

int main() {
char grade;

cout << "Enter your grade (A, B, C, D, or F): ";


cin >> grade;
7
switch (grade) {
case 'A':
cout << "Excellent! You got an A." << endl;
break;
case 'B':
cout << "Good job! You got a B." << endl;
break;
case 'C':
cout << "Not bad. You got a C." << endl;
break;
case 'D':
cout << "You passed, but aim higher. You got a D." << endl;
break;
case 'F':
cout << "Oops! You failed. You got an F." << endl;
break;
default:
cout << "Invalid grade entered." << endl;
}

return 0;
}

Output:

8
Practical no4: Write a C++ program that uses for loop.
#include<iostream>
using namespace std;
int main() {
cout << "Counting from 1 to 5 using a for loop:" << endl;

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


cout << i << endl;
}

9
return 0;
}

Output:

Practical no5: Write a C++ program that uses while


loop.
#include<iostream>
using namespace std;
int main() {
cout << "Counting from 1 to 5 using a while loop:" << endl;

int i = 1; // initialization

while (i <= 5) { // condition


cout << i << endl;

10
++i; // increment
}

return 0;
}

Output:

Practical no6: Write a C++ program that uses do while


loop.
#include<iostream>
using namespace std;
int main() {
cout << "Counting from 1 to 5 using a do-while loop:" << endl;

int i = 1; // initialization

do {
cout << i << endl;

11
++i; // increment
} while (i <= 5); // condition

return 0;

Output:

Practical no7: Write a C++ program which stores


numeric values in a one dimensional array using for
loop and finds the highest, lowest and average values.

12
Output:

13
14
Practical no8: Write a C++ program for adding/
subtracting/ multiplying two integer matrices of the
order up to 4x4.
 #include <iostream>
 using namespace std;

 int main()
 {
 int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;

 cout << "Enter rows and columns for first matrix: ";
 cin >> r1 >> c1;
 cout << "Enter rows and columns for second matrix: ";
 cin >> r2 >> c2;


 while (c1!=r2)
 {
 cout << "Error! column of first matrix not equal to row of second.";

 cout << "Enter rows and columns for first matrix: ";

 cin >> r1 >> c1;

 cout << "Enter rows and columns for second matrix: ";
 cin >> r2 >> c2;
 }


 cout << endl << "Enter elements of matrix 1:" << endl;
 for(i = 0; i < r1; ++i)
 for(j = 0; j < c1; ++j)
 {
 cout << "Enter element a" << i + 1 << j + 1 << " : ";
 cin >> a[i][j];
 }

15
 cout << endl << "Enter elements of matrix 2:" << endl;
 for(i = 0; i < r2; ++i)
 for(j = 0; j < c2; ++j)
 {
 cout << "Enter element b" << i + 1 << j + 1 << " : ";
 cin >> b[i][j];
 }


 for(i = 0; i < r1; ++i)
 for(j = 0; j < c2; ++j)
 {
 mult[i][j]=0;
 }


 for(i = 0; i < r1; ++i)
 for(j = 0; j < c2; ++j)
 for(k = 0; k < c1; ++k)
 {
 mult[i][j] += a[i][k] * b[k][j];
 }


 cout << endl << "Output Matrix: " << endl;
 for(i = 0; i < r1; ++i)
 for(j = 0; j < c2; ++j)
 {
 cout << " " << mult[i][j];
 if(j == c2-1)
 cout << endl;
 }

 return 0;
 }

16
 OUTPUT

17
Practical no9: Write C++ program to perform various
operations on string using string fucntions , i.e. strcpy,
strcat, strlen and strcmp.

18
Output :

19
Practical no10: Write a program involving user
defined function to perform basic arithmetic
operations, i.e. add, subtract, multiply and divide.

20
21
Output :

22
Practical no11: Write a program involving user
defined function to calculate area of circle, triangle
and parallelogram

23
24
25
Output (circle):

Output (triangle):
26
Output (parallelogram):

27
28
Practical no12: Write a program involving use of user
defined function to calculate volume of cylinder,
sphere and cube.
 #include <iostream>
 using namespace std;

 // Function to calculate the volume of a cylinder


 double cylinderVolume(double radius, double height) {
 const double pi = 3.14159;
 return pi * radius * radius * height;
 }

 // Function to calculate the volume of a sphere


 double sphereVolume(double radius) {
 const double pi = 3.14159;
 return (4.0 / 3.0) * pi * radius * radius * radius;
 }

 // Function to calculate the volume of a cube


 double cubeVolume(double side) {
 return side * side * side;
 }

 int main() {
 double radius, height, side;

 cout << "Enter the radius and height of the cylinder: ";
 cin >> radius >> height;

 cout << "Volume of the cylinder: " << cylinderVolume(radius, height) << endl;

 cout << "Enter the radius of the sphere: ";


29
 cin >> radius;

 cout << "Volume of the sphere: " << sphereVolume(radius) << endl;

 cout << "Enter the side of the cube: ";


 cin >> side;

 cout << "Volume of the cube: " << cubeVolume(side) << endl;

 return 0; }

 OUTPUT

30
Practical # 13: Write a program involving user defined
function to calculate factorial of a given number.
 #include <iostream>
 using namespace std;

 // Function to calculate the factorial of a number


 unsigned long long factorial(int num) {
 if (num == 0 || num == 1) {
 return 1; // Factorial of 0 and 1 is 1
 } else {
 unsigned long long fact = 1;
 for (int i = 2; i <= num; i++) {
o fact *= i;
 }
 return fact;
 }
 }

 int main() {
 int number;

 cout << "Enter a number to find its factorial: ";


 cin >> number;

 if (number < 0) {
 cout << "Factorial is not defined for negative numbers." << endl;
 } else {
 unsigned long long result = factorial(number);
 cout << "Factorial of " << number << " is: " << result << endl;
 }

 return 0; }
 OUTPUT
31
32
PRACTICAL #14 Write a program involving user
defined function to calculate average of numbers.
 #include <iostream>
 using namespace std;

 // Function to calculate the average of numbers
 double calculateAverage(int arr[], int size) {
 if (size == 0) {
 return 0; // Avoid division by zero
 }

 int sum = 0;
 for (int i = 0; i < size; i++) {
 sum += arr[i];
 }

 return static_cast<double>(sum) / size;
 }

 int main() {
 int numCount;

 cout << "Enter the number of elements: ";
 cin >> numCount;

 if (numCount <= 0) {
 cout << "Please enter a valid count of numbers." << endl;
 return 1;
 }

 int numbers[numCount];

 cout << "Enter the numbers: ";
 for (int i = 0; i < numCount; i++) {
 cin >> numbers[i];
 }

 double average = calculateAverage(numbers, numCount);
 cout << "Average of the numbers is: " << average << endl;
33

 return 0;
 }

PRACTICAL # 15 Write a simple program using & to


return memory address of a variable and storing it in
a pointer variable.
34
 Input:
#include <iostream>
#include<conio.h>

using namespace std;

int main() {

// Declare Variables
int a;
int *pt;

a = 10;
pt = &a;

cout << "\n[a ]:Value of A = " << a;


cout << "\n[*pt]:Value of A = " << *pt;
cout << "\n[&a ]:Address of A = " << &a;
cout << "\n[pt ]:Address of A = " << pt;
cout << "\n[&pt]:Address of pt = " << &pt;
cout << "\n[pt ]:Value of pt = " << pt;

getch();
return 0;
}
35
 OUTPUT:

PRACTICAL#16 Write a C++ program that uses


pointer variable.
 INPUT:
36
#include <iostream>
#include <conio.h>
using namespace std;

int main() {

// declare variables
int var1 = 3;
int var2 = 24;
int var3 = 17;

// print address of var1


cout<<"Address of var1: "<< &var1<< endl;

// print address of var2


cout<<"Address of var2: "<< &var2<< endl;

// print address of var3


cout<<"Address of var3: "<< &var3<< endl;
getch();
}

 Output:

37

You might also like