Faculteit (wiskunde): verschil tussen versies

Verwijderde inhoud Toegevoegde inhoud
zonder dit is het volgens mij nog even juist
Madyno (overleg | bijdragen)
→‎Met de computer: 1 proramma is genoeg
Regel 124:
{{Clearboth}}
== Met de computer ==
Het onderstaande programaatje in [[Python (programmeertaal)|Python]] berekent van een ingevoerd getal de faculteit.
Voorbeeld in de programmeertaal [[PHP]] dat een HTML-fragment produceert:
<source lang="php">
$max = 20;
$nArray[0] = 1;
 
for ($n = 1; $n <= $max; $n++)
$nArray[$n] = ($nArray[($n - 1)] * $n);
 
foreach ($nArray as $key => $value)
print $key . ' ' . $value . '<br />';
</source>
In [[C++]]:
<source lang="cpp">
int input = 10;
int antwoord = 1;
for (int i = 1; i <= input; i++)
antwoord *= i;
std::cout << "De faculteit van " << input << " is: " << antwoord;
</source>
 
In [[Visual Basic]]
<source lang="vb">
Dim intA As Double
Dim intB As Integer
Dim intC As Double
intA = 10
intC = 1
For intB = 2 To intA
intC = intC * intB
Next
MsgBox ("De faculteit van " & intA & " is: " & intC)
</source>
 
In [[Python (programmeertaal)|Python]]
<source lang="python">
Getal = int(input())
Regel 166 ⟶ 133:
 
print("De faculteit van het ingevoerde getal is: ",fac, end="")
</source>
 
In [[Java (programmeertaal)|Java]]
<source lang="java">
public class Main {
public static void main(String[] args)
{
System.out.println(faculteit(6));
}
public static long faculteit(int getal){
long antwoord = 1;
for(int i = 1; i <= getal; i++){
antwoord *= i;
}
return antwoord;
}
}
</source>
 
In [[Java (programmeertaal)|Java]] (recursief)
<source lang="java">
public class Main {
public static void main(String[] args)
{
System.out.println(faculteit(6));
}
public static long faculteit(int getal){
if(getal >= 1){
return getal * faculteit((getal-1));
} else {
return 1;
}
}
}
</source>