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

商と剰余を計算するJavaプログラム


この記事では、Javaで商とリマインダーを計算する方法を理解します。商とリマインダーは、2つの簡単な式「商=配当/除数」と「剰余=配当%除数」を使用して計算されます。

整数aと非ゼロの整数dが与えられると、a =qd+rおよび0≤r<|d|のように、一意の整数qおよびrが存在することを示すことができます。数qは商と呼ばれ、rは剰余と呼ばれます。

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

商と剰余を計算するJavaプログラム

入力

入力が-

であると仮定します
Dividend value: 50
Divisor: 3

出力

必要な出力は-

になります
Quotient: 16
Remainder: 2

アルゴリズム

Step1- Start
Step 2- Declare four integers as my_dividend , my_divisor, my_quotient, my_remainder
Step 3- Prompt the user to enter two integer value that is my_dividend , my_divisor or define
the integers
Step 4- Read the values
Step 5- Use the formula to find the quotient and the reminder "Quotient = Dividend /
Divisor" and "Remainder = Dividend % Divisor"
Step 6- Display the result
Step 7- Stop

例1

ここでは、プロンプトに基づいてユーザーが入力を入力しています。この例は、コーディンググラウンドツールでライブで試すことができます 商と剰余を計算するJavaプログラム

import java.util.Scanner;
public class RemainderQuotient {
   public static void main(String[] args) {
      int my_dividend , my_divisor, my_quotient, my_remainder;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A scanner object has been defined ");
      System.out.print("Enter the value of dividend : ");
      my_dividend = my_scanner.nextInt();
      System.out.print("Enter the value of divisor : ");
      my_divisor = my_scanner.nextInt();
      my_quotient = my_dividend / my_divisor;
      my_remainder = my_dividend % my_divisor;
      System.out.println("The quotient is " + my_quotient);
      System.out.println("The remainder is " + my_remainder);
   }
}

出力

Required packages have been imported
A reader object has been defined
Enter the value of dividend : 50
Enter the value of divisor : 3
The quotient is 16
The remainder is 2

例2

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

public class RemainderQuotient {
   public static void main(String[] args) {
      int my_dividend , my_divisor, my_quotient, my_remainder;
      my_dividend = 50;
      my_divisor = 3;
      System.out.println("The divident and the divisor are defined as " +my_dividend +" and " +my_divisor);
      my_quotient = my_dividend / my_divisor;
      my_remainder = my_dividend % my_divisor;
      System.out.println("The quotient is " + my_quotient);
      System.out.println("The remainder is " + my_remainder);
   }
}

出力

The divident and the divisor are defined as 50 and 3
The quotient is 16
The remainder is 2

  1. 直方体の表面積と体積を見つけるJavaプログラム

    この記事では、直方体の表面積と体積を計算する方法を理解します。直方体は、長方形の6つの面を持つ3次元オブジェクトです。つまり、辺の長さと幅が異なります。立方体と直方体の違いは、立方体の長さ、高さ、幅が等しいのに対し、直方体ではこれら3つは同じではないことです 直方体の表面積は、式-を使用して計算されます。 2*( length *width + width* height + height*length) 直方体の面積は、式-を使用して計算されます。 length*width*height 以下は同じのデモンストレーションです- 入力 入力が-であると仮定します Length

  2. 2つの数値を読み取り、その商と剰余を出力するPythonプログラム

    2つの数値を読み取り、それらを分割したときに商と余りを出力する必要がある場合は、「//」演算子と「%」演算子を使用できます。 以下は同じのデモンストレーションです- 例 first_num = int(input("Enter the first number...")) second_num = int(input("Enter the second number...")) print("The first number is ") print(first_num) print("The second number i