Java 如何检查一个数是否是自恋数

Java 如何检查一个数是否是自恋数

如果每个数字的幂值与原数的位数之和等于原数,则该数被称为自恋数。

更明确地说,如果原数有n个数字,首先需要计算每个数字的n次幂,然后将这些值相加,并检查计算得到的值和原数是否相等,如果相等,则原数被称为自恋数。

一些自恋数的示例包括:153, 8208, 4210818,等等。

在本文中,我们将看到如何使用Java编程语言来检查一个数是否是自恋数。

为了向您展示一些实例

实例1

输入的数是153。

我们使用自恋数的逻辑来检查它。

153有3个数字。

计算幂值并相加:(1^3) + (5^3) + (3^3) = 1 + 125 + 27 = 153

我们注意到这里计算得到的数和原数是相等的。

因此,153是一个自恋数。

实例2

输入的数是8208。

我们使用自恋数的逻辑来检查它。

8208有4个数字。

计算幂值并相加:(8^4) + (2^4) + (0^4) + (8^4) = 4096 + 16 + 0 + 4096 = 8208

我们注意到这里计算得到的数和原数是相等的。

因此,8208是一个自恋数。

实例3

输入的数是250。

我们使用自恋数的逻辑来检查它。

250有3个数字。

计算幂值并相加:(2^3) + (5^3) + (0^3) = 8 + 125 + 0 = 133

我们注意到这里计算得到的数和原数是不相等的。

因此,250不是一个自恋数。

步骤

步骤1 - 通过静态输入方法获取输入的数字。

步骤2 - 计算原数的位数。

步骤3 - 初始化一个循环来提取数字并计算其幂值。在循环内部将这些计算得到的幂值相加并将其存储在一个变量中。

步骤4 - 将计算得到的值与原数进行比较。

步骤-5 - 如果两个数相同,则输入的数字被称为自恋数,否则不是。

语法

要在Java中获得一个数的另一个数的幂,我们可以使用内置的 java.lang.Math.pow() 方法。

以下是通过使用该方法获得2的幂的语法 –

double power = Math.pow (inputValue,2)

多种方法

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

  • 通过使用静态输入值和用户定义方法

  • 通过使用用户定义的方法和用户定义的方法

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

方法1:通过使用静态输入值

在这个方法中,我们声明一个变量并用一个数字初始化它。通过将这个数字作为参数传递给用户定义的方法来调用该方法。然后,在方法内部,通过使用算法,我们可以检查这个数字是否是一个自恋数。

示例

import java.util.*; 
import static java.lang.Math.*;
public class Main {
   public static void main(String args[]) {

      //declare a variable and initialize it with a number
      int inputNumber = 8208;

      //call the function inside the if condition which checks narcissistic
      if (checkNarcissistic(inputNumber))
         System.out.println(inputNumber + " is a narcissistic number.");
      else
         System.out.println(inputNumber + " is not a narcissistic number.");
   }

   //user-defined method to count the digits
   static int countDig(int num) {
      if (num == 0)
         return 0;
         return 1 + countDig(num / 10);
   }

   //user-defined method to check narcissistic number
   static boolean checkNarcissistic(int num) {

      //declare a variable and store the counts of digits
      int p = countDig(num);

      //declare a variable and temporary store the input number
      int temp = num;

      //Declare a variable which store the sum value and initiate it with 0
      int sum = 0;

      //execute the loop
      while(temp > 0) {

         //extarct digits and find power of it

         //continuously add it to sum value
         sum+= pow(temp % 10, p);

         //removes the digit which is already calculated
         temp = temp / 10;
      }

      //return true if original number is equal to the sum value
      return (num == sum);
   }
}

输出

8208 is a narcissistic number.

方法2:使用静态输入值

在这种方法中,我们声明一个变量并通过用户输入为其赋值。通过将这个数字作为参数调用一个用户定义的方法。然后在方法内部,我们可以使用算法检查该数字是否是水仙花数。

示例

import java.util.*;
import static java.lang.Math.*;
public class Main {
   public static void main(String args[]) {

      //create object of Scanner class
      Scanner sc = new Scanner(System.in);

      //ask the user to enter a number
      System.out.print("Enter the number: ");

      //declare a variable and store the input value
      int inputNumber = sc.nextInt();

      //call the function inside the if condition which checks narcissistic
      if (checkNarcissistic(inputNumber))
         System.out.println(inputNumber + " is a narcissistic number.");
      else
         System.out.println(inputNumber + " is not a narcissistic number.");
   }

   //user-defined method to count the digits
   static int countDig(int num) {
      if (num == 0)
         return 0;
         return 1 + countDig(num / 10);
   }

   //user-defined method to check narcissistic number
   static boolean checkNarcissistic(int num) {

      //declare a variable and store the counts of digits
      int p = countDig(num);

      //declare a variable and temporary store the input number
      int temp = num;

      //Declare a variable which store the sum value and initiate it with 0
      int sum = 0;

      //execute the loop
      while(temp > 0) {

         //extarct digits and find power of it

         //continuously add it to sum value
         sum+= pow(temp % 10, p);

         //removes the digit which is already calculated
         temp = temp / 10;
      }

      //return true if original number is equal to the sum value
      return (num == sum);
   }
}

输出

Enter the number: 8208
8208 is a narcissistic number.

在本文中,我们探讨了如何使用不同的方法在Java中检查一个数字是否是一个自恋数。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程