Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
22 views20 pages

Computer Portfolio XII

The document contains a series of C++ programming exercises demonstrating various programming concepts such as conditional statements (if, if-else, else-if), loops (for, while, do-while), arrays, matrix operations, string functions, user-defined functions for arithmetic operations, area and volume calculations, factorial, average, pointers, and classes. Each practical includes code snippets and expected outputs. The exercises are designed to help learners understand and apply fundamental programming principles in C++.

Uploaded by

Fidget Spiner
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)
22 views20 pages

Computer Portfolio XII

The document contains a series of C++ programming exercises demonstrating various programming concepts such as conditional statements (if, if-else, else-if), loops (for, while, do-while), arrays, matrix operations, string functions, user-defined functions for arithmetic operations, area and volume calculations, factorial, average, pointers, and classes. Each practical includes code snippets and expected outputs. The exercises are designed to help learners understand and apply fundamental programming principles in C++.

Uploaded by

Fidget Spiner
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/ 20

Practical 1: 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 << endl << "The number is positive.";

Output:

2. If – else statement

#include<iostream>

using namespace std;

int main()

int number;

cout << "Enter a number: ";

cin >> number;

if(number % 2 == 0)

cout << endl << "The number is even.";

else

cout << endl << "The number is odd.";

}
Output:

3. else- if statement

#include<iostream>

using namespace std;

int main()

int number;

cout << "Enter a number: ";

cin >> number;

if(number > 0)

cout << endl << "The number is positive.";

else if(number < 0)

cout << endl << "The number is negative.";

else

cout << endl << "The number is zero.";

Output:
Practical 2: Write a program using nested if statement.

#include<iostream>

using namespace std;

int main()

int number;

cout << "Enter a number: ";

cin >> number;

if(number > 0)

cout << endl << "The number is positive.";

if(number % 2 == 0)

cout << endl << "The number is even.";

else

cout << endl << "The number is odd.";

else if(number < 0)

cout << endl << "The number is negative.";

else

cout << endl << "The number is zero.";

Output:
Practical 3: Write a program using switch statement.

#include<iostream>

using namespace std;

int main(){

int day;

cout << "Enter a number (1 to 7) to represent the day of the week: ";

cin >> day;

switch (day){

case 1:

cout << endl << "Monday"; break;

case 2:

cout << endl << "Tuesday"; break;

case 3:

cout << endl << "Wednesday"; break;

case 4:

cout << endl <<"Thursday"; break;

case 5:

cout << endl <<"Friday"; break;

case 6:

cout << endl <<"Saturday"; break;

case 7:

cout << endl <<"Sunday"; break;

default:

cout << endl <<"Invalid input!"; break; } }

Output:
Practical 4: Write a C++ program that uses for loop.

#include<iostream>

using namespace std;

int main()

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

cout << i << " ";

Output:

Practical 5: Write a C++ program that uses while loop.

#include<iostream>

using namespace std;

int main() {

int i = 1;

while(i <= 10)

cout << i << " ";

i++;

Output:
Practical 6: Write a C++ program that uses do while loop.

#include<iostream>

using namespace std;

int main()

int i = 1;

do

cout << i << " ";

i++;

}while (i <= 10);

Output:

Practical 7: 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 n;

cout << "Enter the number of elements: ";

cin >> n;

int arr[n];

cout << "Enter " << n << " numeric values:" << endl;

for (int i = 0; i < n; i++)

cin >> arr[i];


int highest = arr[0];

int lowest = arr[0];

int sum = 0;

for (int i = 0; i < n; i++)

if (arr[i] > highest)

highest = arr[i];

if (arr[i] < lowest)

lowest = arr[i];

sum += arr[i];

float average = static_cast<float>(sum) / n;

cout << endl << "Highest value: " << highest << endl;

cout << "Lowest value: " << lowest << endl;

cout << "Average value: " << average;

Output:
Practical 8: Write a C++ program for adding/ subtracting/ multiplying two integer matrices of the
order up to 4x4.

