CppAssignment
CppAssignment
#include <iostream>
using namespace std;
int main() {
float num1, num2, sum;
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
#include <iostream>
using namespace std;
int main() {
float num1, num2;
return 0;
}
// Output
Enter first number: 45.6
Enter second number: 23.1
45.6 is greater than 23.1
#include <iostream>
using namespace std;
int main() {
int number;
long long factorial = 1;
int i = 1;
cout << "Factorial of " << number << " = " << factorial << endl;
}
return 0;
}
// Output
Enter a positive integer: 5
Factorial of 5 = 120
#include <iostream>
using namespace std;
int main() {
float num1, num2, result;
char 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;
}
// Another run
Enter first number: 20
Enter second number: 4
Choose an operation (+, -, *, /): /
Result: 20 / 4 = 5
#include <iostream>
using namespace std;
int main() {
int A[3][3], B[3][3], C[3][3];
// 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];
}
}
}
return 0;
}
#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];
#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;
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;
}
}
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;
}
Employee 2:
Name: Sara
ID type (1=numeric, 0=text): 0
Text ID: ABC123
Employee 1:
Name: John
ID: 101
Employee 2:
Name: Sara
ID: ABC123
#include <iostream>
using namespace std;
int main() {
float principal, rate, time, interestByValue, interestByReference;
return 0;
}
Results:
Simple Interest (by value): Rs 975
Simple Interest (by reference): Rs 975
#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;
}
int main() {
Numbers numObj;
numObj.inputNumbers();
numObj.calculateSum();
numObj.calculateAverage();
numObj.displayResults();
return 0;
}
#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;
int main() {
Calculator calc;
return 0;
}
// Output
Enter first number: 12.5
Enter second number: 4.5
Result: 12.5 × 4.5 = 56.25
#include <iostream>
using namespace std;
class Pattern {
private:
int rows;
public:
// Constructor declaration
Pattern(int r);
// Function declaration
void printPattern();
};
int main() {
// Create object with 5 rows
Pattern triangle(5);
return 0;
}
// Output
Pattern Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#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;
}
// Destructor
~Student() {
cout << "Destructor called for " << name << " with ID " << id;
}
};
int main() {
cout << "Creating students..." << endl;