Results 1 to 6 of 6

Thread: c# program for factorial

  1. #1
    Join Date
    Nov 2009
    Posts
    53

    c# program for factorial

    Hi Friends,

    I am learning the C# programming. In which I have to write the program for factorial in C sharp. I have one code for factorial but it giving me wrong output along with many runtime errors.
    If anyone has c# program for factorial then please provide me that. I would appreciate your help greatly

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: c# program for factorial

    I have following c sharp code for factorial of number. Please copy below program on your system and try to execute:
    private void fctCalculae_Click
    (object sender, System.EventArgs e)

    {

    long factr = 1;
    long numb = Convert.ToInt64 (txtNumb.Text);

    while ( numb > 0 && numb <= 40)

    {

    factr *= numb;

    numb++;

    }


    txtNumb.Text = "";

    txtNumb.Focus();

    }


    }

    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: c# program for factorial

    Hi friends,

    Below is the C# program code to find out the factorial of any number. I have used the function concept in this program code:
    public class cftr
    {
    static long numfact(long number)
    {
    if( n <= 1 )
    return 1;
    else
    return n * numfact(n - 1);
    }

    static int Main(string[] ars)
    {
    Console.Write("factorial = ",numfact(5));

    }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: c# program for factorial

    Hello,

    You need to implement the array concept in your code to calculate the factorial in C#. Below loop will give you some idea to implement the array. I have used the array within the for loop.
    for (int f = 0; f < nm; f++)
    array[f] = f+1;

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: c# program for factorial

    I recommend you to use the "While loop" . I have code the similar program for factorial but it was in the c programming. The programming logic will remains same for all programming language. So you can use the below "While loop" in your program:
    while(z>=1)
    {
    ft = ft*z;
    z = z-1;
    }

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: c# program for factorial

    c# program for factorial
    Class numft
    {
    Public Static Void Main(String[]as)
    {
    int a,b,c=1;

    Console.Write("Enter Number for factorial");
    b=Console.ReadLine();

    for(a=1;a<=b;a++)
    {

    c=c*a;

    }

    Console.Write("Factorial of number=",c);
    }
    }
    }

Similar Threads

  1. How to create factorial of numbers using sub procedure in vbscript
    By Gurseerat in forum Software Development
    Replies: 1
    Last Post: 04-02-2012, 12:21 PM
  2. n factorial program in C
    By MaryJ in forum Software Development
    Replies: 5
    Last Post: 24-09-2010, 09:59 PM
  3. How to write java program to find factorial of number?
    By Balamohan in forum Software Development
    Replies: 5
    Last Post: 28-11-2009, 10:14 PM
  4. What is the Program to find-out factorial of given number?
    By Sheenas in forum Software Development
    Replies: 5
    Last Post: 27-11-2009, 01:42 PM
  5. How to calculate factorial of a number in C++
    By AZUL in forum Software Development
    Replies: 2
    Last Post: 26-08-2009, 06:08 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,240,670.20488 seconds with 17 queries