Computer Science, asked by akashmbabu, 8 months ago

Q1: Write an algorithm to input a number. Find
its square if the entered number is multiple of
10. If entered number if not a multiple of 10,
then find its cube. [5]​

Answers

Answered by nooblygeek
2

We'll call the input number n.

If n \mod 10 \equiv 0 then

   return n^2

else

   return n^3

If you've not yet learned about modular arithmetic, then you can equivalently

just check if the last digit of the entered number is a 0 or not.

If it is 0 then the number is a multiple of 10. If it does not end in 0, then it is not a multiple of 10.

In e.g. python you could implement this as

def algorithm

if n % 10 == 0:

   n = n ** 2

else:

   n = n ** 3

return n

Similar questions