Golang Example program to find quotient and remainder of a integers


In this example program, We will find quotient and remainder from the given numerator(dividend) and denominator(divisor)  using modulus and division operator in Golang.
The quotient is a result of the division operator in mathematics. In golang division operator / is used and applied to integers.
the remainder is a result of modulus operator in mathematics. % symbol is defined in Golang.

Example: Find Quotient and Remainder

The below example explains about golang arithmetic operators

  • Division operator - Divide the numerator with the denominator
  • Modulus operator - Output remainder for the result of the division operator

Following is an example of Division and modulus operator in Golang

package main  
  
import (  
 "fmt"  
)  
  
func main() {  
 numerator := 40  
 denominator := 20  
 /*quotient := numerator / denominator  
 remainder := numerator % denominator */  
  
 // above commented code can be replaced with single line as below  
 quotient, remainder := numerator/denominator, numerator%denominator  
  
 fmt.Println("quotient result:", quotient)  
 fmt.Print("remainder result:", remainder)  
  
}  

The output of the above program is

quotient result: 2  
remainder result:0  

In the above example, Two integer numbers 40(numerator) and 20(denominator) are stored in a variable - numerator, and denominator.

40 is divided by 20 using / Operator. and result is stored in the quotient variable. the remainder of 40/20 is 0, stored in remainder variable.

 Both quotient and remainder variables values are printed to console using fmt println function.

Similar Posts