# Count number of digits after decimal on dividing a number

We are given two numbers A and B. We need to calculate the number of digits after decimal. If in case the numbers are irrational then print “INF”.

Examples:

Input : x = 5, y = 3 Output : INF 5/3 = 1.666.... Input : x = 3, y = 6 Output : 1 3/6 = 0.5

The idea is simple we follow school division and keep track of remainders while dividing one by one. If remainder becomes 0, we return count of digits seen after decimal. If remainder repeats, we return INF.

`// CPP program to count digits after dot when a ` `// number is divided by another. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `count(` `int` `x, ` `int` `y) ` `{ ` ` ` `int` `ans = 0; ` `// Initialize result ` ` ` ` ` `unordered_map<` `int` `, ` `int` `> m; ` ` ` ` ` `// calculating remainder ` ` ` `while` `(x % y != 0) { ` ` ` ` ` `x = x % y; ` ` ` ` ` `ans++; ` ` ` ` ` `// if this remainder appeared before then ` ` ` `// the numbers are irrational and would not ` ` ` `// converge to a solution the digits after ` ` ` `// decimal will be infinite ` ` ` `if` `(m.find(x) != m.end()) ` ` ` `return` `-1; ` ` ` ` ` `m[x] = 1; ` ` ` `x = x * 10; ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `res = count(1, 2); ` ` ` `(res == -1)? cout << ` `"INF"` `: cout << res; ` ` ` ` ` `cout << endl; ` ` ` `res = count(5, 3); ` ` ` `(res == -1)? cout << ` `"INF"` `: cout << res; ` ` ` ` ` `cout << endl; ` ` ` `res = count(3, 5); ` ` ` `(res == -1)? cout << ` `"INF"` `: cout << res; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

1 INF 1

This article is contributed by **Rahul Chawla**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Number of digits before the decimal point in the division of two numbers
- Find count of digits in a number that divide the number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Given a number N in decimal base, find number of its digits in any base (base b)
- Count total number of digits from 1 to n
- Count digits in given number N which divide N
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count Numbers with N digits which consists of odd number of 0's
- Count Numbers with N digits which consists of even number of 0’s
- Count the number of digits of palindrome numbers in an array
- Count ways to spell a number with repeated digits
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Find the Largest number with given number of digits and sum of digits
- Number of digits to be removed to make a number divisible by 3
- Print a number strictly less than a given number such that all its digits are distinct.