Golang Program to convert a number into a rational number

In this tutorial we will see how to convert a number into a rational number using Go programming language.

A rational number is a type of real number, which is in the form of p/q where q is not equal to zero. Any fraction with non-zero denominators is a rational number. Some of the examples of rational numbers are 1/2, 1/5, 3/4, and so on.

Syntax

funcNewRat(a, b int64) *Rat
NewRat creates a new Rat with numerator a and denominator b

Syntax for Scanln:
Func Scanln(a?interface{}) (n int, err error)

Package math/big implements arbitrary-precision arithmetic (big numbers).

NewRat(a, b) returns a *Rat set to the fraction a/b where a and b are int64 values

Convert A Number To A Rational Number With One Input

Algorithm

Step 1 ? Import the package fmt and math/big package

Step 2 ? Start function main()

Step 3 ? Declare the NewRat function

Step 4 ? Initialize the formula with a value

Step 5 ? Print the rational number on the screen using fmt.Println()

Example

This example shows how to convert a number to a rational number with one input

<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">// import the fmt and math/big package</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 string">"math/big"</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 class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Golang Program to convert a number into a rational number"</span><span class="token punctuation">)</span>
   
   <span class="token comment">// NewRat creates a new Rat with numerator a and denominator b</span>
   r <span class="token operator">:</span><span class="token operator">=</span> <span class="token keyword">new</span><span class="token punctuation">(</span><span class="token class-name"><span class="token namespace">big<span class="token punctuation">.</span></span>Rat</span><span class="token punctuation">)</span>
   
   <span class="token comment">//use the Sscan function so that it returns two</span>
   
   <span class="token comment">//values: a string and an error</span>
   _<span class="token punctuation">,</span> err <span class="token operator">:</span><span class="token operator">=</span> <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Sscan</span><span class="token punctuation">(</span><span class="token string">"2.5"</span><span class="token punctuation">,</span> r<span class="token punctuation">)</span>
   
   <span class="token comment">//if statement to check for an invalid request</span>
   <span class="token keyword">if</span> err <span class="token operator">!=</span> nil <span class="token punctuation">{</span>
   
      <span class="token comment">//and return an error if the request is invalid</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">"error scanning value:"</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
   <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      
      <span class="token comment">// Print the rational number</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 rational number is"</span><span class="token punctuation">,</span>r<span class="token punctuation">)</span>
   <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

Golang Program to convert a number into a rational number
The rational number is 5/2

Description of code

  • In the above program, we first declare the package main.

  • We imported the fmt package that includes the files of package fmt and we also imported math/big package for implementing big numbers

  • Now start the function main()

  • Declare the NewRat function which creates a new rational number with a numerator and denominator

  • Next initialize the formula with a number value, which you want to be converted to a rational number

  • In the line - _, err := fmt.Sscan("2.5", r) - Calling the Sscan() function which returns the number successfully scanned and error if it persists

  • Next - if err != nil { - this line gets executed if there is any error

  • Last we print the rational number on the screen using fmt.Println()

Convert a number to a rational number with 2 inputs

Algorithm

Step 1 ? Import the package fmt and math/big package

Step 2 ? Start function main()

Step 3 ? Declare the NewRat function

Step 4 ? Initialize the formula with a value

Step 5 ? Calling the Scanln() function for scanning, reading and storing the input

Step 6 ? Print the rational number on the screen using fmt.Println()

Example

This example shows how to convert a number to a rational number with 2 inputs

<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 comment">// Including the main package</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>

<span class="token comment">// Importing fmt and math/big</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
   <span class="token string">"fmt"</span>
   <span class="token string">"math/big"</span>
<span class="token punctuation">)</span>
<span class="token comment">// Calling main</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 class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Golang Program to convert a number into a rational number"</span><span class="token punctuation">)</span>
   
   <span class="token comment">// NewRat creates a new Rat with numerator a and denominator b</span>
   r <span class="token operator">:</span><span class="token operator">=</span> <span class="token class-name"><span class="token namespace">big<span class="token punctuation">.</span></span>NewRat</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span>
   <span class="token comment">// Calling Scanln() function for</span>
   <span class="token comment">// scanning and reading the input</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Scanln</span><span class="token punctuation">(</span>r<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 rational number is"</span><span class="token punctuation">,</span>r<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

Golang Program to convert a number into a rational number
The rational number is 3/4

Description of code

  • In the above program, we first declare the package main.

  • We imported the fmt package that includes the files of package fmt and we also imported math/big package for implementing big numbers

  • Now start the function main()

  • Declare the NewRat function which creates a new rational number with a numerator and denominator

  • Next using a value of a number which you want to be cinverted to rational number, apply it to the NewRat formula

  • Calling the Scanln() function for scanning and reading the input and storing it in the variable r

  • Last print the rational number on the screen using fmt.Println()

Conclusion

In the above two examples we have successfully compiled and executed the Golang code to convert a number into a rational number using Go programming language.

Updated on: 2022-12-09T05:54:57+05:30

607 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements