Task 6 complete
This commit is contained in:
50
task6.cpp
Normal file
50
task6.cpp
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
// Задание 6
|
||||||
|
// Написать программу для нахождения частного и остатка от деления двух
|
||||||
|
// чисел методом половинного деления
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// Функция для нахождения частного и остатка от деления
|
||||||
|
void divide(int dividend, int divisor, int "ient, int &remainder) {
|
||||||
|
quotient = 0;
|
||||||
|
remainder = dividend;
|
||||||
|
|
||||||
|
int currentDivisor = divisor;
|
||||||
|
int currentQuotient = 1;
|
||||||
|
|
||||||
|
while (remainder >= divisor) {
|
||||||
|
currentDivisor = divisor;
|
||||||
|
currentQuotient = 1;
|
||||||
|
|
||||||
|
while (remainder >= currentDivisor + currentDivisor) {
|
||||||
|
currentDivisor += currentDivisor;
|
||||||
|
currentQuotient += currentQuotient;
|
||||||
|
}
|
||||||
|
|
||||||
|
remainder -= currentDivisor;
|
||||||
|
quotient += currentQuotient;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int dividend, divisor;
|
||||||
|
|
||||||
|
std::cout << "Введите делимое: ";
|
||||||
|
std::cin >> dividend;
|
||||||
|
std::cout << "Введите делитель: ";
|
||||||
|
std::cin >> divisor;
|
||||||
|
|
||||||
|
if (divisor == 0) {
|
||||||
|
std::cout << "Ошибка: деление на ноль!" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int quotient, remainder;
|
||||||
|
divide(dividend, divisor, quotient, remainder);
|
||||||
|
|
||||||
|
std::cout << "Частное: " << quotient << std::endl;
|
||||||
|
std::cout << "Остаток: " << remainder << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user