给定两个数字A和B。任务是编写一个程序,以在A除以B时找到这两个数字的商和余数。
例子:
Input: A = 2, B = 6
Output: Quotient = 0, Remainder = 2
Input: A = 17, B = 5
Output: Quotient = 3, Remainder = 2
在下面的程序中,要查找两个数字的商和余数,首先要求用户输入两个数字。使用scanf()函数对输入进行扫描并将其存储在变量中和 。然后,变量和使用算术运算运算符进行除法得到商作为结果存储在变量商中;并使用算术运算运算符%获得余数作为结果存储在变量余数中。
以下是查找两个数字的商和余数的程序:
C
// C program to find quotient
// and remainder of two numbers
#include
int main()
{
int A, B, quotient = 0, remainder = 0;
// Ask user to enter the two numbers
printf("Enter two numbers A and B : \n");
// Read two numbers from the user || A = 17, B = 5
scanf("%d%d", &A, &B);
// Calclulate the quotient of A and B using '/' operator
quotient = A / B;
// Calclulate the remainder of A and B using '%' operator
remainder = A % B;
// Print the result
printf("Quotient when A/B is: %d\n", quotient);
printf("Remainder when A/B is: %d", remainder);
return 0;
}
Java
// java program to find quotient
// and remainder of two numbers
import java.io.*;
import java.util.Scanner;
class GFG {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
int A = input.nextInt();
int B= input.nextInt();
int
quotient = 0, remainder = 0;
// Ask user to enter the two numbers
System.out.println("Enter two numbers A and B : "+" "+ A+" "+ B);
// Read two numbers from the user || A = 17, B = 5
// Calclulate the quotient of A and B using '/' operator
quotient = A / B;
// Calclulate the remainder of A and B using '%' operator
remainder = A % B;
// Print the result
System.out.println("Quotient when A/B is: "+ quotient);
System.out.println("Remainder when A/B is: "+ remainder);
}
}
//this code is contributed by anuj_67..
Python3
# Python3 program to find quotient
# and remainder of two numbers
if __name__=='__main__':
quotient = 0
remainder = 0
#Read two numbers from the user || A = 17, B = 5
A, B = [int(x) for x in input().split()]
#Calclulate the quotient of A and B using '/' operator
quotient = int(A / B)
#Calclulate the remainder of A and B using '%' operator
remainder = A % B
#Print the result
print("Quotient when A/B is:", quotient)
print("Remainder when A/B is:", remainder)
#this code is contributed by Shashank_Sharma
输出:
Enter two numbers A and B : 17 5
Quotient when A/B is: 3
Remainder when A/B is: 2
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。