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

Code

The document contains multiple C++ code snippets demonstrating various programming concepts such as basic I/O, class creation, operator overloading, polymorphism, and exception handling. Each code example is followed by sample output illustrating the functionality of the code. Additionally, there is a Java code snippet for performing arithmetic operations.

Uploaded by

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

Code

The document contains multiple C++ code snippets demonstrating various programming concepts such as basic I/O, class creation, operator overloading, polymorphism, and exception handling. Each code example is followed by sample output illustrating the functionality of the code. Additionally, there is a Java code snippet for performing arithmetic operations.

Uploaded by

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

1.

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;

cout << "Enter the table number: ";


cin >> table;
cin.ignore(); // Clear the input buffer

cout << "Enter the customer name: ";


getline(cin, name);

cout << "Enter the customer contact: ";


getline(cin, contact);

cout << "Enter the no of order: ";


cin >> order;
cin.ignore(); // Clear the input buffer

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


string item;
int quantity, price;
cout << "Enter the item: ";
getline(cin, item);
cout << "Enter the quantity: ";
cin >> quantity;
cout << "Enter the price: ";
cin >> price;
cin.ignore(); // Clear the input buffer
items.push_back(item);
quantities.push_back(quantity);
prices.push_back(price);
bill += price * quantity;
}

if (bill > 1000) {


discount = bill * 0.1;
}
else if (bill > 500) {
discount = bill * 0.05;
}
else {
discount = 0;
}

// Print bill header


cout << "\n" << string(40, '-') << endl;
cout << "HOTEL BILL" << endl;
cout << string(40, '-') << endl;

cout << "Table number: " << table << endl;


cout << "Customer name: " << name << endl;
cout << "Customer contact: " << contact << endl;
cout << "\nOrder Details:" << endl;
cout << string(40, '-') << endl;
cout << setw(20) << left << "Item" << setw(10) << "Quantity" << setw(10) <<
"Price" << endl;
cout << string(40, '-') << endl;

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


cout << setw(20) << left << items[i]
<< setw(10) << quantities[i]
<< setw(10) << prices[i] << endl;
}

cout << string(40, '-') << endl;


cout << fixed << setprecision(2);
cout << "Bill amount: Rs." << setw(10) << bill << endl;
cout << "Discount: Rs." << setw(10) << discount << endl;
cout << string(40, '-') << endl;
cout << "Total amount: Rs." << setw(10) << bill - discount << endl;
cout << string(40, '-') << endl;
return 0;}

OUTPUT

Enter the table number: 123


Enter the customer name: XYZ
Enter the customer contact: 123456789
Enter the no of order: 3
Enter the item: Dosa
Enter the quantity: 2
Enter the price: 60
Enter the item: Idli
Enter the quantity: 1
Enter the price: 40
Enter the item: Cold Drink
Enter the quantity: 5
Enter the price: 20

----------------------------------------
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;

cout << "Date: " << date << endl;


cout << "Amount: " << amount << endl;
}
};
int main(){
string name;
int age;
string gender;
string date;
float amount;
cout << "Enter patient name: ";
cin >> name;
cout << "Enter patient age: ";
cin >> age;
cout << "Enter patient gender: ";
cin >> gender;
cout << "Enter date of appointment: ";
cin >> date;
cout << "Enter billing amount: ";
cin >> amount;
Patient p(name, age, gender, date, amount);
p.display();
return 0;
}

OUTPUT

Enter patient name: XYZ


Enter patient age: 12
Enter patient gender: Male
Enter date of appointment: 07/12/2025
Enter billing amount: 50000
Name: XYZ
Age: 12
Gender: Male
Date: 07/12/2025
Amount: 50000
4. CODE

//Perform the addition of two numbers using an inline member function.


#include <iostream>
using namespace std;

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

Enter the first number: 10


Enter the second number: 20
The sum of the two numbers is: 30
5. CODE

//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++;
}

static void showCount() {


cout << "Count: " << count << endl;
}
};

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;
}

Complex operator+(const Complex& other) const {


Complex result;
result.real = real + other.real;
result.imag = imag + other.imag;
return result;
}

Complex operator*(const Complex& other) const {


Complex result;
result.real = real * other.real - imag * other.imag;
result.imag = real * other.imag + imag * other.real;
return result;
}
};

