如何在 Java 中檢查一個數是否為哈沙德數?


哈沙德數可以定義為一個可以被其各位數字之和整除的數。簡單來說,如果一個數的各位數字之和是該數的一個因子,那麼它就是一個哈沙德數。

在本文中,我們將瞭解如何使用 Java 程式語言來檢查哈沙德數。

舉幾個例子:

示例 1

輸入數字為 18

讓我們使用哈沙德數的邏輯來檢查它:

數字的各位數字之和 = 1 + 8 = 9。

所以,18 可以被 9 整除。

因此,18 是一個哈沙德數。

示例 2

輸入數字為 3

讓我們使用哈沙德數的邏輯來檢查它:

數字的各位數字之和 = 3。

所以,3 可以被 3 整除。

因此,3 是一個哈沙德數。

示例 3

輸入數字為 15

讓我們使用哈沙德數的邏輯來檢查它:

數字的各位數字之和 = 1 + 5 = 6。

所以,15 不能被 6 整除。

因此,14 不是一個哈沙德數。

其他一些哈沙德數的例子包括 1、2、4、5、6、7、8、9、10、12、18、20 等。

語法

使用內建的 toString() 方法將整數轉換為字串。

以下是透過將整數轉換為字串,然後查詢其長度並將其長度賦給一個整數變數來獲取數字中總位數的語法:

String str = Integer.toString(input_number);

要獲取整數的長度,我們將使用 Java String 類內建的 length() 方法,該方法返回 String 物件的長度。

int length = st.length();

要獲取字串中特定位置/索引處的字元,我們使用 charAt() 方法。其中 charAt(i)-‘0’ 返回實際的整數值。

int num = st.charAt(i)-‘0’;

其中 ‘st’ 指的是字串,‘i’ 是迭代字串的迭代器變數。

演算法

演算法 1

  • 步驟 1 - 獲取一個整數,可以透過初始化或使用者輸入獲取。

  • 步驟 2 - 透過迭代數字的每一位,找到數字每一位的和。

  • 步驟 3 - 然後檢查原始數字是否可以被所有數字之和整除。如果可以整除,則給定數字是哈沙德數,否則不是哈沙德數。

演算法 2

  • 步驟 1 - 獲取一個整數,可以透過初始化或使用者輸入獲取。

  • 步驟 2 - 使用內建的 toString() 方法將該整數轉換為字串。

  • 步驟 3 - 使用內建的 length() 方法查詢字串的長度。

  • 步驟 4 - 然後使用 for 迴圈,迭代到字串的長度,並使用 charAt(i)-‘0’ 從字串中獲取一位一位的整數值,並跟蹤所有數字的和。

  • 步驟 5 - 然後檢查原始數字是否可以被所有數字之和整除。如果可以整除,則給定數字是哈沙德數,否則不是哈沙德數。

多種方法

我們提供了不同方法的解決方案

  • 不使用字串

  • 使用字串

讓我們逐一檢視程式及其輸出。

方法 1:不使用字串

在這種方法中,將在程式中初始化一個整數值,然後使用 演算法 1 檢查一個數是否為哈沙德數。

示例

public class Main{ //main method public static void main(String args[]){ //Declared an integer variable and initialized a number as value int originalNumber = 21; //printing the given number to be checked System.out.println("Given number: "+originalNumber); //keep a copy of original number int copyOfOriginalNumber = originalNumber; //initialize sum as 0 int sum = 0; //Find sum of all digits of the number //continue the loop till the number is greater than 0 while(originalNumber > 0){ //get the rightmost digit of the number by using % operator int rem = originalNumber%10; //add the digit(rem) to sum sum = sum + rem; //remove the rightmost digit from number and get the updated number originalNumber = originalNumber/10; } //printing the result if(copyOfOriginalNumber % sum == 0) System.out.println(copyOfOriginalNumber+" is a Harshadnumber"); else System.out.println(copyOfOriginalNumber+" is not a Harshadnumber"); } }

輸出

Given number: 21
21 is a Harshad number

方法 2:使用字串

在這種方法中,將在程式中初始化一個整數值,然後使用演算法 2 檢查該數是否為哈沙德數。

示例

public class Main{ //main method public static void main(String args[]){ //Declared an integer variable and initialized a number as value int originalNumber = 40; //printing the given number to be checked System.out.println("Given number: "+originalNumber); //keep a copy of original number int copyOfOriginalNumber = originalNumber; //initialize sum as 0 int sum = 0; //convert the integer to string by using toString() method String str = Integer.toString(originalNumber); //find length of String by using length() method //which is nothing but total number of digits in the given number int length=str.length(); //iterate the String and get the digits by using charAt(i)-'0' //find the sum of digits for(int i = 0; i < length; i++){ sum += str.charAt(i)-'0'; } //printing the result if(copyOfOriginalNumber % sum == 0) System.out.println(copyOfOriginalNumber+" is a Harshad number"); else System.out.println(copyOfOriginalNumber+" is not a Harshad number"); } }

輸出

Given number: 40
40 is a Harshad number

在本文中,我們探討了如何使用不同的方法在 Java 中檢查一個數是否為哈沙德數。

更新於: 2022 年 10 月 27 日

2K+ 次檢視

啟動你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.