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

package main // import the fmt and math/big package // fmt package provides the function to print anything import ( "fmt" "math/big" ) func main() { fmt.Println("Golang Program to convert a number into a rational number") // NewRat creates a new Rat with numerator a and denominator b r := new(big.Rat) //use the Sscan function so that it returns two //values: a string and an error _, err := fmt.Sscan("2.5", r) //if statement to check for an invalid request if err != nil { //and return an error if the request is invalid fmt.Println("error scanning value:", err) } else { // Print the rational number fmt.Println("The rational number is",r) } }

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

// Including the main package package main // Importing fmt and math/big import ( "fmt" "math/big" ) // Calling main func main() { fmt.Println("Golang Program to convert a number into a rational number") // NewRat creates a new Rat with numerator a and denominator b r := big.NewRat(6, 8) // Calling Scanln() function for // scanning and reading the input fmt.Scanln(r) // Printing the result fmt.Println("The rational number is",r) }

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: 09-Dec-2022

353 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements