Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdexcept>
- #include <cmath>
- bool validNums(const int num1, const int num2) {
- if (num1 < 1 || num1 > 9 || num2 < 2 || num2 > 999999999)
- return false;
- else
- return true;
- }
- int divisbleBy(int num1, int num2) {
- for (; num1 != 0; --num1) {
- if (num1 % num2 == 0)
- return num1;
- }
- return 0;
- }
- int produceAnswer(int &num1, int num2) {
- int answer = 0;
- if (!validNums(num1, num2)) {
- return 0;
- }
- num1 = pow(10, num1) - 1; num2 = divisbleBy(num1, num2);
- if (num2 != 0)
- return num2;
- return 0;
- }
- int main()
- {
- int num1 = 0, num2 = 0, answer = 0;
- std::cout << "Enter 2 nums and we'll find biggest number possible with num1 digits and we'll then find closest number that num2 can divide by." << std::endl;
- std::cout << " The first number? [From 1 to 9] "; std::cin >> num1;
- std::cout << "The second number? [From 2 to 999_999_999] "; std::cin >> num2;
- answer = produceAnswer(num1, num2);
- if (answer != 0)
- std::cout << "Larges number is " << num1 << " and closest number that divisible by it is " << answer << std::endl;
- else
- std::cout << "Invalid" << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement