How to Compute Quotient and Remainder in Golang?


In this tutorial, we are going to understand the approach to finding the quotient and the remainder by performing the arithmetic operations on the dividend and the divisor provided as input by the user. This tutorial will cover two ways to do the same thing.

Explanation

To divide any number we use the “ / ” arithmetic operator and to find the remainder we use the “ % ” arithmetic operator

Suppose we have a dividend 19 and a divisor 4 then the quotient and the remainder are as follows:

Quotient = dividend / divisor
   = 19 / 4
   = 4 
Remainder = dividend % divisor
   = 19 % 4
   = 3

Quotient and Remainder within the function

Algorithm

  • Step 1 − Declaring the variables for the dividend, divisor, quotient, and remainder of int32 data type.
  • Step 2 − Taking the input for the dividend, and divisor from the user.
  • Step 3 − Finding the quotient, and remainder using the above arithmetic operators within the function.
  • Step 4 − Printing the result.

Time Complexity

O(1) - The time complexity is constant because no matter what is the input the program will take sane time.

Space Complexity

O(1) - The variables are static in the program so the space complexity is also constant.

Example 1

In this example, we are going to find the quotient and the remainder within the function.

package main // fmt package provides the function to print anything import ( "fmt" ) func main() { // declaring the variables to store dividend, divisor, // quotient, and the remainder of type int32 using the var keyword var dividend, divisor, quotient, remainder int32 fmt.Println("Program to find the quotient and the remainder of a number within the function.") // initializing the dividend dividend = 92 // initializing the dividend divisor = 7 // finding the quotient by / arithmetic operator quotient = dividend / divisor // finding the remainder by % arithmetic operator remainder = dividend % divisor // Printing the result fmt.Println("Dividend =", dividend, "\nDivisor=", divisor) fmt.Println("Quotient = ", quotient) fmt.Println("Reminder = ", remainder) }

Output

Program to find the quotient and the remainder of a number within the function.
Dividend = 92
Divisor= 7
Quotient = 13
Reminder = 1

Description of code

  • var dividend, divisor, quotient, remainder int32 − In this line, we are declaring the dividend, divisor, quotient, and remainder that we are going to use later. As the dividend, divisor, quotient, and remainder will be of integer, we have used the int32 data type.

  • quotient = dividend / divisor − finding the quotient by / arithmetic operator

  • remainder = dividend % divisor − finding the remainder by % arithmetic operator.

  • fmt.Println("The quotient is", quotient) − Printing the quotient

  • fmt.Println("The remainder is", remainder) − Printing the remainder.

Quotient and Remainder in separate functions.

Algorithm

  • Step 1 − Declaring the variables for the dividend, divisor, quotient, and remainder of the int32 data type.

  • Step 2 − Taking the input for the dividend, and divisor from the user

  • Step 3 − Calling the function with the dividend, and divisor as a parameter, and storing the quotient that quotientFunction(dividend, divisor int32) function is returning.

  • Step 4 − Calling the function with the length and breadth as a parameter, and storing the perimeter the remainderFunction(dividend, divisor int32) function is returning

  • Step 5 − Printing the result.

Example 2

In this example, we are going to find the quotient and the remainder by defining the separate functions.

package main // fmt package provides the function to print anything import ( "fmt" ) // This function is returning the quotient func quotientFunction(dividend, divisor int32) int32 { // finding the quotient by / arithmetic operator return dividend / divisor } // This function is returning the remainder func remainderFunction(dividend, divisor int32) int32 { // finding the remainder by % arithmetic operator return dividend % divisor } func main() { // declaring the variables to store dividend, divisor, // quotient, and the remainder of type int32 using the var keyword var dividend, divisor, quotient, remainder int32 fmt.Println("Program to find the quotient and the remainder of a number by using the separate function.") // scanning the value of the dividend from the user fmt.Println("Enter the value of the dividend:") fmt.Scanln(÷nd) // scanning the value of the divisor from the user fmt.Println("Enter the value of the divisor:") fmt.Scanln(&divisor) // finding the quotient by calling the quotientFunction() function quotient = quotientFunction(dividend, divisor) // finding the remainder by calling the remainderFunction() function remainder = remainderFunction(dividend, divisor) // Printing the result fmt.Println("The quotient is", quotient) fmt.Println("The remainder is", remainder) }

Output

Program to find the quotient and the remainder of a number by using the separate function.
Enter the value of the dividend:
63
Enter the value of the divisor:
4
The quotient is 15
The remainder is 3

Description of code

  • var dividend, divisor, quotient, remainder int32 − In this line, we are declaring the dividend, divisor, quotient, and remainder that we are going to use later. As the dividend, divisor, quotient, and remainder will be of integer so we have used the int32 data type.

  • fmt.Scanln(÷nd) − Taking the input for the dividend from the user.

  • fmt.Scanln(&divisor) − Taking the input for the divisor from the user.

  • quotient = quotientFunction(dividend, divisor) − finding the quotient by calling the quotientFunction() function.

    • func quoitentFunction(dividend, divisor int32) int32 {} − This is the function where the logic of finding quotient resides. The function has the dividend, and divisor as parameters of int32 type and also has a return type of int32.

    • func remainderFunction(dividend, divisor int32) int32 {} − This is the function where the logic of finding remainder resides. The function has the dividend, and divisor as parameters of int32 type and also has a return type of int32.

  • remainder = remainderFunction(dividend, divisor) − finding the quotient by calling the remainderFunction() function.

  • fmt.Println("The quotient is", quotient) − Printing the quotient.

  • fmt.Println("The remainder is", remainder) − Printing the remainder.

Conclusion

These are the two ways to find the quotient and the remainder in Golang. The second way is much better in terms of modularity and code reusability as we can call that function anywhere in the project. To learn more about go you can explore these tutorials.

Updated on: 29-Aug-2022

712 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements