Golang Program to get the Quotient and remainder using library function


In this article we will discuss about how to get the quotient and remainder in GO language using library function.

Syntax

func Div(a, b, c uint) (q, r uint)
func Remainder(q, r float64) float64

The div() function accepts three arguments as unsigned integers and returns the quotient and remainder respectively after computing the division process.

The remainder() function accepts two arguments as 64-bit float values and returns the remainder after performing the division process in the same format.

The source code to get the quotient and remainder of division using library functions is compiled and executed below.

Finding The Quotient Of Division Of Two Numbers Using Div()

Algorithm

  • Step 1 − Import the package fmt and bits.

  • Step 2 − Initialize and define the division() function.

  • Step 3 − Start the main function().

  • Step 4 − Initialize the variables and store the values of dividend and divisor in them.

  • Step 5 − Call the division function by passing the value of dividend and divisor as arguments to them.

  • Step 6 − Perform the division process using bits.Div() predefined library function.

  • Step 7 − Return the value of quotient and remainder.

  • Step 8 − print the result on the screen.

Example

Golang Program to get the quotient of division of two numbers using library function 12. Golang Program to get the Quotient and remainder using library function

package main import ( "fmt" "math/bits" ) // fmt package allows us to print anything on the screen. // bits sub package defined in math package is used to perform bitwise operations on unsigned integer data types. // initializing and defining the division() function func division(dividend, divisor uint) (quotient, remainder uint) { // Finding quotient and remainder Using Div() function quotient, remainder = bits.Div(0, dividend, divisor) // returning the results return quotient, remainder } // calling the main() function func main() { // initializing the variables. var dividend, divisor, quotient, remainder uint // assigning the values of dividend. dividend = 50 // assigning the values of divisor. divisor = 7 // calling the division() function and storing the results in quotient and remainder variables quotient, remainder = division(dividend, divisor) // printing the Quotient on the screen fmt.Println("The Quotient of ", dividend, "/", divisor, "is:") fmt.Println(quotient) // printing the Remainder on the screen fmt.Println("The Remainder of ", dividend, "/", divisor, "is:") fmt.Println(remainder) }

Output

The Quotient of 50 / 7 is:
7
The Remainder of 50 / 7 is:
1

Description of the Code

  • First, we import the package fmt and bits that allows us to print anything and perform bitwise manipulation respectively.

  • Initialize and define the division() function that will contain the logic to perform the division process.

  • This function accepts two arguments as unsigned int values and returns the results.

  • We have used variables with unsigned integer data types here because bits.Div() function accepts unsigned integer values.

  • This method accepts three arguments and returns two values. For sake of division of two numbers here we have used one of its value as zero.

  • The values returned by this function are the values of quotient and remainder resp.

  • Store the values returned by this function in separate variables and return them.

  • Start the main() function.

  • Initialize the variables of unsigned int data types and store the values of dividend and divisor in them.

  • Call the division() function and pass the dividend and divisor as arguments to it.

  • Store the results returned by the function in a separate variable namely quotient and remainder resp.

  • Print the results on the screen using fmt.Println() function.

Finding The Quotient Of Division Of Two Numbers Using Remainder()

Algorithm

  • Step 1 − Import the package fmt and bits.

  • Step 2 − Initialize and define the division() function.

  • Step 3 − Start the main function().

  • Step 4 − Initialize the variables and store the values of dividend and divisor in them.

  • Step 5 − Call the division function by passing the value of dividend and divisor as arguments to them.

  • Step 6 − Perform the division process using math.Remainder() predefined library function.

  • Step 7 − Return the value of quotient and remainder.

  • Step 8 − print the result on the screen.

Example

There is one more library function in go language which can be used to compute the remainder by division of two integers. This example will discuss about this method.

package main import ( "fmt" "math" ) // math package enables us to use other predefined mathematical functions. // initializing and defining the division() function func division(dividend, divisor float64) (remainder float64) { // Finding the remainder Using math.Remainder() function // storing the results in remainder variable remainder = math.Remainder(dividend, divisor) // returning the results return remainder } // calling the main() function func main() { // initializing the variables. var dividend, divisor, remainder float64 // storing the value of dividend dividend = 100.0 // storing the value of divisor divisor = 3.0 // calling the division() function remainder = division(dividend, divisor) // printing the Remainder on the screen fmt.Println("The Remainder of ", dividend, "/", divisor, "is:") fmt.Println(remainder) }

Output

The Remainder of 100 / 3 is:
1

Description of the Code

  • First, we import the package fmt and math that allows us to print anything and use predefined mathematical operations respectively.

  • Initialize and define the division() function that will contain the logic to perform the division process.

  • This function accepts two arguments as 64 bit float values and returns the result.

  • We have used 64 bit float type variables here because math.Remainder() function accepts 64 bit float type values.

  • The value returned by math.Remainder() function is the remainder so store it in a separate variable.

  • Return this value.

  • Start the main() function.

  • Initialize the respective variables and store the values of dividend and divisor in them.

  • Call the division() function and pass the dividend and divisor as arguments to it.

  • Store the result returned by the function in a separate variable and print its value.

Conclusion

We have successfully compiled and executed the Go language program that will get the division and remainder using library functions along with examples.

Updated on: 25-Oct-2022

409 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements