i use the following code for factorial is that correct? but it didn't show the result
{
class Program
{
static void Main(string[] args)
{
int number = int.Parse(Console.ReadLine());
for (int i = number; i ==1; --i)
{
number = number * (i);
Console.WriteLine(number);
}
}
}
}
3 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Abhay ShankerPosted Mar 5, 2014, 12:10 AM
int fact=1;
int number = int.Parse(Console.ReadLine());
for (int i = 1; i <=number ; i++)
{
fact = fact * i;
}
Console.WriteLine("\nFactorial of " + number + " = " + fact);
Console.ReadKey();
Jignesh TrivediPosted Mar 4, 2014, 11:10 PM
hi,
you can also use Recursive Functions to get factorial
static int Factorial(int n)
{
if (n <= 1)
return 1;
return n * Fact(n - 1);
}
static void Main(string[] args)
{
Console.Write(Factorial(7));
}
hope this will help you.
VulpesPosted Mar 4, 2014, 6:33 PM
So if the user enters 7, you'll see on the console: