Computer Science, asked by aniefiokw, 10 months ago

write an algorithm to print the first ten even numbers

Answers

Answered by taimurwazir80
2

let count = 0;

let even = 0; // zero is the first even number

while count != 10

{

print even;

even = even + 2;

count = count + 1;

}

If you want to test if an integer is even or odd, test the low-order bit using bitwise AND (&):

if num & 0x1 then

// num is odd

else

// num is even

Note that this is only guaranteed to work when all systems use twos-complement notation. If your program is intended to work on systems that may still be using ones-complement notation (which is rare these days), it's better to use the modus operator (%) instead:

if num % 2 then // num is odd else // num is even

The modus operator simply returns the remainder after division. Dividing any integer by 2 always leaves a remainder of either 0 (false, therefore even) or 1 (true, therefore odd) and is guaranteed to work on both ones-complement as well as twos-complement systems. However, the division adds a computational overhead that is unnecessary on a twos-complement system. A better approach would be to use precompiler directives or compile-time computation to determine the actual notation and choose the appropriate operation accordingly.

Similar questions