Results 1 to 6 of 6

Thread: What is the Program to find-out factorial of given number?

  1. #1
    Join Date
    Nov 2009
    Posts
    877

    What is the Program to find-out factorial of given number?

    Hi Guys,

    Is Anyone know the how to write the program to find-out factorial of given number?

    When I run my program code for factorial, either the output prints wrong results or it goes into the indefinite looping. I referred different books but it doesn't works for me. I don't know how to proceed.

    Please help me to find out the solution over this.

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

    Re: What is the Program to find-out factorial of given number?

    Hi all,

    I have the program code of the to find out the factorial of the given number. Below program is in the C++ language. I have used simple For loop to find out the factorial:

    #include<conio.h>
    #include<iostream.h>

    void main()
    {

    int x=0;
    int Fact=1;

    cout<<"Enter number to calculate Factorial:";
    cin>>x;

    for(int c=1;c<=x;c++)
    {

    Fact=Fact * c;

    }

    cout<<"Factorial of number <<x << is: "<<Fact;

    getch();

    }

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

    Re: What is the Program to find-out factorial of given number?

    Hi,

    See something below can help you to write program to find-out factorial:

    Code:
    Class Fact_demo{

    public static void main(String[] args)
    {
    try
    {

    BufferedReader d = new BufferedReader(
    new InputStreamReader(System.in));
    System.out.println("enter the number");
    int j= Integer.parseInt(d.readLine());
    double fact1= 1;
    System.out.println("Factorial of " +j+ ":");

    for (int k= 1; k<=a; k++)
    {
    fact1=fact1*k;
    }

    System.out.println(fact1);

    }

    catch (Exception e1)
    {
    System.out.println("Array is out of bounds");

    }
    }
    }

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

    Re: What is the Program to find-out factorial of given number?

    If your looping concept is clear then it won't be difficult for you to write the factorial program. I suggest you to use the "While " loop condition for this. Refer below code:


    while(z<=number)
    {
    factorial = factorial *z;
    z++;
    }

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

    Re: What is the Program to find-out factorial of given number?

    Have you tried below following programming logic to find-out factorial?
    #include<conio.h>
    #include<stdio.h>

    void main()
    {
    int facto(int);
    int num;
    clrscr();
    printf("Enter the number: ");
    scanf("%d",&num);
    printf("Factorial %d is: %d",num,facto(n));
    getch();
    }
    int facto(int y)
    {
    int fact4;
    if(y==1)
    return(1);
    else
    fact4=y*facto(y-1);
    return(fact4);
    }

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    Re: What is the Program to find-out factorial of given number?

    Hi friend,

    I had solved the similar problem using different loop of the C. Please try to use nested loop to find out the factorial. I am telling you because nested loop is useful to test the very difficult conditions. Functions with return type is also useful to find out the factorial of the given number. Use "fact(int number)" function in your program.

Similar Threads

  1. n factorial program in C
    By MaryJ in forum Software Development
    Replies: 5
    Last Post: 24-09-2010, 09:59 PM
  2. c# program for factorial
    By roodiii in forum Software Development
    Replies: 5
    Last Post: 19-12-2009, 02:42 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. 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
  5. Program to calulate the square of number
    By windows_user in forum Software Development
    Replies: 3
    Last Post: 10-12-2008, 04:41 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,751,751,918.57460 seconds with 16 queries