Welcome to the Ranch
Make sure to test your program with negative numbers, unless you are specifically restricting its domain to natural numbers.
You aren't writing object‑oriented code, but procedural code. I suggest you change to a functional style:-
You can now use that method from elsewhere, preferably in a second class.
Please always use the
code button. I shall use it on your code and you can see how much better it will look
