Computer Science, asked by nightread, 3 months ago

Can we program Qbasic to give answers in fractions? If yes, then how?

Answers

Answered by Anonymous
1

Answer:

The only way I could think ouf would be:

IF x <> INT(x) THEN 'check to see if its a decimal

FOR top = 1 TO 500000

FOR btm = 1 TO 500000

IF top / btm = x THEN GOTO prnt

NEXT

NEXT

END IF

prnt: PRINT STR$(top) + "/" + STR$(btm)

This would be incredibly slow though, i would just leave it as a decimal

Hope it helps you

Explanation:

Answered by EvelynAdair
1

Answer:

a way in QBasic to express a decimal answer (x) as a fraction? So instead of printing .25, it would print 1/4? Thanks

The (Programmer)(OP)8 Nov 02 19:31

Or, if there isn't a way to do that, is there a way to check if x is a decimal? (yes, I am a newbie)

qbasicking (Programmer)8 Nov 02 22:25

The only way I could think ouf would be:

IF x <> INT(x) THEN 'check to see if its a decimal

FOR top = 1 TO 500000

FOR btm = 1 TO 500000

IF top / btm = x THEN GOTO prnt

NEXT

NEXT

END IF

prnt: PRINT STR$(top) + "/" + STR$(btm)

This would be incredibly slow though, i would just leave it as a decimal

Pappy1942 (TechnicalUser)12 Nov 02 07:51

Hi,

Using STR$, MID$, INSRT, and VAL you can convert the number to a string, extract the decimal part, convert it back to a whole number and figure out the denominator. The denominator will alwasy be a power of ten. For instance .101 is equal to 101/1000.

Hope this gives you a clue.

BTW you have to reduce to get 1/4 from .25 (25/100).

Pappy

You learn new something everyday.

logiclrd (Programmer)6 Dec 02 13:40

I wrote a program a long time ago that estimates a decimal value using an integer fraction. It works on the following principle: if you invert the fraction, then you are left with an expression of the form 1/x, where x is the *inverse* of the fraction. Furthermore, if you take away the integer part of the fraction, then you are left with another fraction to compensate for, so x is of the form (a + y), where a is the integer part and y is the compensation for the fractional part. You can then repeat with 'y' and end up with an expression of the form:

0 + 1

-------------------

5 + 1

-------------

2 + 1

-----

...

(for the fraction 0.18723 -- this approximation yields 0.1875 after 4 levels, the above fraction simplifying to 3/16). This nested fraction is easy to convert back to a simple fraction: you simply start with the deepest nesting, incorporate the integer part into the fraction, then "invert and multiply" to do the division. The result is one less level with the same structure.

Here is the program itself:

Similar questions