How To Check Whether a Number is Strontio Number or Not in Java?



A number is said to be a Strontio number, if we multiply 2 with a four-digit number then in the resultant number the tens place and hundreds place digits are the same.

In simple terms, Strontio numbers are basically four-digit numbers. When we multiply 2 with it, it results in a number whose tens place and hundred places are exactly the same.

Some examples of Strontio numbers are- 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999, 1001 ... etc.

In this article, we will see how to check if a number is a strontio number by using Java programming language.

To show you some instances

Instance-1

Input number is 1111.

Let’s check it by using the logic of Strontio number.

Multiply 2 with the given number= 1111 * 2 = 2222.

As we notice here both the hundreds place and tens place are same in resultant number.

Hence, 1111 is a Strontio number.

Instance-2

Input number is 7777.

Let’s check it by using the logic of Strontio number.

Multiply 2 with the given number= 7777 * 2 = 15554.

As we notice here both the hundreds place and tens place are same in resultant number.

Hence, 7777 is a Strontio number.

Instance-3

Input number is 1234.

Let’s check it by using the logic of Strontio number.

Multiply 2 with the given number= 1234 * 2 = 2468.

As we notice here both the hundreds place and tens place are not same in resultant number.

Hence, 1234 is not a Strontio number.

Algorithm

Step-1 - Get the four-digit input number either by static input or by user input.

Step-2 - Multiply the input number with 2 and remove all digits from the resultant number except hundreds place and tens place.

Step-3 - Now we have a two-digit number, so we check whether both digits of that number are the same or not.

Step-4 - If both the digits are same, then the input number is called as Strontio number else not.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Input Value

  • By Using User Input Value

  • By User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Input Value

In this approach, we declare a variable with a static input value and then by using the algorithm we can check whether the number is a Strontio number or not.

Example

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.");
   }
}

Output

9999 is a strontio number.

Approach-2: By Using User Input Value

In this approach, we take a four-digit number as user input and then by using the algorithm we can check whether the number is a Strontio number or not.

Example

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.");
   } 
}

Output

Enter a four digit number: 2222
2222 is a strontio number.

Approach-3: By Using User Defined Method

In this approach, take a four-digit number as static input and pass this number as a parameter in a user defined method, then inside the method by using the algorithm we can check whether the number is a Strontio number or not.

Example

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;
   }
}

Output

3333 is a strontio number.

In this article, we explored how to check a number whether it is a Strontio number or not in Java by using different approaches.


Advertisements