Home >Java >javaTutorial >How to check if a number is Strong's number in Java?
A number is called a Strontio number and if we multiply 2 with a four-digit number, the tens and hundreds digits of the resulting number are the same.
To put it simply, a Strontio number is basically a four-digit number. When we multiply by 2, the result is a number with exactly the same tens and hundreds digits.
Some examples of Strontio numbers include - 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999, 1001...etc.
In this article, we will learn how to check if a number is strong using Java programming language.
Example 1
The input number is 1111.
Let’s check it out using the logic of Strontio numbers.
Multiply the given number by 2= 1111 * 2 = 2222.
As we noticed here, the resulting numbers in hundreds and tens are the same.
Therefore, 1111 is a Strontio number.
Example 2
The input number is 7777.
Let’s check it out using the logic of Strontio numbers.
Multiply the given number by 2= 7777 * 2 = 15554.
As we noticed here, the resulting numbers in hundreds and tens are the same.
Therefore, 7777 is a Strontio number.
Example 3
The input number is 1234.
Let’s check it out using the logic of Strontio numbers.
Multiply the given number by 2= 1234 * 2 = 2468.
As we noticed here, the resulting numbers in hundreds and tens are not the same.
Therefore, 1234 is not a Strontio number.
Step 1 - Get the four-digit input number via static input or user input.
Step 2 - Multiply the input number by 2 and remove all digits except the hundreds and tens digits from the resulting number.
Step 3 - Now we have a two digit number, so we check if the two digits of the number are the same.
Step 4 - If both numbers are the same, the number entered is called a Strontio number, otherwise it is not.
We provide solutions in different ways.
By using static input values
By using user input value
By user-defined method
Let’s look at the program and its output one by one.
In this method we declare a variable with a static input value and then by using an algorithm we can check if the number is a Strontio number.
import java.util.*; public class Main { public static void main(String args[]) { //declare a variable and store a number to it by static input method int inputNumber=9999; //declare a variable to store the input value temporarily int temp=inputNumber; //remove all the digits excluding hundreds and tens place digit inputNumber=(inputNumber*2%1000)/10; //Now compare hundreds place value with tens place value if(inputNumber%10==inputNumber/10) //if true System.out.println(temp+ " is a strontio number."); else //if false System.out.println(temp+ " is not a strontio number."); } }
9999 is a strontio number.
In this method we take a four digit number as user input and then by using an algorithm we can check if the number is a Strontio number.
import java.util.*; public class Main { public static void main(String args[]) { //create object of Scanner Class Scanner sc=new Scanner(System.in); //Ask the user to enter a four digit number System.out.print("Enter a four digit number: "); //Declare a variable and store the collected number int inputNumber=sc.nextInt(); //declare a variable to store the input value temporarily int temp=inputNumber; //remove all the digits excluding hundreds and tens place digit inputNumber=(inputNumber*2%1000)/10; //Now compare hundreds place value with tens place value if(inputNumber%10==inputNumber/10) //if true System.out.println(temp+ " is a strontio number."); else //if false System.out.println(temp+ " is not a strontio number."); } }
Enter a four digit number: 2222 2222 is a strontio number.
In this method, a four digit number is taken as a static input and the number is passed as a parameter to the user defined method and then using an algorithm inside the method we can check if the number is a Strontio number.
import java.util.*; public class Main { //main method public static void main(String args[]) { //declare a variable and store a number to it by static input method int inputNumber=3333; //call the user-defined method to check the strontio number if(checkStrontio(inputNumber)) //print if condition true System.out.println(inputNumber+ " is a strontio number."); else //print if condition false System.out.println(inputNumber+ " is not a strontio number."); } //user-defined method to check the strontio number static boolean checkStrontio(int n) { //declare a variable to store the input value temporarily int temp=n; //remove all the digits excluding hundreds and tens place digit n=(n*2%1000)/10; //Now compare hundreds place value with tens place value if(n%10==n/10) //if true return true; else //if false return false; } }
3333 is a strontio number.
In this article, we explored how to check if a number is a Strontio number in Java using different methods.
The above is the detailed content of How to check if a number is Strong's number in Java?. For more information, please follow other related articles on the PHP Chinese website!