如何在Java中檢查一個數是否為間諜數?
如果給定數字的各位數字之和等於給定數字的各位數字之積,則稱該數字為間諜數。
為了更清楚地說明,我們首先需要計算給定數字的各位數字之和。然後,我們需要計算給定數字的各位數字之積。然後,我們需要比較這兩個和值和積值,如果它們相同,則給定數字是間諜數,否則不是間諜數。
在本文中,我們將瞭解如何使用Java程式語言來檢查一個數是否為間諜數。
舉幾個例子
示例1
輸入數字為123。
讓我們使用間諜數的邏輯來檢查它。
The sum of the digits of 123 = 1 + 2 + 3 = 6 The product of the digits of 123 = 1 * 2 * 3 = 6
我們注意到這裡的和值和積值相同。
因此,123是間諜數。
示例2
輸入數字為22。
讓我們使用間諜數的邏輯來檢查它。
The sum of the digits of 22 = 2 + 2 = 4 The product of the digits of 22 = 2 * 2 = 4
我們注意到這裡的和值和積值相同。
因此,22是間諜數。
示例3
輸入數字為234。
讓我們使用間諜數的邏輯來檢查它。
The sum of the digits of 234 = 2 + 3 + 4 = 9 The product of the digits of 234 = 2 * 3 * 4 = 24
我們注意到這裡的和值和積值不同。
因此,234不是間諜數。
演算法
步驟1 − 透過初始化或使用者輸入獲取一個整數。
步驟2 − 然後使用迴圈提取數字並計算和值和積值。
步驟3 − 最後,當迴圈結束時,您將得到總和值和積值。
步驟4 − 如果和值和積值彼此相等,則我們可以列印結果,即給定數字是間諜數,否則該數字不是間諜數。
多種方法
我們提供了兩種不同的方法來解決這個問題。
使用靜態輸入值
使用使用者定義的方法
讓我們逐一檢視程式及其輸出。
方法1:使用靜態輸入值
在這種方法中,程式中將初始化一個整數值,然後使用演算法檢查該數是否為間諜數。
示例
public class Main { public static void main(String args[]) { //declare the variables int inputNumber,org, productValue=1, sumValue=0, digit; //initialize the value to the variable inputNumber=123; //store that value to another variable for result print org=inputNumber; //continue the loop till the number is not zero while(inputNumber>0) { //extracting the unit place's digit digit=inputNumber%10; //continuously add that digit to sum variable sumValue=sumValue+digit; //continuously multiply the digit to the product variable productValue=productValue*digit; //removes that digit from the inputNumber inputNumber=inputNumber/10; } //check the condition //whether the sum value is equal to product value or not if(sumValue==productValue) //prints if the condition returns true System.out.println(org + " is a spy number."); else //prints if the condition returns false System.out.println(org + " is not a spy number."); } }
輸出
123 is a spy number.
方法2:使用使用者定義的方法
在這種方法中,程式中將初始化一個整數值,然後我們將呼叫一個使用者定義的方法,並將此輸入數字作為引數傳遞。
在方法內部,我們將使用演算法檢查該數是否為達芬奇數。
示例
public class Main { //main method public static void main(String args[]) { //declare an int variable and initialize number as value int inp = 123; //call the method and return the value to the if condition if(checkSpy(inp)) System.out.println(inp + " is a spy number."); else System.out.println(inp + " is not a spy number."); } //user defined method to check spy number public static boolean checkSpy(int inputNumber) { //declare the variables int productValue=1, sumValue=0, digit; //continue the loop till the number is not zero while(inputNumber>0) { //extracting the unit place's digit digit=inputNumber%10; //continuously add that digit to sum variable sumValue=sumValue+digit; //continuously multiply the digit to the product variable productValue=productValue*digit; //removes that digit from the inputNumber inputNumber=inputNumber/10; } //check the condition //whether the sum value is equal to product value or not if(sumValue==productValue) return true; else return false; } }
輸出
123 is a spy number.
在本文中,我們探討了如何使用三種不同的方法在Java中檢查一個數是否為間諜數。
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP