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

CppAssignment

Uploaded by

Wycliffe Anshul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CppAssignment

Uploaded by

Wycliffe Anshul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

1 Addition of Two Numbers Using Float Data Type

#include <iostream>
using namespace std;

int main() {
float num1, num2, sum;

cout << "Enter first number: ";


cin >> num1;

cout << "Enter second number: ";


cin >> num2;

sum = num1 + num2;

cout << "Sum of " << num1 << " and " << num2 << " is: " << sum << endl;

return 0;
}

// Output
Enter first number: 3.5
Enter second number: 7.8
Sum of 3.5 and 7.8 is: 11.3

CPP Practical Assignment 1


2 Finding the Biggest Number Between Two Numbers

#include <iostream>
using namespace std;

int main() {
float num1, num2;

cout << "Enter first number: ";


cin >> num1;

cout << "Enter second number: ";


cin >> num2;

if(num1 > num2) {


cout << num1 << " is greater than " << num2 << endl;
}
else if(num2 > num1) {
cout << num2 << " is greater than " << num1 << endl;
}
else {
cout << "Both numbers are equal" << endl;
}

return 0;
}

// Output
Enter first number: 45.6
Enter second number: 23.1
45.6 is greater than 23.1

CPP Practical Assignment 2


3 Finding Factorial Using do-while Loop

#include <iostream>
using namespace std;

int main() {
int number;
long long factorial = 1;
int i = 1;

cout << "Enter a positive integer: ";


cin >> number;

// Handle negative numbers


if(number < 0) {
cout << "Error! Factorial of a negative number doesn't exist.";
}
else {
do {
factorial *= i;
i++;
} while(i <= number);

cout << "Factorial of " << number << " = " << factorial << endl;
}

return 0;
}

// Output
Enter a positive integer: 5
Factorial of 5 = 120

CPP Practical Assignment 3


4 Program for Arithmetic Operations Using Switch Case

#include <iostream>
using namespace std;

int main() {
float num1, num2, result;
char operation;

cout << "Enter first number: ";


cin >> num1;
cout << "Enter second number: ";
cin >> num2;

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


cin >> operation;

switch(operation) {
case '+':
result = num1 + num2;
cout << "Result: " << num1 << " + " << num2 << " = " << result;
break;
case '-':
result = num1 - num2;
cout << "Result: " << num1 << " - " << num2 << " = " << result;
break;
case '*':
result = num1 * num2;
cout << "Result: " << num1 << " * " << num2 << " = " << result;
break;
case '/':
if(num2 != 0) {
result = num1 / num2;
cout << "Result: " << num1 << " / " << num2 << " = ";
cout << result;

} else {
cout << "Error: Division by zero!";
}
break;
default:
cout << "Invalid operation!";
}

return 0;
}

CPP Practical Assignment 4


// Output
Enter first number: 10
Enter second number: 5
Choose an operation (+, -, *, /): +
Result: 10 + 5 = 15

// Another run
Enter first number: 20
Enter second number: 4
Choose an operation (+, -, *, /): /
Result: 20 / 4 = 5

CPP Practical Assignment 5


5 Program for Multiplication of Two 3x3 Matrices

#include <iostream>
using namespace std;

int main() {
int A[3][3], B[3][3], C[3][3];

cout << "Enter elements of first 3x3 matrix:\n";


for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
cout << "Enter element A[" << i << "][" << j << "]: ";
cin >> A[i][j];
}
}

cout << "\nEnter elements of second 3x3 matrix:\n";


for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
cout << "Enter element B[" << i << "][" << j << "]: ";
cin >> B[i][j];
}
}

// Initializing result matrix C with zeros


for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
C[i][j] = 0;
}
}

// Matrix multiplication
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
for(int k = 0; k < 3; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}

cout << "\nResultant matrix after multiplication:\n";


for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
cout << C[i][j] << "\t";
}
cout << endl;
}

return 0;
}

CPP Practical Assignment 6


// Output
Enter elements of first 3x3 matrix:
Enter element A[0][0]: 1
Enter element A[0][1]: 2
Enter element A[0][2]: 3
Enter element A[1][0]: 4
Enter element A[1][1]: 5
Enter element A[1][2]: 6
Enter element A[2][0]: 7
Enter element A[2][1]: 8
Enter element A[2][2]: 9

Enter elements of second 3x3 matrix:


Enter element B[0][0]: 9
Enter element B[0][1]: 8
Enter element B[0][2]: 7
Enter element B[1][0]: 6
Enter element B[1][1]: 5
Enter element B[1][2]: 4
Enter element B[2][0]: 3
Enter element B[2][1]: 2
Enter element B[2][2]: 1

Resultant matrix after multiplication:


30 24 18
84 69 54
138 114 90

CPP Practical Assignment 7


6 Program to Store Five Books of Information Using Struct.

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

