
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Denominator from a Rational Number in Golang
In this article, we will discuss how to get the denominator from a rational number.
Rational numbers ? In mathematics, rational numbers are defined as numbers that can be expressed in the form of a/b where a and b are integer values. For example- 1/3, 5/8 etc.
Note that the denominator of a rational number can never be zero.
Syntax
func NewRat(a, b int64) *Rat func (x *Rat) Denom() *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 rational number.
Denom() function in the Go language returns the denominator of a rational number as the result in integer format.
Method-1
Go language program to get the denominator 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 denom() function to get the denominator of the rational number.
Step 5 ? Store the result and print them on the screen
Example
The source code to get the denominator 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 Denom() // Calling main func main() { fmt.Println("Golang Program to get the denomenator of a rational number") // NewRat creates a new Rational number with numerator a and denominator b number := big.NewRat(6, 8) // Printing the result on the screen fmt.Println("The rational number is", number) // getting the denomenator of the rational number // storing the result in a result variable result := number.Denom() // printing the denomenator on the screen fmt.Println("The denomenator of rational number", number, "is: ", result) }
Output
Golang Program to get the denomenator of a rational number The rational number is 3/4 The denomenator of rational number 3/4 is: 4
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 NewRat() function defined in 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 denominator of this number and print it on the screen.
To get the denominator use the Denom() function present in the big package. This function returns the denominator as the result in int format.
Store the value of result in a variable.
Print the result on the screen using println() function.
Method-2
Go language program to get the denominator from a rational number using two functions.
Algorithm
Step 1 ? Import the fmt, big and rand packages.
Step 2 ? Create a getDenominator() function.
Step 3 ? Call the main() function.
Step 4 ? Call the getDenominator() function.
Step 5 ? Initialize a variable to store the rational number in it. Use big.NewRat() to create the rational number.
Step 6 ? Use denom() function to get the denominator 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 denominator 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 rat // rand package allows us to generate random numbers. // Initializing the getDenominator() function. func getDenominator() *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 denomenator of the rational number // storing the result in a result variable result := number.Denom() // returning the result return result } // Calling main func main() { fmt.Println("Golang Program to get the denomenator of a rational number") // calling the getDenominator() function result := getDenominator() // printing the denomenator on the screen fmt.Println("The denomenator of rational number is:", result) }
Output
Golang Program to get the denomenator of a rational number The rational number is 27/29 The denomenator of rational number is: 29
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 getDenominator() function. This function will generate the rational number and will return its denominator.
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 Denom() function to get the value of the denominator and store the result in a variable.
Return this value.
Print the final results on the screen by using fmt.Printf() function.