Code
Code
CODE
// Write a hello world program in C++
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
OUTPUT
Hello, World!
2. CODE
// Write a CPP program to generate Hotel bill. Using following features table
no., customer name, Customer contact, details of order. Compute the bill amount
and offer possible discounts
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
int main() {
int table, order;
string name, contact;
float bill = 0.0, discount;
vector<string> items;
vector<int> quantities;
vector<int> prices;
OUTPUT
----------------------------------------
HOTEL BILL
----------------------------------------
Table number: 123
Customer name: XYZ
Customer contact: 123456789
Order Details:
----------------------------------------
Item Quantity Price
----------------------------------------
Dosa 2 60
Idli 1 40
Cold Drink 5 20
----------------------------------------
Bill amount: Rs.260.00
Discount: Rs.0.00
----------------------------------------
Total amount: Rs.260.00
----------------------------------------
3. CODE
// Create a class called 'Patient'. Display Patient's billing amount and date
of appointment with the help of function. Use proper access specifiers for
Patient's data and distinguish between different types of constructor.
#include <iostream>
#include <string>
using namespace std;
class Patient {
private:
string name;
int age;
string gender;
string date;
float amount;
public:
Patient(string pat_name, int pat_age, string pat_gender, string
pat_date, float pat_amount) {
name = pat_name;
age = pat_age;
gender = pat_gender;
date = pat_date;
amount = pat_amount;
}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
OUTPUT
class Adder {
public:
// Inline member function to add two numbers
inline int add(int a, int b) {
return a + b;
}
};
int main(){
Adder adder;
int x,y;
cout<< "Enter the first number: ";
cin >> x;
cout << "Enter the second number: ";
cin >> y;
cout << "The sum of the two numbers is: " << adder.add(x,y);
return 0;
}
OUTPUT
//Write a program to demonstrate how a static data member can be accessed with
the help of a static member function.'#include <iostream>
#include <iostream>
using namespace std;
class Counter {
private:
static int count;
public:
static void increment() {
count++;
}
int Counter::count = 0;
int main() {
Counter::increment();
Counter::increment();
Counter::showCount();
return 0;
}
OUTPUT
Count: 2
6. CODE
//Implement a class Complex which represents the Complex Number data type.
Implement the following operations: 1. Overloaded operator+ to add two complex
numbers. 2 Overloaded operator* to multiply two complex numbers. 3. Overloaded <<
and >> to print and read Complex Numbers.
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
Complex() : real(0), imag(0) {}
friend istream& operator>>(istream& in, Complex& c) {
cout << "Enter real part: ";
in >> c.real;
cout << "Enter imaginary part: ";
in >> c.imag;
return in;
}
friend ostream& operator<<(ostream& out, const Complex& c) {
out << c.real;
if (c.imag >= 0)
out << " + " << c.imag << "i";
else
out << " - " << -c.imag << "i";
return out;
}
int main() {
Complex num1, num2, sum, product;
return 0;
}
OUTPUT
First Number: 2 + 3i
Second Number: 4 + 5i
Sum: 6 + 8i
Product: -7 + 22i
9. CODE
class Animal {
public:
virtual void sound() = 0;
};
int main() {
Dog dog;
Cat cat;
dog.sound();
cat.sound();
return 0;
}
OUTPUT
Dog says: Woof!
Cat says: Meow!
10. CODE
//Write a C++ program for polymorphism and distinguish between different types of
polymorphism.
#include <iostream>
using namespace std;
// Runtime polymorphism
Dog dog;
Cat cat;
dog.speak();
cat.speak();
return 0;
}
OUTPUT
class Number {
public:
// Virtual function to display the number
virtual void display() {
cout << "This is a generic number." << endl;
}
};
public:
Integer(int v) : value(v) {}
public:
Float(float v) : value(v) {}
int main() {
// Create objects of Integer and Float
Number* num1 = new Integer(10);
Number* num2 = new Float(3.14);
delete num1;
delete num2;
return 0;
}
OUTPUT
Integer value: 10
Float value: 3.14
13. CODE
//Swapping Two Number using Template.
#include <iostream>
using namespace std;
int main() {
// Swapping integers
int x = 10, y = 20;
cout << "Before swapping (int): x = " << x << ", y = " << y << endl;
swapValues(x, y);
cout << "After swapping (int): x = " << x << ", y = " << y << endl;
return 0;
}
OUTPUT
//Arithmetic Exception Handling using single try and catch block (divisible by
0).
#include <iostream>
#include <stdexcept> // For std::runtime_error
using namespace std;
int main() {
int num1, num2;
try {
// Check if denominator is zero
if (num2 == 0) {
throw runtime_error("Error: Division by zero is not allowed!");
}
// Perform division
int result = num1 / num2;
cout << "Result: " << result << endl;
}
catch (const runtime_error& e) {
// Catch the exception and print error message
cout << e.what() << endl;
}
return 0;
}
OUTPUT
JAVA
// Input
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();
// Arithmetic operations
double sum = num1 + num2;
double difference = num1 - num2;
double product = num1 * num2;
double quotient = 0;
if (num2 != 0) {
quotient = num1 / num2;
} else {
System.out.println("Error: Division by zero!");
}
// Output
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
if (num2 != 0) {
System.out.println("Quotient: " + quotient);
}
scanner.close();
}
}
OUTPUT
if num2 != 0:
quotient = num1 / num2
else:
print("Error: Division by zero!")
print("Sum:", sum_result)
print("Difference:", difference)
print("Product:", product)
if num2 != 0:
print("Quotient:", quotient)
OUTPUT