如何在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數。
示例
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數。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP