Computer Code

The following is actual computer code to compute factorials in the 4 most used computer languages (Java, Python, PHP, C#) and Scratch:

Scratch, that's what we will be learning!

Java:

public class Factorial { public static void main(String[] args) { final int NUM_FACTS = 100; for(int i = 0; i < NUM_FACTS; i++) System.out.println( i + "! is " + factorial(i)); } public static int factorial(int n) { int result = 1; for(int i = 2; i <= n; i++) result *= i; return result; } }

Python

# Python program to find the factorial of a number provided by the user.# take input from the user num = int(input("Enter a number: ")) factorial = 1# check if the number is negative, positive or zeroif num < 0: print("Sorry, factorial does not exist for negative numbers")elif num == 0: print("The factorial of 0 is 1")else: for i in range(1,num + 1): factorial = factorial*i print("The factorial of",num,"is",factorial)

Scratch

PHP

function factorial($number) { if ($number < 2) { return 1; } else { return ($number * factorial($number-1)); } }

C#

int numberInt = int.Parse(factorialNumberTextBox.Text);for (int i = 1; i < numberInt; i++){ numberInt = numberInt * i;} factorialAnswerTextBox.Text = numberInt.ToString();