In the below program, to find the quotient and remainder of the two numbers, the user is first asked to enter two numbers. The inputs are scanned using the scanf() function and stored in the variables A and B. Then, the variables A and B are divided using the arithmetic operator / to get the quotient as the result stored in the variable quotient; and using the arithmetic operator % to get the remainder as the result stored in the variable remainder.
Below are the programs to find the quotient and remainder of two numbers:
C++
#include <iostream>
using namespace std;
int main()
{
int A = 17, B = 5;
cout << "Enter two numbers A and B: " ;
cin >> A >> B;
int quotient = A / B;
int remainder = A % B;
cout << "Quotient when A / B is: " << quotient << endl;
cout << "Remainder when A / B is: " << remainder;
}
|
C
#include <stdio.h>
int main()
{
int A, B, quotient = 0, remainder = 0;
printf ( "Enter two numbers A and B : \n" );
scanf ( "%d%d" , &A, &B);
quotient = A / B;
remainder = A % B;
printf ( "Quotient when A/B is: %d\n" , quotient);
printf ( "Remainder when A/B is: %d" , remainder);
return 0;
}
|
Java
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 ;
System.out.println("Enter two numbers A and B :
"+" "+ A+" "+ B);
quotient = A / B;
remainder = A % B;
System.out.println( "Quotient when A/B is: " + quotient);
System.out.println( "Remainder when A/B is: " + remainder);
}
}
|
Python3
if __name__ = = '__main__' :
quotient = 0
remainder = 0
A, B = [ int (x) for x in input ().split()]
quotient = int (A / B)
remainder = A % B
print ( "Quotient when A/B is:" , quotient)
print ( "Remainder when A/B is:" , remainder)
|
Javascript
<script>
var A, B;
document.write( "Enter two numbers A and B: " );
A = parseInt(prompt());
B = parseInt(prompt());
var quotient = Math.floor(A / B);
var remainder = A % B;
document.write( "Quotient when A / B is: " +quotient);
document.write( "Remainder when A / B is: " +remainder);
</script>
|
Output
Enter two numbers A and B: Quotient when A / B is: 3
Remainder when A / B is: 2
The complexity of the above method
Time Complexity: O(1)
Auxiliary Space: O(1)
2. Using Loops
Below is the C program to find the quotient and remainder using loops:
C
#include <stdio.h>
int main()
{
int A = 17, B = 5;
int quotient = 0;
while (quotient * B <= A)
quotient++;
quotient--;
int remainder = A - (B * quotient);
printf ( "Quotient when A/B is: %d\n" , quotient);
printf ( "Remainder when A/B is: %d" , remainder);
return 0;
}
|
C++
#include <iostream>
using namespace std;
int main()
{
int A = 17, B = 5;
int quotient = 0;
while (quotient * B <= A)
quotient++;
quotient--;
int remainder= A - (B * quotient);
cout << "Quotient when A / B is: " <<
quotient << endl;
cout << "Remainder when A / B is: " <<
remainder;
return 0;
}
|
Java
import java.io.*;
import java.util.Scanner;
class GFG {
public static void main (String[] args) {
int A = 17 , B = 5 ;
int quotient = 0 ;
while (quotient * B <= A)
quotient++;
quotient--;
int remainder= A - (B * quotient);
System.out.println( "Quotient when A/B is: " +
quotient);
System.out.println( "Remainder when A/B is: " +
remainder);
}
}
|
Output
Quotient when A/B is: 3
Remainder when A/B is: 2
3. Using Functions
Below is the program to find the quotient and remainder of a number using functions:
C
#include <stdio.h>
int find_quotient( int a, int b)
{
return a / b;
}
int find_remainder( int a, int b)
{
return a % b;
}
int main()
{
int A = 17, B = 5;
int quotient = find_quotient(A, B);
int remainder = find_remainder(A, B);
printf ( "Quotient when A/B is: %d\n" , quotient);
printf ( "Remainder when A/B is: %d" , remainder);
return 0;
}
|
C++
#include <iostream>
using namespace std;
int find_quotient( int a, int b)
{
return a / b;
}
int find_remainder( int a, int b)
{
return a % b;
}
int main()
{
int A = 17, B = 5;
int quotient = find_quotient(A, B);
int remainder=find_remainder(A, B);
cout << "Quotient when A / B is: " <<
quotient << endl;
cout << "Remainder when A / B is: " <<
remainder;
return 0;
}
|
Java
import java.io.*;
import java.util.Scanner;
class GFG {
public static void main (String[] args) {
int A = 17 , B = 5 ;
int quotient = find_quotient(A, B);
int remainder=find_remainder(A, B);
System.out.println( "Quotient when A/B is: " +
quotient);
System.out.println( "Remainder when A/B is: " +
remainder);
}
public static int find_quotient( int a, int b)
{
return a / b;
}
public static int find_remainder( int a, int b)
{
return a % b;
}
}
|
Output
Quotient when A/B is: 3
Remainder when A/B is: 2
C Program to Compute Quotient and Remainder
Given two numbers A and B. The task is to write a program to find the quotient and remainder of these two numbers when A is divided by B.
Input: A = 2, B = 6
Output: Quotient = 0, Remainder = 2
Input: A = 17, B = 5
Output: Quotient = 3, Remainder = 2