Java 如何检查一个数字是否为Tech数字

Java 如何检查一个数字是否为Tech数字

当一个偶数位数字被分成两部分并且这两个数字的和的平方等于原始数字时,该数字被称为Tech数字。

为了更清楚地解释,我们取一个有偶数位数字的数字。然后,将其分成两部分并相加。得到和之后,我们计算它的平方。现在我们比较计算得到的平方值和原始/输入的数字。如果两者相同,则可以说输入的数字是Tech数字,否则不是。

一些Tech数字的示例是:2025,3025,9801,…等等。

在本文中,我们将使用Java编程语言来检查一个数字是否为Tech数字。

为了向您展示一些实例

实例-1

输入的数字是2025。

我们将使用Tech数字的逻辑来检查它。

2025中可用的数字位数为4,这是偶数。

将数字分成两部分=20和25。

20和25相加=20 + 25 = 45

找到45的平方=(45)^2=2025

正如我们在这里注意到的,计算得到的平方值和原始数字都是相同的。

因此,2025是一个Tech数字。

实例-2

输入的数字是3025。

我们将使用Tech数字的逻辑来检查它。

3025中可用的数字位数为4,这是偶数。

将数字分成两部分=30和25。

30和25相加=30 + 25 = 55

找到55的平方=(55)^2=3025

正如我们在这里注意到的,计算得到的平方值和原始数字都是相同的。

因此,3025是一个Tech数字。

实例-3

输入的数字是4020。

我们将使用Tech数字的逻辑来检查它。

4020中可用的数字位数为4,这是偶数。

将数字分成两部分=40和20。

40和20相加=40 + 20 = 60

找到60的平方=(60)^2=3600

正如我们在这里注意到的,计算得到的平方值和原始数字不相同。

因此,4020不是一个Tech数字。

步骤

步骤1 - 通过静态输入方法获得输入的数字。

步骤2 - 计算原始数字中的数字位数。

步骤3 - 如果数字的位数是偶数,则将其分成两部分,并将这两个数字存储在两个不同的变量中。

步骤4 - 然后求出这两个数的和,然后计算该和值的平方。

步骤5 - 如果计算出的平方值和输入的数字相等,则输入的数字被称为技术数,否则不是。

语法

为了得到Java中一个数字的另一个数字的幂,我们可以使用内置的 java.lang.Math.pow() 方法。

使用该方法获取2的幂的语法如下 –

double power = Math.pow (inputValue,2)

多种方法

我们提供了多种解决方案。

  • 通过使用静态输入值

  • 通过使用用户定义的方法

让我们逐个看一下程序及其输出。

方法1:通过使用静态输入值

在这种方法中,我们声明一个变量并将其初始化为一个偶数位正数,然后通过使用算法,我们可以检查该数字是否为Tech数字。

示例

public class Main {
   public static void main(String[] args) {

      //declaring the variables
      int inputNumber, temp, firstNumber, secondNumber, count = 0,res = 0;

      //Declare and initialize the original input number
      inputNumber = 3025;

      //store the original number into a temporary variable
      temp = inputNumber;

      //find the number of digits available in input number
      while (temp > 0) {
         count++;
         temp = temp / 10;
      }


      //check whether the number of digits is ever or not

      //if even number of digits are present then proceed to check Tech number
      if (count % 2 == 0) {
         temp = inputNumber;

         //find the first part and store it into a variable
         firstNumber = temp % (int) Math.pow(10, count / 2);

         //find the second part and store it into a variable
         secondNumber = temp / (int) Math.pow(10, count / 2);

         //calculate the square of sum of those two parts of input number
         res = (int) Math.pow((firstNumber + secondNumber), 2);
         if (inputNumber == res) {

            //if the original number is equal to calculated value
            System.out.println(inputNumber+" is a Tech Number.");
         }
         else {

            //if the original number is not equal to calculated value
            System.out.println(inputNumber+" is not a Tech Number.");
         }
      }
      else {

         //if the input number has odd number of digits
         System.out.println(inputNumber+" is not a Tech Number.");
      }
   }
}

输出

3025 is a Tech Number.

方法2:使用用户定义的方法

在这种方法中,我们声明一个变量,并将其初始化为一个正数,然后通过将这个数字作为参数调用一个用户定义的方法,通过使用算法来判断这个数字是否为Tech数。

示例

public class Main {

   //main method
   public static void main(String[] args) {

      //delare a variable and initialize a value to it
      int inputNumber= 3025;

      //call the user defined method to check the tech number
      if (checkTech(inputNumber)) {
         System.out.println(inputNumber+" is a Tech Number.");
      }
      else {
         System.out.println(inputNumber+" is not a Tech Number.");
      }
   }

   //user defined method for checking of Tech Number
   public static boolean checkTech(int n) {

      //declaring the variables
      int temp, firstNumber, secondNumber, count = 0,res = 0;

      //store the original number into a temporary variable
      temp = n;

      //loop to find the number of digits available in input number
      while (temp > 0) {
         count++;
         temp = temp / 10;
      }

      //check whether the number of digits is ever or not
      if (count % 2 == 0) {
         temp = n;

         //find the first part and store it into a variable
         firstNumber = temp % (int) Math.pow(10, count / 2);

         //find the second part and store it into a variable
         secondNumber = temp / (int) Math.pow(10, count / 2);

         //calculate the square of sum of those two parts of input number
         res = (int) Math.pow((firstNumber + secondNumber), 2);
         if (n == res) {

            //if the original number is equal to calculated value
            return true;
         }
         else {

            //if the original number is not equal to calculated value
            return false;
         }
      }
      else {

         //if the input number has odd number of digits
         return false;
      }
   }
}

输出

3025 is a Tech Number.

在这篇文章中,我们探讨了如何使用不同的方法在Java中检查一个数字是否为Tech数字。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程