struct Book {
string title;
string author;
int year;
float price;
string publisher;
};
int main() {
const int NUM_BOOKS = 5;
Book books[NUM_BOOKS];

// Input information for each book


for(int i = 0; i < NUM_BOOKS; i++) {
cout << "\nEnter details for Book " << (i+1) << ":" << endl;

cout << "Title: ";


cin.ignore(i == 0 ? 0 : 1); // Clear input buffer
getline(cin, books[i].title);

cout << "Author: ";


getline(cin, books[i].author);

cout << "Publication Year: ";


cin >> books[i].year;

cout << "Price: $";


cin >> books[i].price;

cout << "Publisher: ";


cin.ignore();
getline(cin, books[i].publisher);
}

// Display all book information


cout << "\n\n===== BOOK INFORMATION =====\n";
for(int i = 0; i < NUM_BOOKS; i++) {
cout << "\nBook " << (i+1) << ":" << endl;
cout << "Title: " << books[i].title << endl;
cout << "Author: " << books[i].author << endl;
cout << "Publication Year: " << books[i].year << endl;
cout << "Price: $" << books[i].price << endl;
cout << "Publisher: " << books[i].publisher << endl;
cout << "----------------------------" << endl;
}
return 0;
}

CPP Practical Assignment 8


// Output ===== BOOK INFORMATION =====

Enter details for Book 1: Book 1:


Title: C++ Programming Title: C++ Programming
Author: Bjarne Stroustrup Author: Bjarne Stroustrup
Publication Year: 2000 Publication Year: 2000
Price: $45.99 Price: $45.99
Publisher: Addison-Wesley Publisher: Addison-Wesley
----------------------------
Enter details for Book 2:
Title: Data Structures Book 2:
Author: Robert Lafore Title: Data Structures
Publication Year: 1998 Author: Robert Lafore
Price: $39.50 Publication Year: 1998
Publisher: Pearson Price: $39.50
Publisher: Pearson
Enter details for Book 3: ----------------------------
Title: Algorithms
Author: Thomas Cormen Book 3:
Publication Year: 2009 Title: Algorithms
Price: $58.25 Author: Thomas Cormen
Publisher: MIT Press Publication Year: 2009
Price: $58.25
Enter details for Book 4: Publisher: MIT Press
Title: Clean Code ----------------------------
Author: Robert Martin
Publication Year: 2008 Book 4:
Price: $42.75 Title: Clean Code
Publisher: Prentice Hall Author: Robert Martin
Publication Year: 2008
Enter details for Book 5: Price: $42.75
Title: Design Patterns Publisher: Prentice Hall
Author: Erich Gamma ----------------------------
Publication Year: 1994
Price: $49.99 Book 5:
Publisher: Addison-Wesley Title: Design Patterns
Author: Erich Gamma
Publication Year: 1994
Price: $49.99
Publisher: Addison-Wesley
----------------------------

CPP Practical Assignment 9


7 Program to Store Six Employee Information Using Union

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

union EmployeeID {
int num_id;
char text_id[10];
};
struct Employee {
string name;
EmployeeID id;
bool is_num_id; // true for numeric ID, false for text ID
};
int main() {
Employee emp[6];
// Input information for each employee
for(int i = 0; i < 6; i++) {
cout << "\nEmployee " << (i+1) << ":" << endl;

cout << "Name: ";


cin.ignore(i == 0 ? 0 : 1);
getline(cin, emp[i].name);

cout << "ID type (1=numeric, 0=text): ";


cin >> emp[i].is_num_id;

if(emp[i].is_num_id) {
cout << "Numeric ID: ";
cin >> emp[i].id.num_id;
} else {
cout << "Text ID: ";
cin >> emp[i].id.text_id;
}
}

// Display all employee information


cout << "\n===== EMPLOYEES =====\n";
for(int i = 0; i < 6; i++) {
cout << "\nEmployee " << (i+1) << ":" << endl;
cout << "Name: " << emp[i].name << endl;

if(emp[i].is_num_id) {
cout << "ID: " << emp[i].id.num_id << endl;
} else {
cout << "ID: " << emp[i].id.text_id << endl;
}
}

return 0;
}

CPP Practical Assignment 10


// Output
Employee 1:
Name: John
ID type (1=numeric, 0=text): 1
Numeric ID: 101

Employee 2:
Name: Sara
ID type (1=numeric, 0=text): 0
Text ID: ABC123

[Input continues for 4 more employees...]

===== EMPLOYEES =====

Employee 1:
Name: John
ID: 101

Employee 2:
Name: Sara
ID: ABC123

[Output continues for 4 more employees...]

CPP Practical Assignment 11


8 Simple Interest Using Call by Value and Reference

#include <iostream>
using namespace std;

// Function to calculate simple interest using call by value


