Java 程序计算商和余数

javaobject oriented programmingprogramming更新于 2024/4/12 11:24:17

在本文中,我们将了解如何在 Java 中计算商和余数。商和余数使用两个简单公式"商 = 被除数 / 除数"和"余数 = 被除数 % 除数"计算。

给定一个整数 a 和一个非零整数 d,可以证明存在唯一的整数 q 和 r,使得 a = qd + r 且 0 ≤ r < |d|。数字 q 称为商,而 r 称为余数。

下面是相同的演示 −

输入

假设我们的输入是 −

被除数值:50
除数:3

输出

所需的输出将是 −

商:16
余数:2

算法

步骤 1- 开始
步骤 2- 将四个整数声明为 my_dividend 、my_divisor、my_quotient、my_remainder
步骤 3- 提示用户输入两个整数值,即 my_dividend 、my_divisor 或定义
整数
步骤 4- 读取值
步骤 5- 使用公式查找商和余数"商 = 被除数 / 除数"和"余数 = 被除数 % 除数"
步骤 6- 显示结果
步骤 7- 停止

示例 1

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

相关文章


有用资源