Open In App

C Program to Compute Quotient and Remainder

Last Updated : 31 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Quotient Remainder explained

 

Examples

Input: A = 2, B = 6
Output: Quotient = 0, Remainder = 2

Input: A = 17, B = 5
Output: Quotient = 3, Remainder = 2

1. Using Division and Modulo Operator

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++




// C++ program to find quotient
// and remainder of two numbers
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
    int A = 17, B = 5;
 
    // Ask user to enter the two numbers
    cout << "Enter two numbers A and B: ";
 
    // Read two numbers from the user || A = 17, B = 5
    cin >> A >> B;
 
    // Calculate the quotient of A and B using '/' operator
    int quotient = A / B;
 
    // Calculate the remainder of A and B using '%' operator
    int remainder = A % B;
 
    // Print the result
    cout << "Quotient when A / B is: " << quotient << endl;
    cout << "Remainder when A / B is: " << remainder;
}
 
// This code is contributed by sarajadhav12052009


C




// C program to find quotient
// and remainder of two numbers
#include <stdio.h>
 
// Driver code
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);
 
    // Calculate the quotient of A and B using '/' operator
    quotient = A / B;
 
    // Calculate 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
 
    // Calculate the quotient of A and B using '/' operator
    quotient = A / B;
 
    // Calculate 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()]
 
#Calculate the quotient of A and B using '/' operator
    quotient = int(A / B)
 
#Calculate 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


Javascript




<script>
    // Javascript program to find quotient
    // and remainder of two numbers
     
     
    var A, B;
     
    // Ask user to enter the two numbers
    document.write("Enter two numbers A and B: ");
     
    // Read two numbers from the user || A = 17, B = 5
     
    A = parseInt(prompt());
    B = parseInt(prompt());
     
    // Calculate the quotient of A and B using '/' operator
    var quotient = Math.floor(A / B);
     
    // Calculate the remainder of A and B using '%' operator
    var remainder = A % B;
     
    // Print the result
     document.write("Quotient when A / B is: "+quotient);
     document.write("Remainder when A / B is: "+remainder);
     
    // This code is contributed by Aman Kumar
     
</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




// C program to find quotient and
// remainder
#include <stdio.h>
 
// Driver code
int main()
{
    int A = 17, B = 5;
 
    int quotient = 0;
 
    // Initializing the loop
    while (quotient * B <= A)
        quotient++;
 
    // Decrementing 1 because we came
    // one step forward
    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++




// C++ program to find quotient
// and remainder
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
  int A = 17, B = 5;
   
  int quotient = 0;
   
  // Initializing the loop
  while(quotient * B <= A)
    quotient++;
   
  // Decrementing 1 because we came
  // one step forward
  quotient--;
   
  int remainder= A - (B * quotient);
   
  cout << "Quotient when A / B is: " <<
           quotient << endl;
  cout << "Remainder when A / B is: " <<
           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) {
     
    int A = 17, B = 5;
    int quotient = 0;
   
    // Initializing the loop
    while(quotient * B <= A)
      quotient++;
     
    // Decrementing 1 because we came
    // one step forward
    quotient--;
   
    int remainder= A - (B * quotient);
     
    // Print the result
    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




// C program to find quotient and
// remainder
#include <stdio.h>
 
int find_quotient(int a, int b)
{
    // Return Quotient
    return a / b;
}
int find_remainder(int a, int b)
{
    // Return Quotient
    return a % b;
}
 
// Driver code
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++




// C++ program to find quotient and
// remainder
#include <iostream>
using namespace std;
 
int find_quotient(int a, int b)
{
    // Return Quotient
    return a / b;
}
int find_remainder(int a, int b)
{
    // Return Quotient
    return a % b;
}
  
// Driver code
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




// 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) {
     
    int A = 17, B = 5;
     
    int quotient = find_quotient(A, B);
    int remainder=find_remainder(A, B);
     
    // Print the result
    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 Quotient
    return a / b;
}
public static int find_remainder(int a, int b)
{
    // Return Quotient
    return a % b;
}
}


Output

Quotient when A/B is: 3
Remainder when A/B is: 2


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads