need help with basic program.trying to write a program in qbasic to take in a integer calculate and print out the factorial.been on this for a long time

Dani AI

Generated

A compact, practical summary for computing factorials in QBasic plus fixes and options mentioned in the thread. asked for a QBasic solution; was right to ask for posted code. demonstrated the loop algorithm in VB but forgot to initialize the accumulator (it must start at 1). 's suggestion to use logarithms to avoid overflow is sound — in IEEE double-precision the factorial overflows around n = 170, so exact results beyond that require big-integer arithmetic.

A minimal, correct QBasic iterative version (works up to the double limit):

' Simple QBasic factorial (small n only)
INPUT "Enter non-negative integer: "; n%
IF n% < 0 THEN
  PRINT "Factorial undefined for negative numbers."
  END
END IF

result# = 1#
FOR i% = 1 TO n%
  result# = result# * i%
NEXT i%

PRINT n%; "! = "; result#

To avoid trying to print an enormous value use logs to get magnitude or number of digits without computing n! exactly:

' Count digits of n! (base-10)
sum# = 0#
FOR k% = 1 TO n%
  sum# = sum# + LOG(k%) / LOG(10)   ' use LOG10 if available
NEXT k%
digits% = INT(sum#) + 1
PRINT "Digits in "; n%; "! = "; digits%

For exact factorials beyond the double range implement arbitrary-precision arithmetic (big integers). Store digits in an array (least-significant first) and repeatedly multiply by the next integer, carrying as you go — the sketch below shows the core loop used in QBasic-style code:

DIM D(2000)
D(1) = 1
len% = 1
FOR k% = 2 TO n%
  carry% = 0
  FOR i% = 1 TO len%
    prod% = D(i%) * k% + carry%
    D(i%) = prod% MOD 10
    carry% = prod% \ 10
  NEXT i%
  WHILE carry% > 0
    len% = len% + 1
    D(len%) = carry% MOD 10
    carry% = carry% \ 10
  WEND
NEXT k%

Troubleshooting: always initialize the accumulator to 1, handle 0! = 1 and negative input explicitly, test with small values first, and prefer an iterative big-int approach in QBasic (stack depth is limited, so recursion is not ideal).

Recommended Answers

All 3 Replies

could you possibly post the code you have so we can tell exactly what you need help with. that would make it easier to help up thanks :)

I haven't coded in QBasic in years, but in VB it would be like the following:

Dim intNumber As Integer
Dim lngFactorial As Long
Dim lngCounter As Long
 
intNumber = 5
 
For lngCounter = 1 To intNumber
  lngFactorial = lngFactorial * lngCounter
Next lngCounter

"lngFactorial" would then be your number. Is this what you're looking for?

If you do not use logarithmus, you will be getting overflow error for factorial of integers larger then approx. 280.
This is VB6 code:
Dim l As Long
Dim res As Double
For l = 1 To total
res = res + Log(CDbl(l))
Next


The final result "res" is used for furhter mathematical operation, rather then directly printing its value val= Exp(res), since if val is too high, you would just get the overflow error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.