Q:

Write a program to find the quotient and remainder

0

In this article, we will discuss the program to find the quotient and remainder in different programming languages such as in C, Java, PHP, and python.

But before writing the programs, let's first see a brief description of the quotient and remainder.

What is the quotient?

The different parts of division are Dividend, Divisor, Quotient, and Remainder. Quotient can be defined as the resultant of the division. The formula for quotient is -

 

Quotient = Dividend/Divisor  

In division, a number is divided by another number to get the output in the form of another number. The dividend is the number that is getting divided, and the divisor is the number that divides the given number.

In a fraction, the numerator is dividend, denominator is divisor, and when numerator is divided by the denominator, the result will be quotient. For instance, suppose we have to divide 20 (dividend) by 4 (divisor); we will get 5, which is the quotient.

What is the remainder?

Remainder is defined as a value that is left after division. As an instance, if we divide 25 with 4, we will get 1 as a remainder. If the number divides another number completely, the remainder will be 0.

Remainder is always smaller than the divisor; if it is larger, that means the division is not completed. But remainder can be less or greater than the quotient; for instance, if we divide 41 by 7, we will get 5 as a quotient, and 6 as a remainder which is less than divisor but greater than the quotient.

We know the method of division -

 

Dividend = Divisor x Quotient + Remainder  

So, the formula for remainder will be -

 

Remainder = Dividend - (Divisor x Quotient) 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Programs to find the quotient and remainder

Program: Write a program to find the quotient and remainder in the C language.

#include <stdio.h>  
  
int main()  
{  
   int num1, num2, quotient = 0, rem = 0;  
    
    printf("Enter two numbers : \n");  
    scanf("%d%d", &num1, &num2);  
    quotient = num1 / num2;  
    rem = num1 % num2;  
    printf("Quotient when %d/%d is: %d\n", num1, num2, quotient);  
    printf("Remainder when %d is divided by %d is: %d", num1, num2, rem);  
    
    return 0;  
}   

Output

 

Program: Write a program to find the quotient and remainder in the C# language.

In C#, we can use the Math.DivRem method to find the quotient and remainder of two numbers. We can also write the program without using Math.DivRem method. So, let's see both approaches of finding quotient and remainder in C#.

Using Math.DivRem() method

using System;  
public class JTP {  
   public static void Main(){  
      int dividend = 34, divisor = 3, remainder = 0;  
        int quotient = Math.DivRem(dividend, divisor, out remainder);  
      Console.WriteLine("Quotient = " + quotient);  
      Console.WriteLine("Remainder = " + remainder);  
   }  
}   

Output:

 

Native approach

using System;  
public class JTP {  
   public static void Main(){  
      int dividend = 34, divisor = 3, remainder = 0;  
        int quotient = dividend/divisor;  
        remainder = dividend%divisor;  
      Console.WriteLine("Quotient = " + quotient);  
      Console.WriteLine("Remainder = " + remainder);  
   }  
}  

Output:

 

Program: Write a program to find the quotient and remainder in Java language.

import java.io.*;  
import java.util.Scanner;  
  
class Javatpoint {  
  
  
    public static void main (String[] args) {  
    System.out.println("Enter the value of num1 and num2");  
    Scanner sc = new Scanner(System.in);  
    int num1 = sc.nextInt();  
    int num2 = sc.nextInt();  
    int quotient = 0, remainder = 0;  
  
      
    quotient = num1 / num2;  
  
    remainder = num1 % num2;  
  
    System.out.println("Quotient when " + num1 + "/" + num2 + " is: "   
  
+ quotient);  
    System.out.println("Remainder when " + num1 + " is divided by "   
  
+ num2 + " is: " + remainder);  
    }  
}   

Output:

 

Program: Write a program to find the quotient and remainder in JavaScript language.

In this example, we are using the Math.floor() function to calculate the divisor. The JavaScript Math.floor() method decreases the given number up to the closest integer value and returns it. For example, 3 for 3.7, 5 for 5.9, etc.

If the given number is already an integer, the floor() method returns it directly.

<!DOCTYPE html>  
<html>  
   <body>  
<script>  
function fun(num1,num2)  
{  
    let quotient, remainder;  
    quotient = Math.floor(num1/num2);  
    remainder = num1%num2;  
    document.write("The quotient of " + num1 + " and " + num2 + " is " + quotient + "." + "<br>");  
    document.write("The remainder when " + num1 + " is divided by " + num2 + " is " + remainder + ".");  
}  
    let num1 = 34, num2 = 3;  
    fun(num1, num2);  
</script>  
</body>  
</html>  

Output:

 

Program: Write a program to find the quotient and remainder in PHP language.

In PHP, we can find the quotient and remainder by using the intdiv() and fmod() functions.

The intdiv() is a mathematical function of PHP, which is used to calculate integer division. It returns the integer quotient of the division.

 

int intdiv ( int $dividend , int $divisor)   

PHP fmod() function is used to return the floating point remainder of the division of the argument.

 

float fmod ( float $x , float $y );  

Now, let's see the programs with or without using the default PHP functions.

Using intdiv() and fmod() functions

<?php  
$dividend = 35;  
$divisor = 2;  
$quotient = intdiv($dividend, $divisor);  
$remainder = fmod($dividend, $divisor);  
echo "The quotient of " . $dividend . " and " . $divisor . " is: " . $quotient . "\n";  
echo "The remainder of " . $dividend . " and " . $divisor . " is: " . $remainder . "\n";  
?>   

 Output:

 

Native approach

<?php  
$dividend = 35;  
$divisor = 2;  
$quotient = floor($dividend / $divisor);  
$remainder = $dividend % $divisor;  
echo "The quotient of " . $dividend . " and " . $divisor . " is: " . $quotient . "<br>";  
echo "The remainder of " . $dividend . " and " . $divisor . " is: " . $remainder . "\n";  
?>  

  

Output:

 

Program: Write a program to find the quotient and remainder in Python language.

Here, we also see two approaches of finding quotient and remainder in python. The first approach is by using the divmod() function and second by native approach.

Python divmod() function is used to get the remainder and quotient of two numbers. This function takes two numeric arguments and returns a tuple.

 

divmod (number1, number2)    

Using divmod() function

dividend = int(input("Enter the first number: "))  
divisor = int(input("Enter the second number: "))  
quotient, remainder = divmod(dividend, divisor)  
print("The quotient of " , dividend , " and " , divisor , " is: " , quotient)  
print("The remainder of " , dividend , " and " , divisor , " is: " , remainder)  

Output:

 

Using native approach

import math  
dividend = int(input("Enter the first number: "))  
divisor = int(input("Enter the second number: "))  
quotient = dividend/divisor   
remainder = dividend%divisor  
print("The quotient of " , dividend , " and " , divisor , " is: " , math.floor(quotient))  
print("The remainder of " , dividend , " and " , divisor , " is: " , remainder) 

 

Output:

 

So, that's all about the article. We have discussed the program to find the quotient and remainder in C, Java, C#, PHP, python, and JavaScript. Hope you find the article informative and gain knowledge about the finding of quotient and remainder in different programming languages.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now