hello everoyne,

I am nu 2 this so pleaz b kind. I need 2 make a program which does the factorial. But i not no how to do this. It is urgent so if you could help me i would be greatfull. God bless.

i have this so far.

#include<iostream.h>

int main()
{
  please enter your number to be made factorial.
  enter number>>number;
if(number=1)
{
  cout<<1;
}
if(number=2)
{
 cout<<1*2;
}
if(number=3)
{
 cout<<1*2*3;
}
if(number=4)
{
cout<<1*2*3*4;
}

}

<< moderator edit: added [code][/code] tags >>

I am soo confused pleaz help me. :cry:

Comments
:icon_cheesygrin:

The simplest way is to use a loop. A for(unsigned long i=user_input; i > 1; i--); could be one solution.

Look for a pattern in your code to figure out how to do this in the generic sense. For examply you start with 1. Then you multiply 1 by 2. Then you multiply that value times three. Then you multiply that value times 4, etc. The last value you multiply by is the number input by the user.

Next remember that in C/C++ you can reuse the same variable in a given statement, under the right circumstances. So:

the new value of x is the old value of x times some other value y

is a valid statement in pseudocode.

Finally, if you are doing the same thing over and over again, think loops. That should get you close to a solution.

It turns out you can "simplify" the new value is the old value times some other value statement in that C/C++ also defines a *= operator such that:

if x = 3;
and you do this to x-----x *= 2;
then x now equals 6.

you can do somthing like this:

#include <iostream>

using namespace std;

int main()
{
    int num = 50; // test num
    bool done = false; // for tetsing if done
    while (!done) 
    {
        done = true; 
        if(num % 2 == 0) // testing if 2 is a factor of it, 
        {
            num /= 2; // if, devide it with 2.
            cout << "2"; 
            done = false; // set done to false, and run it thru one more time.
        }else // if not a factor of 2
        {
            for (int i = 3; i <= num && done; i += 2) // start on 3 , and go +2  until i is larger than num.
            {
                if (num % i == 0) // if i is a factor of num
                {
                    num /= i; // devide on i
                    cout << "*" << i; // print out i
                    done = false; // and set done to false
                }
            }
        }
    }
}

it does not print out 1* becuse 1 is not a prime number ;)

He said "factorial," zyruz.

Iamthwee, you know what a for loop is, right? Start 'answer' at 1 and multiply answer by all the numbers from 1 to n.

>He said "factorial," zyruz.
My bad :)
you can also use a while loop:

#include <iostream>

using namespace std;

int main ()
{
    int num = 5;
    int temp = 1;
    
    while (num > 1)
    {
        temp *= num--; 
    }
    cout << temp;
}

tank you guys!

But i am confused with what is going on here

temp *= num--;

my teach said the '*' is sometimes used as a pointer, but i can't use pointers yet. I have not done them. thank you. :o

tank you guys!
temp *= num--;

temp *= num--; is equivallent to: temp = temp*(num-1);
temp += 5; is equivallent to: temp = temp + 5;

The shorthand operators are basic and useful, due to their simplicity and length.

A factorial is n * (n-1) * (n-2) * .... so all you need is a simple loop to calculate the factorial

long n = 5;
long total = 1;
while(n > 1)
{
  total = total * n;
  --n;
}
// output result
cout << total << endl;

[edit]Sorry about duplicating a previous answer. I didn't see them when I posted this. :( [/edit]

Comments
your very first post :D

hello there, i have wrote this simple factorial program for you, i would suggest that you run it aswell as actually follow the code in your head with smaller numbers so you can get the hang of it how it is working

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

int number;
int answer;

int main ()
{
 cout<<"Please enter number: ";
 cin>>number;
 
 cout<<number<<"! = ";
 answer = number;
  while (number>1)
  {
   answer=answer*(number-1);
   number--;      
  } 
  cout<<answer; 
  getch(); 
}

good luck

thank u everyone.

I think i no what is happening here.
temp = temp*(num-1); makes more sense 2 me now! Great now sum1 said i can put it in a funcrtion. Can u tell me how a function works and wat it is. God bless.

:cool:

a function is like a seperate piece of code that can be called from within the main code, im not very good at explaining it but ill try and give you an example

#include<iostream.h> //needed for output functions
#include<conio.h>     //needed for getch();

addition(number1,number2); //defining the function (prototyping)

int number1;
int number2;

int main ()
{
cout<<"enter number 1: ";
cin>>number1;                   //get number1
cout<<"enter number 2: ";
cin>>number2;                   //get number 2

addition(number1,number2); //call the addition function 
                                      //with these 2 numbers
getch();                           //keep the output on screen

int addition(number1,number2) //actual function
{                                        //outside main code

 cout<<number1<<" + "<<number2<<" = "<<number1 + number2;

}

i hope this helps

This article has been dead for over six months. Start a new discussion instead.