Thread: Program to calculate factorials

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    2

    Question Program to calculate factorials

    OK, I know that this will seem a pitifully stupid question to anyone with any programming knowledge, but here goes anyway.

    I'm new to C++ and I need to write a program to calculate factorials from an integer input. It's not so much a language difficulty with C++ as a logical problem.

    Beginning with a factor of 1, I need a loop which multiplies the user input number by the factor, adds 1 to the factor, then multiplies the previous result by the new factor, continuing until the factor=(starting number - 1). Can anyone help?

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    is this what u mean:

    int factorial (int number)
    {
    if(number > 1) return (number * factorial(number - 1));
    else return 1;
    }

    That should work.
    My Website

    "Circular logic is good because it is."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program to calculate change return.
    By OrAnGeWorX in forum C Programming
    Replies: 15
    Last Post: 11-17-2008, 09:49 AM
  2. C Program to calculate the FILE Size Sytem in Unix
    By anwar_pat in forum C Programming
    Replies: 6
    Last Post: 02-23-2006, 10:17 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM