如何在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