Exercise: Find a Number in a List

Test your knowledge of the conditional and looping directives covered in this chapter.

Problem statement

In this challenge, you have to find a number in a list.

Given a number $num and a list @list, traverse the list to check if it contains the number $num:

  • If $num exists in the list, report it using:
print 'Found the number';
  • If $num doesn’t exist in the list, report it using:
print 'Did not find the number';

Sample input

@list = (1, 2, 3, 4, 5, 6, 7, 8)
$num = 5

Expected output

Found the number

Coding challenge

This is a simple problem related to the conditional and looping directives we’ve covered. Assume that the number $num and list @list have already been declared and defined.

If you feel stuck, you can always refer to the solution review in the next lesson.

Good luck!

Get hands-on with 1200+ tech skills courses.