N-Divisible Digits small program Heres the idea
Write a program that takes two integers, N and M, and find the largest integer composed of N-digits that is evenly divisible by M. N will always be 1 or greater, with M being 2 or greater. Note that some combinations of N and M will not have a solution.
Example: if you are given an N of 3 and M of 2, the largest integer with 3-digits is 999, but the largest 3-digit number that is evenly divisible by 2 is 998, since 998 Modulo 2 is 0. Another example is where N is 2 and M is 101. Since the largest 2-digit integer is 99, and no integers between 1 and 99 are divisible by 101, there is no solution.

I would just like some tips on what to improve

#include &lt;iostream&gt;
#include &lt;stdexcept&gt;
#include &lt;cmath&gt;

using std::cin; using std::cout; using std::endl; using std::runtime_error; 
using std::pow;

unsigned largestNumber(unsigned num1) {
return pow(10, num1) - 1;
}

void validNums(const int num1, const int num2) {
    if (num1 &lt; 1 || num1 &gt; 9 || num2 &lt; 2 || num2 &gt; 999999999)
        throw std::runtime_error("invalid input");
}

int divisbleBy(int num1, int num2) {
    for (; num1 != 0; --num1) {
        if (num1 % num2 == 0)
            return num1;
    }
    return 0;
}

int main()
{
    int num1 = 0, num2 = 0;
    int answer = 0;
    cout &lt;&lt; "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. Num1 has to be between 1-9 and num2 between 2-999-999-999" &lt;&lt; endl;
    cin &gt;&gt; num1 &gt;&gt; num2;
    validNums(num1, num2);
    if (answer = divisbleBy(largestNumber(num1), num2))
        cout &lt;&lt; "Largest number possible with " &lt;&lt; num1 &lt;&lt; " is " &lt;&lt; largestNumber(num1) &lt;&lt; " and the closest number to " &lt;&lt; largestNumber(num1) &lt;&lt; " divisible by " &lt;&lt; num2 &lt;&lt; " is " &lt;&lt; answer &lt;&lt; endl;
    else
        cout &lt;&lt; "Invalid" &lt;&lt; endl;
    return 0;
}
You should try to find the O(1) algorithm for this problem, instead of using your current O(M) algorithm.

For example, given the input 9 500000001, your program takes 2.3 seconds (on my laptop) to run through the half-billion integers between 999999999 and 500000001. Whereas, using the relatively obvious O(1) algorithm, it takes 0.007 seconds.

Since this looks like homework and/or a Project Euler–type problem, I won't give the spoiler here.



Stylistically,


Prefer to write out std::cin, std::cout, etc., instead of wasting space at the top of the program with a bunch of using directives. It's shorter, cleaner, and (most importantly) more idiomatic to qualify names at the point of use instead of relying on using directives to pull them into scope.
Your function validNums uses exceptions for control flow, which is frowned upon in C++. Prefer to make it a function returning bool, and have its caller do something sensible (such as print an error message) if the given numbers aren't valid.
You have a named abstraction for largestNumber, when that could just as well have been a named local variable int largestNumber = pow(...);.
You don't have a named abstraction for produceTheActualAnswer; instead, you inline a complicated test if (answer = divisbleBy(largestNumber(num1), num2)) straight into main. This makes it unnecessarily difficult to switch out your bad old algorithm for a new and better one.
Putting an assignment inside an if condition is supremely bad style — so bad that both GCC and Clang (and probably MSVC too) will emit a warning on this code. You should write either

int answer = produceTheActualAnswer(num1, num2);
if (answer != 0) {
    ...
} else {
    ...
}


or, if you really want to use "interesting" features of C++, you might write

if (int answer = produceTheActualAnswer(num1, num2)) {
    ...
} else {
    ...
}


But I don't recommend the latter.




You should also consider taking your num1 and num2 parameters via the command line, instead of reading them from stdin:

int main(int argc, char **argv) {
    if (argc &lt; 3) {
        // maybe read them from stdin anyway
    } else {
        int m = std::atoi(argv[1]);
        int n = std::atoi(argv[2]);
        int answer = f(m, n);
        if (answer != 0) { ... } else { ... }
    }
}


This makes it easier to test your code quickly from the command line, and also makes it easy to time ./a.out 9 500000001 to see if you've beaten my 0.007 seconds yet. ;)