Find Division Upto Given Decimal Places

Easy
0/40
Average time to solve is 15m
profile
Contributed by
31 upvotes
Asked in companies
MicrosoftIBM

Problem statement

You are given three integers ‘X’, ‘Y’ and ‘N’. Find the value of division X / Y up to ‘N’ decimal places.

You should return a string that represents the value X / Y up to ‘N’ decimal places. This string must have ‘N’ digits after decimals. You don’t need to round off the result to ‘N’ decimal places, just find the first ‘N’ places after decimals.

Note:

1. It is guaranteed that division X / Y is always finite.

Example:

Consider X = 5, Y = 4 and N = 5, then you should return “1.25000”. 
Note, here we add 3 trailing zeros so that it has exactly 5 digits after decimals.    
Detailed explanation ( Input/output format, Notes, Images )
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases, then ‘T’ test cases follow.

The first and only line of each test case consists of three space-separated integers, ‘X’, ‘Y’, ‘N’ respectively.    
Output format :
For each test case, print in a separate line, a string representing the value X / Y  up to ‘N’ decimal places, and it must have ‘N’ digits after decimals.

Note:

You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 50
-10^8  <= X <= 10^8
-10^8  <= Y <= 10^8
Y != 0
1 <= N <= 10^4

Time limit: 1 sec
Sample Input 1:
2
5 4 1
5 4 5   
Sample Output 1:
1.2
1.25000
Explanation of Sample Input 1:
Test case 1:
Division 5/4 = 1.25, but we need to print only ‘1’ digit after decimals, thus we should print 1.2.

Test case 2:
See the problem statement for an explanation.
Sample Input 2:
3
-1 -2 1
1 -5 6
-1 1000 2
Sample Output 2:
0.5
-0.200000
-0.00
Hint

Repeatedly multiply the remainder by 10 and divide it with the given dividend to find digits after the decimal.

Approaches (1)
Standard Division Algorithm

Following is the detailed algorithm :

 

  • Create an empty string ‘RESULT’.
  • If exactly one of ‘X’ or ‘Y’ is negative then append ‘-’ in ‘RESULT’.
  • Do floor division of the absolute value of ‘X’ by the absolute value of  ‘Y’, i.e abs(‘X’) / abs(‘Y’), and append the value obtained in ’RESULT’ after converting it into string.
  • Append ‘.’ in ‘RESULT’.
  • Initialize an integer ‘REM’ := abs(‘X’) % abs(‘Y’).
  • Run a loop where ‘i’ ranges from 1 to ‘N’ and in each iteration do the following
    • Multiiply ‘REM’ by 10, i.e do ‘REM’ := ‘REM’ * 10.
    • Initialize an integer variable ‘DIGIT’ by the value obtained by floor division of ‘REM’ by the absolute value of ‘Y’, i.e  ‘REM’ := floor( ‘REM’ / abs(‘Y’) );
    • Do ‘REM’ := ‘REM’ % abs(‘Y’).
    • Append ‘DIGIT’ in ‘RESULT’ after converting it into the string.
  • Return ‘RESULT’.
Time Complexity

O(N),  where ‘N’ is the number of digits after the decimal. 

 

We are iterating on the number of digits after decimal. Thus the time complexity will be O(N).

Space Complexity

O(N),  where ‘N’ is the number of digits after the decimal. 

 

The length of the string ‘RESULTwill be of order O(N). Thus, overall space complexity will be O(N).

Code Solution
(100% EXP penalty)
Find Division Upto Given Decimal Places
Full screen
Console