Of course there’s an easier way. Just integrate the state of the art API dedicated for this exact problem. https://isevenapi.xyz/
You could do this in one line…
By removing all the linebreaks.
modulo
pseudocode:
if number % 2 == 0
return "number is even" (is_num_even = 1 or true)
else
return "number is odd" (is_num_even = 0 or false)
plus you’d want an input validation beforehand
who needs modulo when you can get less characters out of
while (number > 1) {
number -= 2;
}
return number;
very efficient
edit: or theres the trusty iseven api
#You are an input. You have value! You matter!
if number % 2 == 0
return "number is even" (is_num_even = 1 or true)
else
return "number is odd" (is_num_even = 0 or false)
Am I doing it right? /S.
Modulo
Back when I was learning programming a lot of lessons would make you do something like this, and then show you the real way to do it in the next lesson. My reaction was always “why didn’t you lead with this?”.
Because the point of the lesson is to demonstrate that you can solve the same problem multiple ways where some paths are more efficient than others.
Bad programmers are the ones that find the first solution and implement it no matter how inefficient it is.
Good programmers spend time on figuring out the solution with the least amount broken or inefficient code. You don’t learn this by jumping straight to the best answer every time.