Java 程序:求商和余数
原文:https://www.studytonight.com/java-programs/java-program-to-find-quotient-and-remainder
在本教程中,我们将学习如何通过接受用户的输入来找到商和余数。但是在继续之前,如果你不熟悉 java 中算术运算符的概念,那么一定要查看关于 Java 中运算符的文章。
输入:输入第一个数字:6
输入第二个数字:2
输出:
6 和 2 的商是 3
6 和 2 的余数是 0
上述问题可以通过以下方式解决:
方法 1:当值被预定义时
方法 2:当值由用户定义时
让我们分别看看这些方法。
程序 1:求商和余数
在这个程序中,当两个数字是用户定义的时,我们将找到它们的商和余数。
算法:
- 开始
- 声明两个变量。
- 初始化变量。
- 用除法运算符求商。
- 使用模运算符求余数。
- 显示商和余数。
- 停下来。
下面是相同的代码。
//Java Program to find the quotient and remainder
public class Main
{
public static void main(String[] args)
{
int num1 = 19, num2 = 4; //Declare and initialize the numbers
System.out.println("The entered number is: "+num1);
System.out.println("The entered number is: "+num1);
int quotient = num1 / num2; //Find quotient
int remainder = num1 % num2; //Find Remainnder
System.out.println("After division the quotient and remainder are: ");
//Print the quotient and remainder
System.out.println("The quotient is: " + quotient);
System.out.println("The remainder is: " + remainder);
}
}
输入的数字是:19 输入的数字是:19 除法后的商和余数是: 商是:4 余数是:3
程序 2:求商和余数
在这个程序中,当两个数字是用户定义的时,我们将找到它们的商和余数。这意味着,在这里,首先我们将要求用户初始化数字,然后我们将找到商和余数。
算法:
- 开始
- 创建 Scanner 类的实例以从用户处获取输入。
- 声明两个变量。
- 请用户初始化它。
- 用除法运算符求商。
- 使用模运算符求余数。
- 显示商和余数。
- 停下来。
下面是相同的代码。
//Java Program to find the quotient and remainder
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create object of Scanner class
Scanner sc=new Scanner(System.in);
System.out.println("Enter the numbers ");
System.out.println("Enter the first number: ");
int num1=sc.nextInt(); //Initialize the number
System.out.println("Enter the second number: ");
int num2=sc.nextInt(); //Initialize the number
int quotient = num1 / num2;
int remainder = num1 % num2;
System.out.println("After division the quotient and remainder are:");
//Print the Quotient
System.out.println("The quotient is: " + quotient);
System.out.println("The remainder is: " + remainder);
}
}
输入数字 输入第一个数字:19 输入第二个数字:7 除法后商和余数为: 商为:2 余数为:5
程序 3:求商和余数
在这个程序中,我们将使用用户定义的方法,用用户定义的输入来求商和余数。
算法:
- 开始
- 创建 Scanner 类的实例以从用户处获取输入。
- 声明两个变量。
- 请用户初始化它。
- 调用用户定义的方法来求商和余数。
- 用除法运算符求商。
- 使用模运算符求余数。
- 显示商和余数。
- 停下来。
下面是相同的代码。
//Java Program to find the quotient and remainder
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create object of Scanner class
Scanner sc=new Scanner(System.in);
System.out.println("Enter the numbers ");
System.out.println("Enter the first number: ");
int num1=sc.nextInt(); //Initialize the number
System.out.println("Enter the second number: ");
int num2=sc.nextInt(); //Initialize the number
findQuotient(num1,num2);
}
//user defined method
static void findQuotient(int num1, int num2)
{
int quotient=num1/num2;
int remainder=num1%num2;
//display result
System.out.println("The quotient of "+num1+" and "+num2+" is "+quotient);
System.out.println("The remainder of "+num1+" and "+num2+" is"+remainder);
}
}
输入数字 输入第一个数字:9 输入第二个数字:7 9 和 7 的商是 1 9 和 7 的余数是 2