float calculateInterestByValue(float principal, float rate, float time) {
return (principal * rate * time) / 100;
}

// Function to calculate simple interest using call by reference


void calculateInterestByReference(float principal, float rate, float time,
float &interest) {
interest = (principal * rate * time) / 100;
}

int main() {
float principal, rate, time, interestByValue, interestByReference;

cout << "Enter principal amount: Rs ";


cin >> principal;

cout << "Enter rate of interest (% per annum): ";


cin >> rate;

cout << "Enter time period (in years): ";


cin >> time;

// Calculate interest using call by value


interestByValue = calculateInterestByValue(principal, rate, time);

// Calculate interest using call by reference


calculateInterestByReference(principal, rate, time,
interestByReference);

cout << "\nResults:" << endl;


cout << "Simple Interest (by value): Rs " << interestByValue << endl;
cout << "Simple Interest (by reference): Rs " << interestByReference <<
endl;

return 0;
}

CPP Practical Assignment 12


// Output
Enter principal amount: Rs 5000
Enter rate of interest (% per annum): 6.5
Enter time period (in years): 3

Results:
Simple Interest (by value): Rs 975
Simple Interest (by reference): Rs 975

CPP Practical Assignment 13


9 Sum and Average of Five Numbers Using Class and Objects

#include <iostream>
using namespace std;

class Numbers {
private:
float n1, n2, n3, n4, n5;
float sum;
float average;

public:
// Function to input numbers
void inputNumbers() {
cout << "Enter five numbers:" << endl;
cin >> n1 >> n2 >> n3 >> n4 >> n5;
}

// Function to calculate sum


void calculateSum() {
sum = n1 + n2 + n3 + n4 + n5;
}

// Function to calculate average


void calculateAverage() {
average = sum / 5;
}

// Function to display results


void displayResults() {
cout << "\nResults:" << endl;
cout << "Sum: " << sum << endl;
cout << "Average: " << average << endl;
}
};

int main() {
Numbers numObj;

numObj.inputNumbers();
numObj.calculateSum();
numObj.calculateAverage();
numObj.displayResults();

return 0;
}

CPP Practical Assignment 14


// Output
Enter five numbers: 10 20 30 40 50
Results:
Numbers entered: 10, 20, 30, 40, 50
Sum: 150
Average: 30

CPP Practical Assignment 15


10 Multiply Two Numbers Using Private and Public Memb. Func.

#include <iostream>
using namespace std;

class Calculator {
private:
// Private member function
float multiplyNumbers(float a, float b) {
return a * b;
}

public:
// Public member function that uses the private function
void getProductAndDisplay() {
float num1, num2, product;

cout << "Enter first number: ";


cin >> num1;

cout << "Enter second number: ";


cin >> num2;

// Call private member function


product = multiplyNumbers(num1, num2);

cout << "\nResult: ";


cout << num1 << " × " << num2 << " = " << product << endl;
}
};

int main() {
Calculator calc;

// We can only call the public function


calc.getProductAndDisplay();

// The following line would cause a compilation error because


multiplyNumbers is private
// calc.multiplyNumbers(5, 10);

return 0;
}

// Output
Enter first number: 12.5
Enter second number: 4.5
Result: 12.5 × 4.5 = 56.25

CPP Practical Assignment 16


11 Print Triangle Structure Using Scope Resolution Operator

#include <iostream>
using namespace std;

class Pattern {
private:
int rows;

public:
// Constructor declaration
Pattern(int r);

// Function declaration
void printPattern();
};

// Constructor definition using scope resolution operator


Pattern::Pattern(int r) {
rows = r;
}

// Function definition using scope resolution operator


void Pattern::printPattern() {
for(int i = 1; i <= rows; i++) {
for(int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << endl;
}
}

int main() {
// Create object with 5 rows
Pattern triangle(5);

cout << "Pattern Output:" << endl;


triangle.printPattern();

return 0;
}

// Output
Pattern Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

CPP Practical Assignment 17


12 Program for Constructor and Destructor

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

class Student {
private:
int id;
string name;

public:
// Parameterized constructor
Student(int studentId, string studentName) {
id = studentId;
name = studentName;
}

// Function to display student information


void displayInfo() {
cout << "Student ID: " << id << ", Name: " << name << endl;
}

// Destructor
~Student() {
cout << "Destructor called for " << name << " with ID " << id;
}
};

int main() {
cout << "Creating students..." << endl;

// Using parameterized constructor


Student student1(101, "John Smith");
student1.displayInfo();

cout << "\n";

cout << "\nExiting main function..." << endl;

// Destructors will be called automatically when objects go out of scope


return 0;
}

CPP Practical Assignment 18


// Output
Creating students...
Student ID: 101, Name: John Smith
Exiting main function...
Destructor called for John Smith with ID 101.

CPP Practical Assignment 19

You might also like