Problem: Write a Java program to check whether the given number is a Nelson number or not.

In cricket, the number 111 is sometimes called “a Nelson” after Admiral Nelson, who allegedly only had “One Eye, One Arm, One Leg” near the end of his life. This is, in fact, inaccurate—Nelson never lost a leg.

Wikipedia

The number 111 is called a nelson number and multiples of nelson are called double Nelson (222), triple Nelson (333), and so on.

It is important to note that only few multiples of 111 are called Nelson numbers. These numbers are 111, 222, 333, 444, 555, 666, 777, 888, and 999.

Any other number divisible by 111 are not Nelson.

To check the nelson number in Java, we match the input number with the above valid nelson numbers and output the result accordingly.

import java.util.Scanner;
 
public class Main
{
    public static void main(String[] args) {
    	Scanner in = new Scanner(System.in);
    	
    	System.out.println("Enter a number");
    	int n=in.nextInt();
    		
    	if(n==111 || n==222 || n==333 || n==444 ||
    	   n==555 || n==666 || n==777 || n==888 || n==999){
            System.out.println("Nelson Number");
        } else {
            System.out.println("Not a Nelson Number");
    	} 
    }
}

Output:

Enter a number
111
Nelson Number

That’s all we need to write in Java to check whether the given number is Nelson or not. If you have any suggestions or doubts then please comment below.

Leave a Reply

4 × 3 =