Java
 Computer >> コンピューター >  >> プログラミング >> Java

アームストロング数をチェックするJavaプログラム


この記事では、指定された番号がアームストロング番号であるかどうかを確認する方法を理解します。アームストロング数は、それ自体の桁の立方体の合計に等しい数です。

整数は、すべての桁が分離され、3乗されて合計される場合、n次のアームストロング数と呼ばれます。合計は数値と同じになります。つまり、abcd ... =a3 + b3 + c3 + d3 + ...

>

アームストロング数が3桁の場合、各桁の立方体の合計はその数自体に等しくなります。例:153はアームストロング数です。

153 = 13 + 53 + 33

例:371はアームストロング数です。

以下は同じのデモンストレーションです-

入力

入力が-

であると仮定します
Enter the number : 407

出力

必要な出力は-

になります
407 is an Armstrong number

アルゴリズム

Step 1 - START
Step 2 - Declare four integer values namely my_input, my_temp, my_remainder, my_result
Step 3 - Read the required values from the user/ define the values
Step 4 - Run a while loop to check Armstrong numbers using %, / and * operator
Step 5 - Divide by 10 and get remainder for ‘check’ .
Step 6 - Multiply ‘rem’ thrice, and add to ‘sum’, and make that the current ‘sum’.
Step 7 - Divide ‘check’ by 10, and make that the current ‘check’. Store the resultant value.
Step 8 - If the resultant value is equal to the input value, the input value is an Armstrong number, else it’s not an Armstrong number
Step 9 - Display the result
Step 10- Stop

例1

ここでは、プロンプトに基づいてユーザーが入力を入力しています。この例は、コーディンググラウンドツールでライブで試すことができます アームストロング数をチェックするJavaプログラム

import java.util.Scanner;
public class IsArmstrong {
   public static void main(String[] args) {
      int my_input, my_temp, my_remainder, my_result;
      my_result = 0;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the number : ");
      my_input = my_scanner.nextInt();
      my_temp = my_input;
      while (my_temp != 0){
         my_remainder = my_temp % 10;
         my_result += Math.pow(my_remainder, 3);
         my_temp /= 10;
      }
      if(my_result == my_input)
         System.out.println(my_input + " is an Armstrong number");
      else
         System.out.println(my_input + " is not an Armstrong number");
   }
}

出力

Required packages have been imported
A reader object has been defined
Enter the number : 407
407 is an Armstrong number

例2

ここでは、整数は事前に定義されており、その値にアクセスしてコンソールに表示されます。

public class IsArmstrong {
   public static void main(String[] args) {
      int my_input, my_temp, my_remainder, my_result;
      my_input = 407;
      my_result = 0;
      System.out.println("The number is defined as " +my_input);
      my_temp = my_input;
      while (my_temp != 0){
         my_remainder = my_temp % 10;
         my_result += Math.pow(my_remainder, 3);
         my_temp /= 10;
      }
      if(my_result == my_input)
         System.out.println(my_input + " is an Armstrong number");
      else
         System.out.println(my_input + " is not an Armstrong number");
   }
}

出力

The number is defined as 407
407 an Armstrong number

  1. 回文をチェックするJavaプログラム

    回文数は、逆にしたときに同じままの数です。たとえば、121、313、525などです。 例 回文をチェックする例を見てみましょう- public class Palindrome {    public static void main(String[] args) {       int a = 525, revVal = 0, remainder, val;       val = a;       System.out.println("Number to be che

  2. アームストロング数をチェックするPythonプログラム

    この記事では、特定の問題ステートメントを解決するための解決策とアプローチについて学習します。 問題の説明 整数nが与えられた場合、与えられた整数がアームストロング数であることを確認する必要があります。 正の整数は、次の場合、n次のアームストロング数と呼ばれます abcd... = a^n + b^n + c^n + d^n + … ここでは、3桁のアームストロング数、つまり3桁のブルートフォースアプローチについて説明します。 オーダーnのアームストロング番号を確認するには、3を行番号7の対応するオーダー値に置き換える必要があります。 それでは、実装を見てみましょう- 例