# Program to compute division upto n decimal places

Last Updated : 17 Feb, 2023

Given 3 numbers x, y and n compute the division (x/y) upto n decimal places.

Examples :

Input  : x = 22, y = 7, n = 10
Output : 3.1428571428
Explanation : Since n = 10, division (x / y) is taken till 10 decimal places.

Input  : x = 22, y = 7, n = 20
Output : 3.14285714285714285714

Approach :

1. Get the remainder and get it subtracted by the dividend, multiply it by ten and go to the next iteration.
2. If we reach the complete result, we may not need to continue until the pre-defined number of iterations are reached.

Below is the implementation of the above approach:

## C++

 `// CPP program to compute division upto n ` `// decimal places. ` `#include ` `using` `namespace` `std; ` ` `  `void` `precisionCompute(``int` `x, ``int` `y, ``int` `n) ` `{ ` `    ``// Base cases ` `    ``if` `(y == 0) { ` `        ``cout << ``"Infinite"` `<< endl; ` `        ``return``; ` `    ``} ` `    ``if` `(x == 0) { ` `        ``cout << 0 << endl; ` `        ``return``; ` `    ``} ` `    ``if` `(n <= 0) { ` `        ``// Since n <= 0, don't compute after ` `        ``// the decimal ` `        ``cout << x / y << endl; ` `        ``return``; ` `    ``} ` ` `  `    ``// Handling negative numbers ` `    ``if` `(((x > 0) && (y < 0)) || ((x < 0) && (y > 0))) { ` `        ``cout << ``"-"``; ` `        ``x = x > 0 ? x : -x; ` `        ``y = y > 0 ? y : -y; ` `    ``} ` ` `  `    ``// Integral division ` `    ``int` `d = x / y; ` ` `  `    ``// Now one by print digits after dot ` `    ``// using school division method. ` `    ``for` `(``int` `i = 0; i <= n; i++) { ` `        ``cout << d; ` `        ``x = x - (y * d); ` `        ``if` `(x == 0) ` `            ``break``; ` `        ``x = x * 10; ` `        ``d = x / y; ` `        ``if` `(i == 0) ` `            ``cout << ``"."``; ` `    ``} ` `} ` ` `  `// Driver Program ` `int` `main() ` `{ ` `    ``int` `x = 22, y = 7, n = 15; ` `    ``precisionCompute(x, y, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to compute division upto n ` `// decimal places. ` `import` `java.util.*; ` ` `  `class` `Eulerian { ` `    ``public` `static` `void` `precisionCompute(``int` `x, ``int` `y, ``int` `n) ` `    ``{ ` `        ``// Base cases ` `        ``if` `(y == ``0``) { ` `            ``System.out.print(``"Infinite"``); ` `            ``return``; ` `        ``} ` `        ``if` `(x == ``0``) { ` `            ``System.out.print(``"0"``); ` `            ``return``; ` `        ``} ` `        ``if` `(n <= ``0``) { ` `            ``// Since n <= 0, don't compute after ` `            ``// the decimal ` `            ``System.out.print(x / y); ` `            ``return``; ` `        ``} ` ` `  `        ``// Handling negative numbers ` `        ``if` `(((x > ``0``) && (y < ``0``)) || ((x < ``0``) && (y > ``0``))) { ` `            ``System.out.print(``"-"``); ` `            ``x = x > ``0` `? x : -x; ` `            ``y = y > ``0` `? y : -y; ` `        ``} ` ` `  `        ``// Integral division ` `        ``int` `d = x / y; ` ` `  `        ``// Now one by print digits after dot ` `        ``// using school division method. ` `        ``for` `(``int` `i = ``0``; i <= n; i++) { ` `            ``System.out.print(d); ` `            ``x = x - (y * d); ` `            ``if` `(x == ``0``) ` `                ``break``; ` `            ``x = x * ``10``; ` `            ``d = x / y; ` `            ``if` `(i == ``0``) ` `                ``System.out.print(``"."``); ` `        ``} ` `    ``} ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `x = ``22``, y = ``7``, n = ``15``; ` `        ``precisionCompute(x, y, n); ` `    ``} ` `} ` ` `  `// This code is contributed by rishabh_jain `

