Write a program to find factorial of a number in Qbasic.
For example,
Input 4 Output 24 Explanation 4! = 4 * 3 * 2 * 1 = 24
Steps
- First, the program asks the user to enter a number – n.
- Then we initialize a variable fact = 1.
- After that, we use a loop and multiply fact with each number from 1 to n.
- The factorial of n is equal to fact.
Factorial Program in QBasic
CLS
INPUT "Enter a Number: ", n
fact = 1
FOR i = 1 TO n
fact = fact * i
NEXT i
PRINT "Factorial of ";n;" is ";fact
END
Factorial Program in QBasic – Using Subroutine
DECLARE SUB factorial
CLS
factorial
END
SUB factorial
INPUT "Enter a Number: ", n
fact = 1
FOR i = 1 TO n
fact = fact * i
NEXT i
PRINT "Factorial of ";n;" is ";fact
END SUB
Factorial Program in QBasic – Using Function
DECLARE FUNCTION factorial(n)
CLS
INPUT "Enter a Number: ", n
PRINT "Factorial of ";n;" is ";factorial(n)
END
FUNCTION factorial(n)
fact = 1
FOR i = 1 TO n
fact = fact * i
NEXT i
factorial = fact
END FUNCTION
Factorial Program in QBasic – With Recursion
DECLARE FUNCTION factorial(n)
CLS
INPUT "Enter a Number: ", n
PRINT "Factorial of ";n;" is ";factorial(n)
END
FUNCTION factorial(n)
IF n = 0 OR n = 1 THEN
factorial = 1
ELSE
factorial = n * factorial(n-1)
END IF
END FUNCTION
Output
Enter a Number: 5
Factorial of 5 is 120
Read
Pretty good actually . If only output was also given.
Sample output is given at the end. All programs have the same output.
how to find the factorial from real numbers
To find factorial of real numbers, you will need to write a program that can perform integration which can be complex.
For approminate results, you can directly use Stirling’s approximation
https://en.wikipedia.org/wiki/Stirling%27s_approximation
H.W :write a program to compute the factorial from real numbers
Can you help me I needed a program to solve this problem in QBasic
Just google Stirling’s approximation and use the formula.
It is direct n! = ((2*3.14*n)^(1/2))*((n/2.71)^n)
Good