- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Haskell Program to get the denominator from a rational number
In Haskell, we can use denominator, div, quot and gcd functions to obtain the denominator from a rational number. In the first example we are going to use (denominator r) function and in the second example, we are going to use (d `div` gcd n d) function.
Algorithm
Step 1 − The Data.Ratio module is imported to use denominator function.
Step 2 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. It calls the denominator function with the rational number and prints the denominator.
Step 3 − The variable named, “r” is being initialized. It will hold the rational number value whose denominator is to be printed.
Step 4 − The resultant denominator value is printed to the console using ‘putStrLn’ statement after the function is called.
Example 1
In this example, we are going to see that how we can get the denominator from the rational number by using denominator function.
import Data.Ratio main :: IO () main = do let r = 3 % 4 let num = denominator r putStrLn $ "The denominator of " ++ show r ++ " is: " ++ show num
Output
The denominator of 3 % 4 is: 4
Example 2
In this example, we are going to see that how we can get the denominator from the rational number by using div and gcd function.
import Data.Ratio getdenominator :: Rational -> Integer getdenominator r = d `div` gcd n d where n = numerator r d = denominator r main :: IO () main = do let r = 3 % 4 let num = getdenominator r putStrLn $ "The denominator of " ++ show r ++ " is: " ++ show num
Output
The denominator of 3 % 4 is: 4
Example 3
In this example, we are going to see that how we can get the denominator from the rational number by using quot and gcd function.
import Data.Ratio getdenominator :: Rational -> Integer getdenominator r = denominator r `quot` gcd (numerator r) (denominator r) main :: IO () main = do let r = 3 % 4 let num = getdenominator r putStrLn $ "The denominator of " ++ show r ++ " is: " ++ show num
Output
The denominator of 3 % 4 is: 4
Conclusion
The denominator of a rational number is the bottom part of the fraction. In other words, it is the number by which the numerator is divided with.