Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
<span class="token string">"fmt"</span>
<span class="token punctuation">)</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// declaring the variables to store dividend, divisor,</span>
<span class="token comment">// quotient, and the remainder of type int32 using the var keyword</span>
<span class="token keyword">var</span> dividend<span class="token punctuation">,</span> divisor<span class="token punctuation">,</span> quotient<span class="token punctuation">,</span> remainder int32
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Program to find the quotient and the remainder of a number within the function."</span><span class="token punctuation">)</span>
<span class="token comment">// initializing the dividend</span>
dividend <span class="token operator">=</span> <span class="token number">92</span>
<span class="token comment">// initializing the dividend</span>
divisor <span class="token operator">=</span> <span class="token number">7</span>
<span class="token comment">// finding the quotient by / arithmetic operator</span>
quotient <span class="token operator">=</span> dividend <span class="token operator">/</span> divisor
<span class="token comment">// finding the remainder by % arithmetic operator</span>
remainder <span class="token operator">=</span> dividend <span class="token operator">%</span> divisor
<span class="token comment">// Printing the result</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Dividend ="</span><span class="token punctuation">,</span> dividend<span class="token punctuation">,</span> <span class="token string">"\nDivisor="</span><span class="token punctuation">,</span> divisor<span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Quotient = "</span><span class="token punctuation">,</span> quotient<span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Reminder = "</span><span class="token punctuation">,</span> remainder<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
<span class="token string">"fmt"</span>
<span class="token punctuation">)</span>
<span class="token comment">// This function is returning the quotient</span>
func <span class="token function">quotientFunction</span><span class="token punctuation">(</span>dividend<span class="token punctuation">,</span> divisor int32<span class="token punctuation">)</span> int32 <span class="token punctuation">{</span>
<span class="token comment">// finding the quotient by / arithmetic operator</span>
<span class="token keyword">return</span> dividend <span class="token operator">/</span> divisor
<span class="token punctuation">}</span>
<span class="token comment">// This function is returning the remainder</span>
func <span class="token function">remainderFunction</span><span class="token punctuation">(</span>dividend<span class="token punctuation">,</span> divisor int32<span class="token punctuation">)</span> int32 <span class="token punctuation">{</span>
<span class="token comment">// finding the remainder by % arithmetic operator</span>
<span class="token keyword">return</span> dividend <span class="token operator">%</span> divisor
<span class="token punctuation">}</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// declaring the variables to store dividend, divisor,</span>
<span class="token comment">// quotient, and the remainder of type int32 using the var keyword</span>
<span class="token keyword">var</span> dividend<span class="token punctuation">,</span> divisor<span class="token punctuation">,</span> quotient<span class="token punctuation">,</span> remainder int32
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Program to find the quotient and the remainder of a number by using the separate function."</span><span class="token punctuation">)</span>
<span class="token comment">// scanning the value of the dividend from the user</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Enter the value of the dividend:"</span><span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Scanln</span><span class="token punctuation">(</span>÷nd<span class="token punctuation">)</span>
<span class="token comment">// scanning the value of the divisor from the user</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Enter the value of the divisor:"</span><span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Scanln</span><span class="token punctuation">(</span><span class="token operator">&</span>divisor<span class="token punctuation">)</span>
<span class="token comment">// finding the quotient by calling the quotientFunction() function</span>
quotient <span class="token operator">=</span> <span class="token function">quotientFunction</span><span class="token punctuation">(</span>dividend<span class="token punctuation">,</span> divisor<span class="token punctuation">)</span>
<span class="token comment">// finding the remainder by calling the remainderFunction() function</span>
remainder <span class="token operator">=</span> <span class="token function">remainderFunction</span><span class="token punctuation">(</span>dividend<span class="token punctuation">,</span> divisor<span class="token punctuation">)</span>
<span class="token comment">// Printing the result</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The quotient is"</span><span class="token punctuation">,</span> quotient<span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The remainder is"</span><span class="token punctuation">,</span> remainder<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div>
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.
