41 lines
914 B
C#
41 lines
914 B
C#
// See https://aka.ms/new-console-template for more information
|
|
//dichiarazione e inizializzazione variabili
|
|
int input, fattoriale, i, j;
|
|
i = 1;
|
|
j = 1;
|
|
fattoriale = 1;
|
|
|
|
//input e input sanitization
|
|
do
|
|
{
|
|
Console.Write("Inserisci un numero: ");
|
|
input = Convert.ToInt32(Console.ReadLine());
|
|
if (input <= 0)
|
|
{
|
|
Console.WriteLine("Il numero inserito non può essere né zero né negativo");
|
|
}
|
|
}
|
|
while (input <= 0);
|
|
//inizio calcolo
|
|
while (i <= input)
|
|
{
|
|
if (i == 1)
|
|
{
|
|
fattoriale = 1;
|
|
}
|
|
else
|
|
{
|
|
fattoriale = i;
|
|
while(i-j > 1)
|
|
{
|
|
fattoriale = fattoriale * (i-j);
|
|
j++;
|
|
}
|
|
j = 0; //reset contatore
|
|
fattoriale = fattoriale / i; //fix per correggere un giro extra
|
|
}
|
|
//output
|
|
Console.WriteLine("Fattoriale di " + i + ": " + fattoriale);
|
|
i++;
|
|
}
|