# Golang Program to get the numerator from a rational number

In this article, we will discuss how to get a numerator from a rational number.

### Syntax

func NewRat(a, b int64) *Rat
func (x *Rat) Num() *Int


NewRat() function takes two integer numbers as input arguments and returns a rational number in the form of a/b. here a is the numerator and b is the denominator of a rational number.

Num() function in the Go language returns the numerator of a rational number as the result in integer format.

## Method-1

Go language program to get the numerator from a rational number.

### Algorithm

• Step 1 − Import the fmt and big packages.

• Step 2 − Call the main() function.

• Step 3 − Initialize a variable to store the rational number. use big.NewRat() to create the rational number.

• Step 4 − Use num() function to get the numerator of the rational number.

• Step 5 − Store the result and print them on the screen

### Example

The source code to get the numerator of a rational number using a library function is as follows −

// Including the main package
package main

// Importing fmt and math/big
import (
"fmt"
"math/big"
)
// fmt package allows us to print anything on the screen
// big defined in math package allows us to use various predefined methods like Num()

// Calling main
func main() {
fmt.Println("Golang Program to get the numerator of a rational number")

// NewRat creates a new Rational number with numerator a and denominator b
number := big.NewRat(10, 16)

// Printing the result on the screen
fmt.Println("The rational number is", number)

// getting the numerator of the rational number
// storing the result in a result variable
result := number.Num()

// printing the numerator on the screen
fmt.Println("The numerator of rational number", number, "is:", result)
}


### Output

Golang Program to get the numerator of a rational number
The rational number is 5/8
The numerator of rational number 5/8 is: 5


### Description

• First we need to import the fmt package that allows us to print anything and the big package to use the various predefined methods.

• Call the main() function.

• Create a rational number using the NewRat() function defined in the big package by providing two inputs as arguments to the function. These inputs are given in such a way that the first one is treated as the numerator of the rational number and the second one is its denominator.

• Then store the number in a variable called number.

• Now we need to get the numerator of this number and print it on the screen.

• To get the numerator use the Num() function present in the big package. This function returns the numerator as the result in int format.

• Store the value of the result in a variable.

• Print the result on the screen using the println() function.

## Method-2

Go language program to get the numerator from a rational number using two functions.

### Algorithm

• Step 1 − Import the fmt, big and rand packages.

• Step 2 − Create a getNumerator() function.

• Step 3 − Call the main() function.

• Step 4 − Call the getNumerator() function.

• Step 5 − Initialize a variable to store the rational number in it. Use big.NewRat() to create the rational number.

• Step 6 − Use the Num() function to get the numerator of the rational number.

• Step 7 − Return this value.

• Step 8 − Store the result and print it on the screen.

### Example

The source code to get the numerator of a rational number using two functions is given below −

// Including the main package
package main

// Importing fmt and math/big
import (
"fmt"
"math/big"
"math/rand"
)
// fmt package allows us to print anything on the screen.
// big package allows us to use various predefined mathematical methods like a rat
// rand package allows us to generate random numbers.

// Initializing the getNumerator() function.
func getNumerator() *big.Int {
// NewRat creates a new Rational number with numerator a and denominator b
number := big.NewRat(int64(rand.Intn(200)), int64(rand.Intn(200)))

// Printing the result
fmt.Println("The rational number is", number)

// getting the numerator of the rational number
// storing the result in a result variable
result := number.Num()

// returning the result
return result
}
// Calling main
func main() {
fmt.Println("Golang Program to get the numerator of a rational number")

// calling the getNumerator() function
result := getNumerator()

// printing the numerator on the screen
fmt.Println("The numerator of rational number is:", result)
}


### Output

Golang Program to get the numerator of a rational number
The rational number is 27/29
The numerator of rational number is: 27


### Description

• Import the fmt, big and rand packages.

• fmt package allows us to print anything on the screen. The big package allows us to use various predefined mathematical methods like rat and the rand package allows us to generate random numbers.

• Create and define the getNumerator() function. This function will generate the rational number and will return its numerator.

• Here to generate the rational number we have used NewRat() method. We have used RandInt() function to generate random integer values up to 200 which act like inputs to this function in the form of numerator and denominator.

• We have converted the 32-bit int number generated in this way to 64-bits because NewRat() accepts 64-bit values as input.

• Use Num() function to get the value of the numerator and store the result in a variable.

• Return this value.

• Print the final results on the screen by using fmt.Printf() function.

Updated on: 22-Dec-2022

166 Views

##### Kickstart Your Career

Get certified by completing the course