C++ program to find the quotient and remainder using class

Given the value of dividend and divisor, we have to find the quotient and remainder using the class and object approach.
Submitted by Shubh Pachori, on September 09, 2022

Example:

Input: 
Enter Dividend : 23
Enter Divisor : 3

Output: 
Quotient of 23 / 3 is 7
Remainder of 23 % 3 is 2

C++ code to find the quotient and remainder using the class and object approach

#include <iostream>
using namespace std;

// create a class
class Division {
  // private data members
 private:
  int Dividend, Divisor;

  // public member function
 public:
  // getValues() function to insert values
  void getValues() {
    cout << "Enter Dividend : ";
    cin >> Dividend;

    cout << "Enter Divisor : ";
    cin >> Divisor;
  }

  // division() function to calculate
  // Remainder and Quotient
  void division() {
    float Quotient, Remainder;

    // arithmetic operations
    Quotient = Dividend / Divisor;
    Remainder = Dividend % Divisor;

    // displaying of quotient and remainder
    cout << "Quotient of " << Dividend << " / " << Divisor << " is " << Quotient
         << endl;
    cout << "Remainder of " << Dividend << " % " << Divisor << " is "
         << Remainder << endl;
  }
};

int main() {
  // creating object
  Division D;

  // calling getValues() function to 
  // insert values
  D.getValues();

  // calling division() function to calculate
  // Remainder and Quotient
  D.division();

  return 0;
}

Output:

RUN 1:
Enter Dividend : 10
Enter Divisor : 3
Quotient of 10 / 3 is 3
Remainder of 10 % 3 is 1

RUN 2:
Enter Dividend : 49
Enter Divisor : 9
Quotient of 49 / 9 is 5
Remainder of 49 % 9 is 4

Explanation:

In the above code, we have created a class Division,two int type data members Dividend and Divisor to store dividend and divisor, and public member functions getValues() and division() to store dividend and divisor, to calculate remainder and quotient.

In the main() function, we are creating an object D of class Division, reading dividend an divisor by the user using getValues() function, and finally calling the division() member function to calculate remainder and quotient. The division() function contains the logic to calculate remainder and quotient.

C++ Class and Object Programs (Set 2) »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.