Advertisement
Guest User

Untitled

a guest
Jan 5th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdexcept>
  3. #include <cmath>
  4.  
  5. bool validNums(const int num1, const int num2) {
  6.     if (num1 < 1 || num1 > 9 || num2 < 2 || num2 > 999999999)
  7.         return false;
  8.     else
  9.         return true;
  10. }
  11.  
  12. int divisbleBy(int num1, int num2) {
  13.     for (; num1 != 0; --num1) {
  14.         if (num1 % num2 == 0)
  15.             return num1;
  16.     }
  17.     return 0;
  18. }
  19.  
  20. int produceAnswer(int &num1, int num2) {
  21.     int answer = 0;
  22.     if (!validNums(num1, num2)) {
  23.         return 0;
  24.     }
  25.     num1 = pow(10, num1) - 1; num2 = divisbleBy(num1, num2);
  26.     if (num2 != 0)
  27.         return num2;
  28.     return 0;
  29. }
  30.  
  31. int main()
  32. {
  33.     int num1 = 0, num2 = 0, answer = 0;
  34.     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;
  35.     std::cout << " The first number? [From 1 to 9] "; std::cin >> num1;
  36.     std::cout << "The second number? [From 2 to 999_999_999] "; std::cin >> num2;
  37.     answer = produceAnswer(num1, num2);
  38.     if (answer != 0)
  39.         std::cout << "Larges number is " << num1 << " and closest number that divisible by it is " << answer << std::endl;
  40.     else
  41.         std::cout << "Invalid" << std::endl;
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement