Results 1 to 3 of 3

Thread: How to calculate factorial of a number in C++

  1. #1
    Join Date
    Jun 2009
    Posts
    3,859

    How to calculate factorial of a number in C++

    I am having problem coding in C++. I want to calculate the factorial of a number that the user inputs but the problem is that whatever logic I try to do, it fails. So I am here to know what are the possibilities and how do you calculate factorial of a user specified number in C++?

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

    Re: How to calculate factorial of a number in C++

    You can calculate the factorial of a number in C++ using an external function as below and calling that function in your main code:

    Code:
    int calfactorial (int x)
    {
     int finalx = 1;
     if (x==1) return 1;
     for (int i = 1; i <= x; ++i) finalx = finalx * i;
     return finalx;
    }

  3. #3
    Join Date
    May 2008
    Posts
    685

    Re: How to calculate factorial of a number in C++

    Here is how factorial is calculated in C++:

    Code:
    # include <iostream.h>
    int factorial (int);
    int main()
    {
        int result; 
        cout << "Enter your number: ";
        cin >> result;
        cout << "Factorial of " << result << "is "<< factorial (result) << endl;
    }
    
    int factorial (int n)
    {
       int fact = 1;
       if  (n <= 1)
    	 return 1;
       else
    	 fact  = n * factorial (n - 1);
       return fact;
    }

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. c# program for factorial
    By roodiii in forum Software Development
    Replies: 5
    Last Post: 19-12-2009, 02:42 PM
  4. 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
  5. 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

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,040,682.38107 seconds with 16 queries