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

関数を使用して間隔間のアームストロング数を表示するJavaプログラム


この記事では、関数を使用して区間間のアームストロング数を表示する方法を理解します。アームストロング数は、それ自体の桁の立方体の合計に等しい数です。

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

>

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

153 = 13 + 53 + 33

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

371 = 27 + 343 + 1

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

入力

入力が-

であると仮定します
The two inputs : 1 and 500

出力

必要な出力は-

になります
The Armstrong numbers are:
153 370 371 407

アルゴリズム

Step 1 - START
Step 2 - Declare three integer values namely my_low, my_high and i
Step 3 - Read the required values from the user/ define the values
Step 4 - Define a function IsArmstrong which takes an integer value returns Boolean value.
Step 5- In this function, Divide the input variable by 10 and get remainder for ‘check’ .
Step 6 - Then Multiply ‘my_rem thrice, and add to ‘my_sum’, and make that the current ‘my_sum.
Step 7 - Later ‘check’ by 10, and make that the current ‘check’. Compare the ‘my_sum’ with the function input ‘I’ and return true or false.
Step 8 - Using a for loop, iterate from my_low to my_high, for each number, call the function IsArmstrong. If true is returned , it is an Armstrong number, store the number
Step 9 - Display the result
Step 10 - Stop

例1

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

import java.util.Scanner;
public class ArmstrongNumbers {
   public static void main(String[] args) {
      int my_low, my_high, i;
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("Required packages have been imported");
      System.out.println("A scanner object has been defined ");
      System.out.println("Enter the first number :");
      my_low = my_scanner.nextInt();
      System.out.println("Enter the limit :");
      my_high = my_scanner.nextInt();
      System.out.println("The Armstrong numbers are :");
      for(i = my_low + 1; i < my_high; ++i) {
         if (IsArmstrong (i))
            System.out.print(i + " ");
      }
   }
   public static boolean IsArmstrong(int i) {
      int check, my_rem, my_sum;
      my_sum = 0;
      check = i;
      while(check != 0) {
         my_rem = check % 10;
         my_sum = my_sum + (my_rem * my_rem * my_rem);
         check = check / 10;
      }
      if(my_sum == i){
         return true;
      }
      return false;
   }
}

出力

Required packages have been imported
A scanner object has been defined
Enter the first number :
1
Enter the limit :
500
The Armstrong numbers are :
153 370 371 407

例2

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

public class ArmstrongNumbers {
   public static void main(String[] args) {
      int my_low, my_high, i;
      my_low = 1;
      my_high = 500;
      System.out.println("The starting and ending numbers are defined as " + my_low + " and " + my_high);
      System.out.println("The Armstrong numbers are :");
      for(i = my_low + 1; i < my_high; ++i) {
         if (IsArmstrong (i))
            System.out.print(i + " ");
      }
   }
   public static boolean IsArmstrong (int i) {
      int check, my_rem, my_sum;
      my_sum = 0;
      check = i;
      while(check != 0) {
         my_rem = check % 10;
         my_sum = my_sum + (my_rem * my_rem * my_rem);
         check = check / 10;
      }
      if(my_sum == i){
         return true;
      }
      return false;
   }
}

出力

The starting and ending numbers are defined as 1 and 500
The Armstrong numbers are :
153 0 371 407

  1. 再帰関数を使用して数値のGCDを見つけるCプログラム

    問題 Cプログラミング言語の再帰関数を使用して、指定された2つの数値の最大公約数(GCD)を見つけます。 解決策 再帰関数を使用して、指定された2つの数値の最大公約数(GCD)を見つけるための解決策は、次のとおりです- アルゴリズム 再帰関数を使用して、指定された2つの数値の最大公約数(GCD)を見つけるには、以下のアルゴリズムを参照してください。 ステップ1 −再帰関数を定義します。 ステップ2 −2つの整数aとbを読み取ります。 ステップ3 −再帰関数を呼び出します。 a. if i>j b. then return the function with parameter

  2. forループを使用して1からNまでのすべての素数を表示するCプログラム

    問題 1からnまでのすべての素数を表示するCプログラムを作成します。これは、実行時にユーザーが指定した値です。 解決策 1からnまでのすべての素数を表示するCプログラムは、実行時にユーザーが指定した値です- アルゴリズム 以下に示すのは、1からnまでのすべての素数を表示するアルゴリズムであり、実行時にユーザーが指定した値です。 ステップ1 −n値を読み取ります。 ステップ2 −カウントの初期化=0 ステップ3 − for i=2からn    a. for j = 1 to i    b. if i % j = 0