Java程式檢查阿姆斯特朗數


在數論中,阿姆斯特朗數是一種基於模式的數字,其每個數字的總位數次冪之和等於該數字本身。因此,要檢查一個數字是否為阿姆斯特朗數,首先確定數字的總位數,並將其假設為“n”。然後分離每個數字,並將它們提升到“n”的冪。在最後一步,計算每個數字的冪,並將它們全部加起來。如果我們得到的總和等於原始數字,則它是一個阿姆斯特朗數,否則不是。本文旨在解釋如何在Java中檢查給定數字是否為阿姆斯特朗數。

Java程式檢查阿姆斯特朗數

在本節中,我們將編寫兩個Java程式來識別數字是否為阿姆斯特朗數。在此之前,讓我們藉助示例討論問題陳述 -

示例

輸入1

1634

輸出

1634 is an Armstrong number

解釋

1^4 + 6^4 + 3^4 + 4^4 = 1634, sum of powers and the number both are same.

輸入2

525

輸出

525 is not an Armstrong number 

解釋

5^3 + 2^3 + 5^3 = 258, sum of powers and the number both are not same.

現在,讓我們進入Java程式,以檢查給定數字是否為阿姆斯特朗數。

示例1:檢查阿姆斯特朗數

在以下示例中,我們將使用Scanner類從使用者那裡獲取輸入數字,並檢查給定數字是否為阿姆斯特朗數。

方法

  • 首先,宣告一個變數來儲存數字的冪之和。

  • 然後,建立一個Scanner類的例項,以從使用者那裡獲取整數輸入。

  • 將原始輸入複製到另一個變數。這是必要的,因為原始數字將在下一個while迴圈結束時遞減到0。

  • 現在,將整數輸入轉換為字串,以便我們可以使用for迴圈遍歷數字的每個數字。

  • 使用一個while迴圈,該迴圈將在原始數字變為0之前執行。我們將在此迴圈中放入檢查阿姆斯特朗數的邏輯。

  • 最後,檢查每個數字的冪之和是否等於原始數字。

import java.util.*;
public class IsArmstrong {
   public static void main(String[] args) {
      // variable to store sum of powers
      int sum = 0;
      // creating instance of Scanner class
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a number to check Armstrong number: ");
      // to take input number
      int num = sc.nextInt();
      // copying the input to variable named copy
      int copy = num;
      // converting integer to string
      String n = Integer.toString(num);
      // storing the length of converted string
      int len = n.length();
      // loop to check armstrong number
      while(num != 0) {
         int rem = num % 10;
         int mul = 1;
         for(int i = 1; i <= len; i++) {
            mul *= rem;
         }
         sum += mul;
         num /= 10;
      }
      // to print the result
      if(sum == copy) {
         System.out.println(copy + " is an Armstrong number");
      } else {
         System.out.println(copy + " is not an Armstrong number");
      }
   }
}

輸出1

Enter a number to check Armstrong number: 
525
525 is not an Armstrong number

輸出2

Enter a number to check Armstrong number: 
1634
1634 is an Armstrong number

示例2:檢查阿姆斯特朗數

這是另一個檢查阿姆斯特朗數的示例,其中我們將在宣告時初始化輸入數字,而不是從使用者那裡獲取輸入。

import java.util.*;
public class IsArmstrong {
   public static void main(String[] args) {
      int sum = 0;
      int num = 153;
      int copy = num;
      System.out.println("The number defined to check Armstrong is: " + num);
      String n = Integer.toString(num);
      int len = n.length();
      while(num != 0) {
         int rem = num % 10;
         int mul = 1;
         for(int i = 1; i <= len; i++) {
            mul *= rem;
         }
         sum += mul;
         num /= 10;
      }
      if(sum == copy) {
         System.out.println(copy + " is a Armstrong number");
      } else {
         System.out.println(copy + " is not an Armstrong number");
      }
   }
}

輸出

The number defined to check Armstrong is: 153
153 is an Armstrong number

結論

在本文中,我們瞭解了什麼是阿姆斯特朗數以及如何檢查給定數字是否為阿姆斯特朗數。為此,我們在Java程式中使用了while和for迴圈以及if-else條件。

更新於: 2024年6月21日

11K+瀏覽量

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.