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

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:通过使用静态输入值

在这种方法中,程序将初始化一个整数值,然后通过使用算法来检查一个数是否是间谍数。

示例

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:使用用户自定义方法

在这种方法中,程序将初始化一个整数值,然后我们将通过将该输入数字作为参数来调用用户自定义方法。

在方法内部,我们将使用算法来检查一个数字是否为 Disarium数

示例

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中的间谍数字。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程