We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 18
program to find the largest of three numbers.
// Input three numbers
cout << "Enter three numbers: ";
cin>>a>>b>> =
// Find the largest using a single if statement
int large = a;
if (b> large) large = b;
if (c > large) large = c;
cout << "The largest number is: << large << endl;
return 0;
OUTPUT:
Enter three numbers: 5 91
The largest number is: 9int arr{n];
cout << “Enter " << n <<" elements:
for (int i = 0; i > arr{i];
// Bubble sort for both orders
i arr{j + 1) swap(arr[j], arr{j+4 ys
B
}
cout << "Ascending: ";
for (inti=0; i=
+) cout << arr[ 1] <<""3
cout << endl;
return 0;
}
OUTPUT:
Enter number of elements: 5
Enter 5 elements: 24159 ;
Ascending: 12459
Descending: 95421:
3. hvelop a C++ program using classes to display student name, roll
number, marks obtained in two subjects and total score of student.
PROGRAM CODE:
finclude
using namespace std;
class Student {
public:
string name;
int rolINo, marks1, marks2;
// Function to input student details
void input() {
cout << "Enter name: ";
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter marks in two subjects: ";
cin >> marks1 >> marks2;
}
// Function to display student details
void display() {
int total = marks1 + marks2;
cout << "Name: " << name << "\nRoll No: "<< rollNo
<<"\nTotal Marks: " << total << endl;
int main() {
Student s;
s.input();
s.display();
return 0;4. Develop a C++ program for a bank empolyee to print name of the
‘employee, account_no. & balance. Print invalid balance if amount<
Display the same, also display the balance after withdraw and deposit.
PROGRAM CODE:
Hinclude
using namespace std;
class BankEmployee {
public:
string name;
int account_no;
float balance;
void input() {
cout << "Enter name, account number, and balance: ";
cin >> name >> account_no >> balance;
if (balance < 500)
cout << "Invalid balance! Balance must be at least 500." << endl;
void display() {
cout << "Name: "<< name << "\nAccount No: " << account_no <<
"\nBalance: " << balance << endl;
)
void withdraw(float amount) {
if (balance - amount < 500)
cout << "Withdrawal failed! Insufficient balance." << endl;
else {
balance -= amount;
cout << "New Balance: " << balance << endl;
y
}
void deposit(float amount) {cout << "Enter amount to withdraw: "; cin >> amount;
i emp.withdraw(amount);
cin >> amount;
cout << "Enter amount to deposi
emp.deposit(amount);
}
return 0;
}
OUTPUT :
Enter name, account number, and balance: Uddhav 1098765423 1000
Name: Uddhav
Account No: 1098765423
Balance: 1000
Enter amount to withdraw: 500
New Balance: 500
Enter amount to deposit: 200
New Balance: hae |following prototypes. add(int a, int b)
add(double a, double b)
PROGRAM CODE:
Hinclude
using namespace std;
// Function to add two integers
int add(int a, int b) {
return a+b;
}
// Function to add two doubles
double add(double a, double b) {
return a + b;
int main() {
// Adding integers
int intResult = add(5, 3);
cout << "Sum of integers: " << intResult << endl; Jp
// Adding doubles
double doubleResult = add(5.5, 3.2);
cout << "Sum of doubles: " << doubleResult << endl;
return 0; Fiimduch
} pany name spare SY:
imt add (imk a, int 6);
OUTPUT: be
0
Sum of integers: 8 Couple add (double a, double b),
Sum of doubles: 8.7 Ca amet}
£ .
tow << odd (3,4) 2a eral 5
Gout << add (35,45);
tmt add Limba, cmt b) ft Reeth
im dourle (dourle a, double b)
yveturm Geb) )
eint ny;
public:
void getNum(int x)
4
n=x;,
}
void dispNum(void)
{
cout << "value of nis:" << n;
}
void operator - (void)
{
n=-n;
}
i
int main()
{
NUM num;
num.getNum(10);
-num;
num.dispNum();
cout << endl;
return 0;
}
OUTPUT:
value of nis: -10// Base class for arithmetic operations
class Arithmetic
public:
double num1, num2;
// Constructor to initialize numbers
Arithmetic(double a, double b) : num1(a), num2(b) {}
double add() { return num1 + num2; }
double subtract() { return num1 - num2; }
\
// Base class for displaying results
class Display {
public:
void showResult(double result) {
cout << “Result: " << result << endl;
)
i
// Derived class for operations
class Operations : public Arithmetic, public Display {
public:
Operations(double a, double b) : Arithmetic(a, b) {}
i
| void perform) ( E
__ showResult(add()); ——_// Show addition result
showResult(subtract()); // Show subtraction resultobject of the derived class
op(number1, number2);
fpecorn and display results
% ‘op.perform();
return 0;
}
OUTPUT:
Enter two numbers: 5 6
Result: 11
Result: -1ct of the derived class
ber1, number2);
id and display results
OUTPUT:
Enter two numbers: 5 6
Result: 11
Result: -1
—Constructor in Derived classes to initialize q
corresponding values: i
public:
Base(int a) : alpha(a) {} // Constructor
‘
// Derived class
class Derived ; public Base {
private:
int beta, gamma;
public:
Derived(int a, int b, int c) ; Base(a), beta(b), gamma(c) { }// Constructor
void display() {
cout << "Alpha; "<< alpha << ", Beta: " << beta <<", Gamma: " <<
gamma << endl;
} ) Class &
int main() { probectad: imt A
int alpha, beta, gamma; = z
cout << "Enter alpha, beta, and gamma: "; eee ae mie
cin >> alpha >> beta >> gamma; s:
Derived obj(alpha, beta, gamma); // Create object class D
obj. display( ); // Display values 2
return 0; private: imt B, @
}
ourput: public: D (imr@,ir y:
Enter alpha, beta, and gamma: 5 10 15 ime 1B
Alpha: 5, Beta: 10, Gamma: 15
eeJlusing ofstream for output file operations.
ofstream file;
// Opening file "Gfg.txt" in write mode.
file.open("Gfg.txt");
// Check if the file was successfully created.
if (!file.is_open())
{
cout << “Error in creating file!" << endl;
// Return a non-zero value to indicate an error.
return 1;
}
cout << "File created successfully." << endl;
// Close the file to free up resources.
file.close();
return 0;
OUTPUT:
File created successfully.10. Develop a C++ program to write and read time in/from binary file using
fstream.
PROGRAM CODE:
Hinclude
Hinclude
using namespace std;
// Structure to hold time values
struct Time {
int hours, minutes, seconds;
i
int main() {
Time t;
// Input time from the user
cout << "Enter time (hours minutes seconds): ";
cin >> t.hours >> t.minutes >> t.seconds;
// Write time to a binary file
ofstream outFile("time.dat", ios::binary);
outFile.write(reinterpret_cast(&t), sizeof(t));
outFile.close();
// Read time from the binary file
Time readTime;
ifstream inFile("time.dat", ios::binary);
inFile.read(reinterpret_cast(&readTime), sizeof(readTime));
inFile.close();
// Display the read time
cout << "Time read from file: "
<< readTime.hours << "."41. Develop a function which throws a division by zero exception
catch it in catch block. Write a C++ program to demonst
try, catch and throw to handle exception.
PROGRAM CODE:
#include
using namespace std;
// Function that divides two numbers
double divide(int numerator, int denominator) {
if (denominator == 0) {
throw "Division by zero!"; // Throw exception
}
return numerator / (double)denominator; // Cast denominator to
double
}
int main() {
int num, denom;
// \nput from user
cout << "Enter numerator: ";
cin >> num;
cout << "Enter denominator: ";
cin >> denom;
try{
double result = divide(num, denom); // Try to divide
cout << "Result: " << result << endl;
} catch (char* e) { // Catch division by zero
cout << “Error: " << e << endl;
}
return 0; }
OUTPUT:
Enter numerator: 10
Enter denominator: 0
Error: Division by zero!
and
rate usage of//\nput from user
cout << "Enter index to access (0-4): ";
cin >> index;
try {
// Try to get the element at the given index
int value = getElement(arr, 5, index);
cout << "Element at index " << index <<
} catch (char* e) {
// Catch and print error if index is out of bounds
cout << "Error: " << e << endl;
<< value << endl;
return 0;
&Element at index 2 is: 30
Enter index to access (0-4): 5
Error: Array index out of bounds!