int Number=1;
Number=Convert.ToInt32(TextBox1.Text);
if(Number==0)
Label1.Text="enter a number greater than 0";
else
//int functionReturnValue = 0;
//functionReturnValue = 1;
for (int i = 0; i >Number-1; i--)
{
Number=Number*i;
}
Label1.Text=Convert.ToString(Number);
}
}
But i am unable to get the result help me with this??
It is not about what caused you to fall flat on your back that defines you; it is what you do to get back up on your feet.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Factorial
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" Give me the number you want the factorial: \n ");
long a = Convert.ToInt64(Console.ReadLine());
Console.WriteLine(" The factorial of given number is: " + Factorial(a));
Console.ReadKey();
}
public static long Factorial(long number)
{
if (number <= 1)
return 1;
else
return number * Factorial(number - 1);
}
}
}
Reply me for any issues..
protected void Page_Load(object sender, EventArgs e)
{
long Number = 1;
Number = Convert.ToInt64(TextBox1.Text);
if (Number == 0)
Label1.Text = "enter a number greater than 0";
else
{
Number = Factorial(Number);
}
Label1.Text = Convert.ToString(Number);
}
static long Factorial(long number)
{
if (number <= 1)
return 1;
else
return number * Factorial(number - 1);
}
the return part will be 3*Factorial (3-1) (i.e..) it ll be 3*Factorial(2)
and now for Factorial(2) the same process will occur until ot enters the
if block the same recursive process will continue and u ll get the required output.
.net_junkie
Member
297 Points
825 Posts
Factorial of a number
May 08, 2012 06:03 AM|LINK
This is my code to find a factorial
int Number=1; Number=Convert.ToInt32(TextBox1.Text); if(Number==0) Label1.Text="enter a number greater than 0"; else //int functionReturnValue = 0; //functionReturnValue = 1; for (int i = 0; i >Number-1; i--) { Number=Number*i; } Label1.Text=Convert.ToString(Number); } }But i am unable to get the result help me with this??
karthicks
All-Star
31382 Points
5424 Posts
Re: Factorial of a number
May 08, 2012 06:11 AM|LINK
hi, refer below code
int Number=1;
Number=Convert.ToInt32(TextBox1.Text);
if(Number==0)
Label1.Text="enter a number greater than 0";
else
//int functionReturnValue = 0;
//functionReturnValue = 1;
for (int i = Number; i >0; i--)
{
Number=Number*i;
}
Label1.Text=Convert.ToString(Number);
}
}
Karthick S
kirupa.v
Contributor
2070 Points
531 Posts
Re: Factorial of a number
May 08, 2012 06:13 AM|LINK
Hi,
Try the following code
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Factorial { class Program { static void Main(string[] args) { Console.WriteLine(" Give me the number you want the factorial: \n "); long a = Convert.ToInt64(Console.ReadLine()); Console.WriteLine(" The factorial of given number is: " + Factorial(a)); Console.ReadKey(); } public static long Factorial(long number) { if (number <= 1) return 1; else return number * Factorial(number - 1); } } }Reply me for any issues..
tusharrs
Contributor
3230 Points
668 Posts
Re: Factorial of a number
May 08, 2012 06:23 AM|LINK
protected void Page_Load(object sender, EventArgs e) { long Number = 1; Number = Convert.ToInt64(TextBox1.Text); if (Number == 0) Label1.Text = "enter a number greater than 0"; else { Number = Factorial(Number); } Label1.Text = Convert.ToString(Number); } static long Factorial(long number) { if (number <= 1) return 1; else return number * Factorial(number - 1); }( Mark as Answer if it helps you out )
View my Blog
.net_junkie
Member
297 Points
825 Posts
Re: Factorial of a number
May 08, 2012 06:29 AM|LINK
with your code i am getting the result as 0
and i want the code with recursion also can anyone provide me the code??
kirupa.v
Contributor
2070 Points
531 Posts
Re: Factorial of a number
May 08, 2012 06:30 AM|LINK
Hi,
Mine will give u the required result..
You can try with that ..
.net_junkie
Member
297 Points
825 Posts
Re: Factorial of a number
May 08, 2012 06:57 AM|LINK
hi can you explain me this logic??what really Factorial doing?
kirupa.v
Contributor
2070 Points
531 Posts
Re: Factorial of a number
May 08, 2012 07:13 AM|LINK
Hi,
Actually recurssively the process occurs..
For eg::
the input no : is 3
Initially it enters inside the else part
the return part will be 3*Factorial (3-1) (i.e..) it ll be 3*Factorial(2)
and now for Factorial(2) the same process will occur until ot enters the if block the same recursive process will continue and u ll get the required output.
tusharrs
Contributor
3230 Points
668 Posts
Re: Factorial of a number
May 08, 2012 07:28 AM|LINK
if you pass 6 to the function
then it checks whehter it is <=1 then its no
so it goes in else condition and in return statement its
number * factorial(number-1)
means number multiplied by the result of factorial(6-1) i.e factorial (5)
so it again calls factorial function with parameter 5
this way it calls the function till it goes to factorial(0)
and in factorial(0) it returns 1
and now it executes only the return statements so
finally 6 * factorial(5) i.e. 120 * factorial(4) i.e. 24 * factorial(3) i.e. 6 * factorial(2) i.e. 2 * factorail(1) i.e. 1
the result is 720
( Mark as Answer if it helps you out )
View my Blog
.net_junkie
Member
297 Points
825 Posts
Re: Factorial of a number
May 08, 2012 09:34 AM|LINK
So factorial is a method and we are using this code in other part and we are passing aprameters isnt it?