## C#

 `// C# program to compute division ` `// upto n decimal places. ` `using` `System; ` ` `  `class` `Eulerian { ` ` `  `    ``public` `static` `void` `precisionCompute(``int` `x, ``int` `y, ` `                                        ``int` `n) ` `    ``{ ` `        ``// Base cases ` `        ``if` `(y == 0) { ` `            ``Console.WriteLine(``"Infinite"``); ` `            ``return``; ` `        ``} ` `        ``if` `(x == 0) { ` `            ``Console.WriteLine(``"0"``); ` `            ``return``; ` `        ``} ` `        ``if` `(n <= 0) { ` ` `  `            ``// Since n <= 0, don't compute after ` `            ``// the decimal ` `            ``Console.WriteLine(x / y); ` `            ``return``; ` `        ``} ` ` `  `        ``// Handling negative numbers ` `        ``if` `(((x > 0) && (y < 0)) || ((x < 0) && (y > 0))) { ` `            ``Console.WriteLine(``"-"``); ` `            ``x = x > 0 ? x : -x; ` `            ``y = y > 0 ? y : -y; ` `        ``} ` ` `  `        ``// Integral division ` `        ``int` `d = x / y; ` ` `  `        ``// Now one by print digits after dot ` `        ``// using school division method. ` `        ``for` `(``int` `i = 0; i <= n; i++) { ` `            ``Console.Write(d); ` `            ``x = x - (y * d); ` `            ``if` `(x == 0) ` `                ``break``; ` `            ``x = x * 10; ` `            ``d = x / y; ` `            ``if` `(i == 0) ` `                ``Console.Write(``"."``); ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `x = 22, y = 7, n = 15; ` `        ``precisionCompute(x, y, n); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m `

## PHP

 ` 0) && (``\$y` `< 0)) || ` `        ``((``\$x` `< 0) && (``\$y` `> 0))) ` `    ``{ ` `        ``echo` `"-"``; ` `        ``\$x` `= ``\$x` `> 0 ? ``\$x` `: -``\$x``; ` `        ``\$y` `= ``\$y` `> 0 ? ``\$y` `: -``\$y``; ` `    ``} ` ` `  `    ``// Integral division ` `    ``\$d` `= ``\$x` `/ ``\$y``;  ` ` `  `    ``// Now one by print digits after dot ` `    ``// using school division method. ` `    ``for` `(``\$i` `= 0; ``\$i` `<= ``\$n``; ``\$i``++)  ` `    ``{ ` `        ``echo` `\$d``; ` `        ``\$x` `= ``\$x` `- (``\$y` `* ``\$d``); ` `        ``if` `(``\$x` `== 0) ` `            ``break``;  ` `        ``\$x` `= ``\$x` `* 10; ` `        ``\$d` `= ``\$x` `/ ``\$y``; ` `        ``if` `(``\$i` `== 0) ` `            ``echo` `"."``; ` `    ``} ` `} ` ` `  `// Driver Code ` `\$x` `= 22; ``\$y` `= 7; ``\$n` `= 15; ` ` `  `precisionCompute(``\$x``, ``\$y``, ``\$n``); ` ` `  `// This code is contributed by aj_36  ` `?> `

## Python3

 `# Python3 program to compute  ` `# division upto n decimal places. ` ` `  `def` `precisionCompute(x, y, n): ` `     `  `    ``# Base cases ` `    ``if` `y ``=``=` `0``: ` `        ``print``(``"Infinite"``); ` `        ``return``; ` `    ``if` `x ``=``=` `0``: ` `        ``print``(``0``); ` `        ``return``; ` `    ``if` `n <``=` `0``: ` `         `  `        ``# Since n <= 0, don't ` `        ``# compute after the decimal ` `        ``print``(x ``/` `y); ` `        ``return``; ` `         `  `    ``# Handling negative numbers ` `    ``if` `(((x > ``0``) ``and` `(y < ``0``)) ``or`  `        ``((x < ``0``) ``and` `(y > ``0``))): ` `        ``print``(``"-"``, end ``=` `""); ` `        ``if` `x < ``0``: ` `            ``x ``=` `-``x; ` `        ``if` `y < ``0``: ` `            ``y ``=` `-``y; ` `             `  `    ``# Integral division ` `    ``d ``=` `x ``/` `y; ` `     `  `    ``# Now one by print digits  ` `    ``# after dot using school  ` `    ``# division method. ` `    ``for` `i ``in` `range``(``0``, n ``+` `1``): ` `        ``print``(d); ` `        ``x ``=` `x ``-` `(y ``*` `d); ` `        ``if` `x ``=``=` `0``: ` `            ``break``; ` `        ``x ``=` `x ``*` `10``; ` `        ``d ``=` `x ``/` `y; ` `        ``if` `(i ``=``=` `0``): ` `            ``print``(``"."``, end ``=` `""); ` ` `  `# Driver Code ` `x ``=` `22``; ` `y ``=` `7``; ` `n ``=` `15``; ` `precisionCompute(x, y, n); ` ` `  `# This code is contributed by mits `

## Javascript

 ` `

Output :

`3.142857142857142`

Time complexity: O(n)
Auxiliary space: O(1)

Previous
Next