Home > Java > javaTutorial > body text

How to check if a number is Strong's number in Java?

PHPz
Release: 2023-08-26 18:21:03
forward
1035 people have browsed it

How to check if a number is Strongs 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.

Show you some examples

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.

algorithm

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.

Multiple methods

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.

Method 1: Using static input values

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.

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

Output

9999 is a strontio number.
Copy after login

Method 2: Using user input values

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.

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

Output

Enter a four digit number: 2222
2222 is a strontio number.
Copy after login

Method 3: Using user-defined methods

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.

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;
   }
}
Copy after login

Output

3333 is a strontio number.
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!