1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include <iostream>
using namespace std;
int Quotient(long a, long b);
int Remainder(long a, long b, int c);
// function quotient
int Quotient(long a, long b)
{
return a / b;
}
// Function remainder
int Remainder(long a, long b, int c)
{
int quotient;
int remainder;
int mul;
quotient = a / b;
if (c == 1){
a*= 10; // 1 decimal after the decimal point
mul = 10;
} else if (c == 2) {
a*= 100; // 2 decimal after the decimal point
mul = 100;
} else if (c == 3) {
a*= 1000; // 3 decimal after the decimal point
mul = 1000;
} else {
a*= 10; // default. 1 decimal after the decimal point
mul = 10;
}
remainder = a / b;
remainder = (remainder - (quotient * mul));
return remainder;
}
int main()
{
int a;
int b;
int c;
int r;
cout << "Enter a divisor! \n";
cin >> a;
cout << "Enter a dividend! \n";
cin >> b;
cout << "Enter decimals! 1 to 3 \n";
cin >> c;
cout << "The quotient of those numbers is " << Quotient(a, b) << endl;
cout << "The remainder of those numbers is " << Remainder(a, b, c) << endl;
r = Remainder(a, b, c);
if (c == 1) {
cout << "The number is " << Quotient(a, b) << "." << r << endl;
} else if (c == 2) {
if (r > 9) {
cout << "The number is " << Quotient(a, b) << "." << r << endl;
} else {
cout << "The number is " << Quotient(a, b) << ".0" << r << endl;
}
} else if (c == 3) {
if (r > 99) {
cout << "The number is " << Quotient(a, b) << "." << r << endl;
} else if (r > 9) {
cout << "The number is " << Quotient(a, b) << ".0" << r << endl;
} else {
cout << "The number is " << Quotient(a, b) << ".00" << r << endl;
}
}
cin >> a;
return 0;
}
|