如何在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 - 如果计算出的平方值和输入数字相同,则输入数字称为Tech数,否则不是。

语法

在Java中,我们有内置的java.lang.Math.pow()方法来获取任何数字的幂。

以下是使用该方法获取2的幂的语法:

double power = Math.pow (inputValue,2)

多种方法

我们提供了不同的方法来解决这个问题。

  • 使用静态输入值

  • 使用用户自定义方法

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

方法-1:使用静态输入值

在这种方法中,我们声明一个变量并用一个偶数位的正数初始化它,然后使用算法来检查该数字是否是Tech数。

示例

Open Compiler
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数。

示例

Open Compiler
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数。

更新于:2022年12月9日

7K+ 次浏览

启动你的职业生涯

完成课程获得认证

开始学习
广告