Javaプログラムで数値の累乗を計算する
この記事では、数値の累乗を計算する方法を理解します。数値の累乗は、ループを使用して計算され、それ自体に複数回乗算されます。
以下は同じのデモンストレーションです-
入力
入力が-
であると仮定しますNumber : 4 Exponent value : 5
出力
必要な出力は次のようになります。つまり、4 5
The result is 1024
アルゴリズム
Step 1 - START Step 2 – Declare two integer values namely my_input and my_exponent Step 3 - Read the required values from the user/ define the values Step 4 – Using a while loop, multiply the input value with itself for n number of times where n is the exponent value. Store the result. Step 5- Display the result Step 6- Stop
例1
ここでは、プロンプトに基づいてユーザーが入力を入力しています。この例は、コーディンググラウンドツールでライブで試すことができます
。
import java.util.Scanner;
public class Exponents {
public static void main(String[] args) {
int my_input , my_exponent;
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();
System.out.print("Enter the exponent value : ");
my_exponent = my_scanner.nextInt();
long my_result;
my_result = 1;
while (my_exponent != 0) {
my_result *= my_input;
--my_exponent;
}
System.out.println("The result is = " + my_result);
}
} 出力
Required packages have been imported A reader object has been defined Enter the number : 4 Enter the exponent value : 5 The result is = 1024
例2
ここでは、整数は事前に定義されており、その値にアクセスしてコンソールに表示されます。
public class Exponents {
public static void main(String[] args) {
int my_input , my_exponent;
my_input = 4;
my_exponent = 5;
System.out.println("The number is defined as " +my_input +" and the exponent is defined as " + my_exponent);
long my_result;
my_result = 1;
while (my_exponent != 0) {
my_result *= my_input;
--my_exponent;
}
System.out.println("The result is = " + my_result);
}
} 出力
The number is defined as 4 and the exponent is defined as 5 The result is = 1024
-
複利を計算するJavaプログラム
以下は、Javaの複利を計算するプログラムです- 例 import java.io.*; public class Demo{ public static void main(String args[]){ double princ = 456000, rt = 9.75, tm = 7; double comp_int = princ *(Math.pow((1 + rt / 100), tm)); System.out.println(&q
-
Javaで数を数えるプログラムを実装するにはどうすればよいですか?
プログラムはJLabelを使用します カウントラベルを保持するには、 JTextField 数値を保持するコンポーネントカウント 、 JButton 追加を作成するコンポーネント 、削除 およびリセット ボタン。追加ボタンをクリックすると、JTextFieldのカウントがインクリメントされます 投稿者 1 削除ボタンをクリックすると、カウントが「1」ずつ減らされます。 [リセット]ボタンをクリックすると、リセットされます 0へのカウント 。 例 import java.awt.*; import java.awt.event.*; import javax.swing.*; publ