Coding Exercise: Extension Methods

Test yourself on the extension method.

Problem

This activity uses an extension method to determine if a number is lucky. Create a console application called Lucky Number that meets the requirements below.

  1. Display the prompt: Enter a number to find out if it is lucky:
  2. Create an extension method for an int that returns a string, Lucky or Not Lucky (in the example below, the lucky numbers are 7 and 13).
  3. Display whether or not the number is lucky.

Sample input and output

The finished output of the program should look like the examples below:

Example 1:

Enter a number to find out if it is lucky: 7
The number 7 is lucky!

Example 2:

Enter a number to find out if it is lucky: 12
The number 12 is NOT lucky!

Get hands-on with 1200+ tech skills courses.