In this tutorial you will learn about the Program to check Nelson Number in Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to check Nelson Numbers in Java using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
What is Nelson Number
The number “111” is called “Nelson Number” after the name of sir “Admiral Nelson“,because he had only had “One Eye, One Arm, One Leg” at the end of his life.Double of Nelson number called Double Nelson (222), triple Nelson (333), and so on..
Program to check Nelson Number in Java
In this our program we will find given number is a Nelson number or not in using java programming. We would first declared and initialized the required variables. Next, we would prompt user to input the a number.Later we find number is Nelson number or not let’s have a look at the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
//Java Program to fin Nelson number import java.util.Scanner; public class Nelson_Number { public static void main(String[] args) { // Declaring variable and taking values... Scanner in = new Scanner(System.in); System.out.println("Please enter a number"); int number=in.nextInt(); // Checking condition for nelson number if(number==111 || number==222 || number==333 || number==444 || number==555 || number==666 || number==777 || number==888 || number==999) { System.out.println("It is Nelson Number "+number); } else { System.out.println("It is Not a Nelson Number"+number); } } } |
Output
Nelson number.
Not a nelson number.
In the above program, we have first declared and initialized a set variables required in the program.
- number= it will hold entered number.
After declaring variables in the next statement user will be prompted to enter a value and which will be assigned to variable ‘number’.
And after that we check condition for Nelson number if condition get satisfied it means the given number is Nelson number if not then it is not Nelson number.
Condition for Nelson number.
If the above Condition satisfied the given number by user is a Nelson number.
If not then the given number is not a Nelson number.