Anjalijaiswal1011 / C-codes

0 stars 0 forks source link

C++ 1 #1

Open Anjalijaiswal1011 opened 4 months ago

Anjalijaiswal1011 commented 4 months ago

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

cout << "Maths=" << Maths << ", Physics=" << Physics << ", Chemistry=" << Chemistry << endl;

return 0;

}

Anjalijaiswal1011 commented 4 months ago

/Write a program to read 3 numbers from the keyboard and display the larger value on the screen./

include

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 }

Anjalijaiswal1011 commented 4 months ago

/Write a function using reference variables as arguments to swap the values of a pair of integers./

include

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;

}

Anjalijaiswal1011 commented 4 months ago

/Write a macro that obtains the largest of 3 numbers/

include

using namespace std;

define MAX(a, b) ((a > b) ? a : b)

define MAX_OF_THREE(a, b, c) MAX(MAX(a, b), c)

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;

}

Anjalijaiswal1011 commented 4 months ago

/*Define a class to represent a bank account. Include the following members: Data members

  1. Name of the depositor
  2. Account number
  3. Type of account
  4. Balance amount in the account Member functions
  5. To assign initial values
  6. To deposit an amount
  7. To withdraw an amount after checking the balance
  8. To display name and balance Write a main program to test the program*/

include

include

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; }
Anjalijaiswal1011 commented 4 months ago

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

include

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;

}

Anjalijaiswal1011 commented 4 months ago

Oops