int main() {
Complex num1, num2, sum, product;

cout << "Enter first complex number:\n";


cin >> num1;

cout << "\nEnter second complex number:\n";


cin >> num2;

sum = num1 + num2;


product = num1 * num2;

cout << "\nFirst Number: " << num1 << endl;


cout << "Second Number: " << num2 << endl;
cout << "Sum: " << sum << endl;
cout << "Product: " << product << endl;

return 0;
}

OUTPUT

Enter first complex number:


Enter real part: 2
Enter imaginary part: 3

Enter second complex number:


Enter real part: 4
Enter imaginary part: 5

First Number: 2 + 3i
Second Number: 4 + 5i
Sum: 6 + 8i
Product: -7 + 22i
9. CODE

// Write a program based on abstract class in c++


#include <iostream>
using namespace std;

class Animal {
public:
virtual void sound() = 0;
};

class Dog : public Animal {


public:
void sound() override {
cout << "Dog says: Woof!" << endl;
}
};

// Derived class: Cat


class Cat : public Animal {
public:
void sound() override {
cout << "Cat says: Meow!" << endl;
}
};

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;

// Compile-time Polymorphism: Function Overloading


class Calculator {
public:
int add(int a, int b) {
return a + b;
}

float add(float a, float b) {


return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}
};

// Runtime Polymorphism: Inheritance and Virtual Functions


class Animal {
public:
virtual void speak() {
cout << "Animal makes a sound." << endl;
}
};

class Dog : public Animal {


public:
void speak() override {
cout << "Dog says: Woof!" << endl;
}
};

class Cat : public Animal {


public:
void speak() override {
cout << "Cat says: Meow!" << endl;
}
};
int main() {
// Compile-time polymorphism
Calculator calc;
cout << "Add (int, int): " << calc.add(2, 3) << endl;
cout << "Add (float, float): " << calc.add(2.5f, 3.5f) << endl;
cout << "Add (int, int, int): " << calc.add(1, 2, 3) << endl;

cout << endl;

// Runtime polymorphism
Dog dog;
Cat cat;

dog.speak();
cat.speak();

return 0;
}

OUTPUT

Add (int, int): 5


Add (float, float): 6
Add (int, int, int): 6

Dog says: Woof!


Cat says: Meow!
12. CODE
//Write a C++ program on virtual function
#include <iostream>
using namespace std;

class Number {
public:
// Virtual function to display the number
virtual void display() {
cout << "This is a generic number." << endl;
}
};

class Integer : public Number {


private:
int value;

public:
Integer(int v) : value(v) {}

void display() override { // Override the virtual function


cout << "Integer value: " << value << endl;
}
};

class Float : public Number {


private:
float value;

public:
Float(float v) : value(v) {}

void display() override { // Override the virtual function


cout << "Float value: " << value << endl;
}
};

int main() {
// Create objects of Integer and Float
Number* num1 = new Integer(10);
Number* num2 = new Float(3.14);

// Call the virtual function display() on both objects


num1->display(); // Integer's display() will be called
num2->display(); // Float's display() will be called

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;

template <typename T>


void swapValues(T &a, T &b) {
T temp = a;
a = b;
b = temp;
}

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;

// Swapping floating-point numbers


float p = 3.14f, q = 1.618f;
cout << "Before swapping (float): p = " << p << ", q = " << q << endl;
swapValues(p, q);
cout << "After swapping (float): p = " << p << ", q = " << q << endl;

return 0;
}

OUTPUT

Before swapping (int): x = 10, y = 20


After swapping (int): x = 20, y = 10
Before swapping (float): p = 3.14, q = 1.618
After swapping (float): p = 1.618, q = 3.14
14. CODE

//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;

// Taking user input


cout << "Enter two numbers: ";
cin >> 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

Enter two numbers: 12 0


Error: Division by zero is not allowed!
15. CODE

JAVA

//Arithmetic operations using java


import java.util.Scanner;

public class ArithmeticOperations {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Input
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();

System.out.print("Enter second number: ");


double num2 = 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

Enter first number: 10


Enter second number: 20
Sum: 30.0
Difference: -10.0
Product: 200.0
Quotient: 0.5
PYTHON

# Arithmetic operations using python


num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

sum_result = num1 + num2


difference = num1 - num2
product = num1 * num2
quotient = None

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

Enter first number: 10


Enter second number: 25
Sum: 35.0
Difference: -15.0
Product: 250.0
Quotient: 0.4

You might also like