Senin, 13 April 2020

SMALL BASIC: Factorial Calculator

Hi, SMALL Basic programmer, welcome.

Did you know, we can also make factorial calculator using SMALL BASIC?

We will divide the code as:
1. Code with wrong result of  0!
2. Code that with correct result of 0!
So, you can see the difference.

Below is the code:

1. Code with wrong result of 0!
TextWindow.WriteLine("Welcome to factorial calculator")
TextWindow.WriteLine("Enter the n: " )
n = TextWindow.Read()
res = n
For i = n-1 To 1 Step -1
  res = res * i
EndFor

TextWindow.WriteLine(res)


2. Code with correct result of 0!
This below is completed program:

TextWindow.WriteLine("Welcome to factorial calculator.")
TextWindow.WriteLine("Enter the n: " )
n = TextWindow.Read()
res = n

If n = 0 Then
  res = 1
Else
 
For i = n-1 To 1 Step -1
  res = res * i
EndFor
EndIf

TextWindow.WriteLine("The result of permutation is: " + res)

Tidak ada komentar:

Posting Komentar