# Thread: Fraction outputted as decimal

1. ## Fraction outputted as decimal

Hello I have wrriten a recursive program that will display a fraction, where the numerator is less than the denominator, out to a user specificed number of decimal places. My program works for numbers like 1/7 35/46 etc. But it doesnt work for some cases. If i try to output 1/10 or 1/100 it doesnt display the one, it will out put 0.00 and not the 1 that should be at the end. I also get a stack overflow when i try to do 2/8,1/4, and some other cases. I cant figure out why these things are happening. You can run the program yourself and check it out. Any help is appreciated. Thanks

Code:
```// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;

void divide(int,int,int,Int32 []);
void print(Int32[]);
int _tmain()
{
int n, d,size,i=0;	//Variable declarations, i is index for array

Console::Write(S"Input A Numerator:");
Console::Write(S"Input A Denominator(Bigger Than Numerator):");
Console::Write(S"Enter number of Decimal Places Desired:");

//Store The Answer In This Array
Int32 a[]=new Int32[size];

divide(n,d,i,a);
print(a);

return 0;
}

//Recursive Division Function
void divide(int n,int d, int i, Int32 a[]) {

if(n<d)
return divide(n*10,d,i,a);
if(n>d && i<a->Length) {
a[i]=n/d;
return divide(n%d,d,i+1,a);
}
}

//Print Array
void print(Int32 d[]) {

for (int i=0; i!=d->Length; i++)
Console::Write(S"{0}", d[i].ToString());
Console::WriteLine();
}```

2. Originally Posted by blindman858
void divide(int,int,int,Int32 []);
void print(Int32[]);

void divide(int n,int d, int i, Int32 a[]) {

if(n<d)
return divide(n*10,d,i,a);
if(n>d && i<a->Length) {
a[i]=n/d;
return divide(n%d,d,i+1,a);
}
}
[/CODE]
Hi,

I think you should try dividing two Doubles and not Integers. Your array should also be Double.
If you are trying to do something like
Code:
`int a = 1/10;`
the compiler will do 1/10 = (int) 0,1 = 0

3. If i use two doubles I cant use the modulus operator.

4. Ok I figured out why numbers like 1/100 wont print correctly. Its not the data type actually. I have to add another case in the recursion,

Code:
```if(n==d)
a[i]=1;```