#include<iostream>

using namespace std;

const int MAX_SIZE = 4;

void inputMatrix(int mat[MAX_SIZE][MAX_SIZE], int rows, int cols)

for (int i = 0; i < rows; i++)

for (int j = 0; j < cols; j++)

cin >> mat[i][j];

void displayMatrix(int mat[MAX_SIZE][MAX_SIZE], int rows, int cols){

for (int i = 0; i < rows; i++){

for (int j = 0; j < cols; j++)

cout << mat[i][j] << " ";

cout << endl;

int main(){

int mat1[MAX_SIZE][MAX_SIZE], mat2[MAX_SIZE][MAX_SIZE], result[MAX_SIZE]


[MAX_SIZE];

int rows, cols;

char op;

cout << "Enter rows and columns (up to 4): ";

cin >> rows >> cols;

cout << "Enter matrix 1 (" << rows << "x" << cols << "):" << endl;

inputMatrix(mat1, rows, cols);

cout << "Enter matrix 2 (" << rows << "x" << cols << "):" << endl;

inputMatrix(mat2, rows, cols);


cout << "Choose operation (+, -, *): ";

cin >> op;

for (int i = 0; i < rows; i++){

for (int j = 0; j < cols; j++){

if (op == '+')

result[i][j] = mat1[i][j] + mat2[i][j];

else if (op == '-')

result[i][j] = mat1[i][j] - mat2[i][j];

else if (op == '*'){

result[i][j] = 0;

for (int k = 0; k < cols; k++)

result[i][j] += mat1[i][k] * mat2[k][j];

cout << "Result:" << endl;

displayMatrix(result, rows, cols);

Output:
Practical 9: Write C++ program to perform various operations on string using string functions. strcpy,
strcat, strlen and strcmp.

#include<iostream>

#include<cstring>

using namespace std;

int main()

char str1[100], str2[100], result[200];

cout << "Enter first string: ";

cin.getline(str1, 100);

cout << "Enter second string: ";

cin.getline(str2, 100);

strcpy(result, str1);

cout << "After strcpy: " << result << endl;

strcat(result, str2);

cout << "After strcat: " << result << endl;

cout << "Length of result: " << strlen(result) << endl;

int cmp = strcmp(str1, str2);

cout << "Comparison: " << (cmp == 0 ? "Equal" : cmp < 0 ? "str1 < str2" : "str1 > str2");

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

#include<iostream>

using namespace std;

double add(double a, double b){

return a + b;

double subtract(double a, double b) {

return a - b;

double multiply(double a, double b) {

return a * b;

double divide(double a, double b) {

return b != 0 ? a / b : 0;

int main()

double num1, num2;

char op;

cout << "Enter two numbers and operation (+, -, *, /): ";

cin >> num1 >> num2 >> op;

switch(op){

case '+':

cout << "Result: " << add(num1, num2); break;

case '-':

cout << "Result: " << subtract(num1, num2); break;

case '*':

cout << "Result: " << multiply(num1, num2); break;


case '/':

if (num2 == 0)

cout << "Error: Division by zero!";

else

cout << "Result: " << divide(num1, num2); break;

default:

cout << "Invalid operation!";

Output:

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

#include<iostream>

#include<cmath>

using namespace std;

double areaCircle(double r){

return M_PI * r * r;

double areaTriangle(double b, double h){

return 0.5 * b * h;

double areaParallelogram(double b, double h){

return b * h;

int main()

{
int choice;

double b, h, r;

cout << "Choose shape:\n1. Circle\n2. Triangle\n3. Parallelogram\nEnter choice (1/2/3): ";

cin >> choice;

switch (choice)

case 1: cout << "Enter radius: "; cin >> r; cout << "Area: " << areaCircle(r); break;

case 2: cout << "Enter base & height: "; cin >> b >> h; cout << "Area: " << areaTriangle(b, h); break;

case 3: cout << "Enter base & height: "; cin >> b >> h; cout << "Area: " << areaParallelogram(b, h);

break;

default: cout << "Invalid choice!";

Output:

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

#include<iostream>

#include<cmath>

using namespace std;

double volumeCylinder(double radius, double height);

double volumeSphere(double radius);

double volumeCube(double side);

int main() {
int choice;

double radius, height, side;

cout << "Choose a shape to calculate volume:\n";

cout << "1. Cylinder\n2. Sphere\n3. Cube\n";

cout << "Enter your choice (1/2/3): ";

cin >> choice;

switch (choice){

case 1:

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

cin >> radius >> height;

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

break;

case 2:

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

cin >> radius;

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

break;

case 3:

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

cin >> side;

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

break;

default:

cout << "Invalid choice!" << endl;

return 0;

double volumeCylinder(double radius, double height){

return M_PI * pow(radius, 2) * height;


}

double volumeSphere(double radius){

return (4.0 / 3.0) * M_PI * pow(radius, 3);

double volumeCube(double side){

return pow(side, 3);

Output:

Practical 13: Write a program involving user defined function to calculate factorial of a given number.

#include<iostream>

using namespace std;

unsigned long long factorial(int n){

if (n == 0 || n == 1) return 1;

return n * factorial(n - 1);

int main(){

int num;

cout << "Enter a number to calculate its factorial: ";

cin >> num;

if (num < 0)

cout << "Factorial is not defined for negative numbers.";

else
cout << "Factorial of " << num << " is: " << factorial(num);

Output:

Practical 14: Write a program involving user defined function to calculate average of numbers.

#include<iostream>

using namespace std;

double calculateAverage(int numbers[], int size){

double sum = 0;

for (int i = 0; i < size; i++)

sum += numbers[i];

return sum / size;}

int main(){

int size;

cout << "Enter the number of elements: ";

cin >> size;

int numbers[size];

cout << "Enter " << size << " numbers: ";

for (int i = 0; i < size; i++)

cin >> numbers[i];

cout << "Average: " << calculateAverage(numbers, size);

Output:
Practical 15: Write a simple program using & to return memory address of a variable and storing it in a
pointer variable.

#include<iostream>

using namespace std;

int main()

int num = 57;

int* ptr;

ptr = &num;

cout << "Value of num: " << num << endl;

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

cout << "Value stored in ptr: " << ptr << endl;

cout << "Value pointed to by ptr: " << *ptr;

Output:

Practical 16: : Write a C++ program that uses pointer variable.

#include<iostream>

using namespace std;

int main(){

int number;

int* pointer;

cout << "Enter an integer: ";

cin >> number;

pointer = &number;

cout << "Original value: " << number << endl;


cout << "Memory address of number: " << &number << endl;

cout << "Pointer variable stores the address: " << pointer << endl;

*pointer += 10;

cout << "New value: " << number << endl;

Output:

Practical 17: Write a C++ program to declare a class along with data members and member functions
in its body and create the objects of class in the main( ) function and call member functions of class
with the help of objects.

#include<iostream>

using namespace std;

class Student

private:

string name;

int age;

public:

void setData(string n, int a)

name = n;

age = a;

void displayData()

{
cout << "Name: " << name << endl;

cout << "Age: " << age << endl;

};

int main()

Student student1, student2;

student1.setData("Ammar", 17);

student2.setData("Ahmer", 19);

cout << "Student 1 Details:" << endl;

student1.displayData();

cout << "\nStudent 2 Details:" << endl;

student2.displayData();

Output:

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

#include<iostream>

using namespace std;

class Employee

private:

int salary;
public:

string name;

void setSalary(int s)

salary = s;

void displayDetails()

cout << "Name: " << name << endl;

cout << "Salary: " << salary << endl;

};

int main()

Employee emp;

emp.name = "Mushtak Abro";

emp.setSalary(100000);

cout << "Employee Details:" << endl;

emp.displayDetails();

Output:

You might also like