Count number of digits after decimal on dividing a number | CPP program to count digits after dot when a number is divided by another . ; int ans = 0 ; Initialize result ; calculating remainder ; if this remainder appeared before then the numbers are irrational and would not converge to a solution the digits after decimal will be infinite ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int x , int y ) { unordered_map < int , int > m ; while ( x % y != 0 ) { x = x % y ; ans ++ ; if ( m . find ( x ) != m . end ( ) ) return -1 ; m [ x ] = 1 ; x = x * 10 ; } return ans ; } 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 ; }