Open Anjalijaiswal1011 opened 6 months ago
/Write a program to read 3 numbers from the keyboard and display the larger value on the screen./
using namespace std; int main() //main function { int a,b,c; cout<<"enter the value of num 1="; //output cin>>a; cout<<"enter the value of num 2="; cin>>b; cout<<"enter the value of num 3="; cin>>c; if(a>b>c) { cout<<"the greater among all is" <<a<<endl; // using if else condition } else if (b>a) { cout<<"the greater among the all is"<<b<<endl;
}
else{ cout<<"the greater among all is" <<c<<endl;
} return 0; //returning value }
/Write a function using reference variables as arguments to swap the values of a pair of integers./
using namespace std; // Function to swap values using reference variables void swap(int &a, int &b) { int temp = a; a = b; b = temp; }
int main() { int x = 45; int y = 109;
cout << "Before swap: x = " << x << ", y = " << y << std::endl;
// Call the swap function
swap(x, y);
cout << "After swap: x = " << x << ", y = " << y << std::endl;
return 0;
}
/Write a macro that obtains the largest of 3 numbers/
using namespace std;
int main() { int num1, num2, num3;
cout << "Enter three integers: ";
cin >> num1 >> num2 >> num3;
int largest = MAX_OF_THREE(num1, num2, num3);
cout << "The largest of the three numbers is: " << largest << endl;
return 0;
}
/*Define a class to represent a bank account. Include the following members: Data members
using namespace std;
class BankAccount { private: string depositorName; long accountNumber; string accountType; double balance;
public: BankAccount(const string& name, long accNumber, const string& accType, double initialBalance) { depositorName = name; accountNumber = accNumber; accountType = accType; balance = initialBalance; }
void deposit(double amount) {
balance += amount;
cout << "Deposit of $" << amount << " successful." << endl;
}
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
cout << "Withdrawal of $" << amount << " successful." << endl;
} else {
cout << "Insufficient balance. Withdrawal failed." << endl;
}
}
void display() {
cout << "Depositor Name: " << depositorName << endl;
cout << "Balance: $" << balance << endl;
}
};
int main() { BankAccount account("John Doe", 123456789, "Savings", 1000.0);
account.deposit(500.0);
account.withdraw(200.0);
account.display();
return 0; }
/Create two classes DM and DB which store the value of distances. DM stores distances in meters and centimeters and DB in feet and inches. Write a program that can read values for the class objects and odd one object of DM with another object of DB. Use a friend function to carry out the addition operation. The object that stores the results may be a DM object or DB object, depending on the units in which the result are required. The display should be in the format of feet and inches or meters and centimeters depending on the object on display./
using namespace std;
class DB;
class DM { private: int meters; int centimeters;
public: DM(int m = 0, int cm = 0) : meters(m), centimeters(cm) {}
friend void add(DM &d1, DB &d2);
void display() {
cout << "Distance: " << meters << " meters " << centimeters << " centimeters" << endl;
}
};
class DB { private: int feet; int inches;
public: DB(int f = 0, int i = 0) : feet(f), inches(i) {}
friend void add(DM &d1, DB &d2);
void display() {
cout << "Distance: " << feet << " feet " << inches << " inches" << endl;
}
}; void add(DM &d1, DB &d2) { int totalMeters = d1.meters + d2.feet 0.3048; int totalCentimeters = d1.centimeters + d2.inches 2.54;
totalMeters += totalCentimeters / 100;
totalCentimeters %= 100;
cout << "Result in DM: " << totalMeters << " meters " << totalCentimeters << " centimeters" << endl;
}
int main() { DM d1(5, 80); DB d2(16, 24); cout << "Original distances:" << endl; cout << "DM: "; d1.display(); cout << "DB: "; d2.display(); add(d1, d2);
return 0;
}
Oops
/Write a program to display the following output using a single cout statement. Maths=90, Physics=74, Chemistry=76/
include
using namespace std;
int main() { int Maths = 90, Physics = 74, Chemistry = 76;
}