I'm a rookie at C++ and trying to learn on my own with a lot of tutorials. My question is, what is the best method of caculating and displaying the values of 1! to 10! using nested "for" statements? Thanks for any advice in advance.

Printable View

- 03-15-2005s_ny33factorials??
I'm a rookie at C++ and trying to learn on my own with a lot of tutorials. My question is, what is the best method of caculating and displaying the values of 1! to 10! using nested "for" statements? Thanks for any advice in advance.

- 03-15-2005prog-bman
You don't really need nested for statments you only need one loop think about it.

a = 1;

for i = 0; i < 10; i++

a = a * i

print a

[edit] thanks hunter ya jerk :) [/edit] - 03-15-2005Hunter2
>>for i =

**10**; i < 10; i++

;) - 03-15-2005KrakCode:
`/************************************`

Calculates factorial of a number (n!)

************************************/

int fact(int n){

int result=1;

//Special cases: n==0 and n<0

if(n==0){return 1;} //0! = 1

if(n<0) {return 0;} //Negative factorial is undefined

//If the number is positive:

while(n>0){

result *=n;

n--;

}

return result;

}

- 03-15-2005IfYouSaySo
Not entirely relevent, but this was always my favorite:

Code:

# include <iostream>

using namespace std;

template <int x>

class factorial {

public:

enum { VALUE = x * factorial<x-1>::VALUE };

};

template <>

class factorial<0> {

public:

enum { VALUE = 1 };

};

int main()

{

cout << "Factorial is " << factorial<10>::VALUE << endl;

return 0;

}

- 03-16-20057stud
You can do it with a recursive function as well, although personally I can't write a recursive function to save my life. There is a tutorial on this site covering recursive functions, but I would never have figured out the output of this without running it:

Code:`void printnum ( int begin )`

{

cout<< begin;

if ( begin < 9 )

printnum ( begin + 1 );

cout<< begin;

}

- 03-16-2005treenef
Hi,

I just thought I'd add a little bit although you probably get the picture now. I have written a complete code which computes n! from n=1 to n=10, like you have mentioned using a 'nested for loop'.

There are easier ways to do it, but I am just doing what you have specified. Underneath is the entire code which can be copied and pasted into your programme straight away.

Code:`/************************************`

Calculates factorial of a number (n!)

from n=1 to n=10

************************************/

#include<iostream.h>

int main()

{

cout<<"*******************************"<<endl;

cout<<"Calculates n! from n=1 to n=10"<<endl;

cout<<"*******************************"<<endl;

cout<<""<<endl;

int accum;

for (int b=1; b<11; b++)

{

accum=1; //initialise accumulate

for (int a=b; a>0; a--)

{

accum=accum*a;

}

cout<<"The solution is: "<<accum<<endl;

}

int stop;

cin>>stop;

}

- 03-16-2005s_ny33
Thanks everybody for responding. To help me better understand, why did treenef initialize the sample code within the inner loop? I notice if I take this out, you get weird negative numbers. The code in question is:

accum=1; //initialise accumulate

Thanks - 03-16-2005Hunter2
Actually, it's the outer loop. The outer loop is for displaying the factorial from 1 to 10, and the inner loop is for calculating the factorial of

**i**. Since the accumulator must be reset for each factorial you are calculating, it makes sense to reset it each time at the beginning of the outer loop. - 03-17-2005treenef
Yes hunter is correct.

I should have initialised accum before the nested for statements.

And I should have reset accum at the end of the outer loop. However, I had written the code in a rush and since it worked fine I didn't notice that the logic was incorrect. Here is the ammended code.

Oh and just a helpful hint S_ny 33 -

it's always useful to convert code into flow charts to help you visualise what is going on, especially when it comes to nested statements.

Code:`/************************************`

Calculates factorial of a number (n!)

from n=1 to n=10

************************************/

#include<iostream.h>

int main()

{

cout<<"*******************************"<<endl;

cout<<"Calculates n! from n=1 to n=10"<<endl;

cout<<"*******************************"<<endl;

cout<<""<<endl;

int accum;

accum=1; //initialise accumulate

for (int b=1; b<11; b++)

{

for (int a=b; a>0; a--)

{

accum=accum*a;

}

cout<<"The solution is: "<<accum<<endl;

accum=1;

}

int stop;

cin>>stop;

}

- 03-17-2005s_ny33
Treenef -- your code is fine and it was very helpful...thanks. The flowchart is a good idea because I think that is my problem when determining the proper outlay of the code. Thanks.

- 03-17-2005Hunter2
>>I should have initialised accum before the nested for statements.

No, it's fine as it is - accum will always be initialized to 1 before being used, and it will be reset when the outer loop wraps around to the beginning again. Either way will work fine though. - 03-17-2005Krak
Okay boys and girls, let's learn something really quick:

You do not need 2 for loops to do something simple like factorials. Usually, 2 for loops would be used to anything involving a 2D array or something of that nature. Also for creating a map or tiles or representing anything in 2 dimensions:

Code:`for(int i=0; i<height; i++){`

cout << endl;

for(int j=0; j<width; j++){

cout << "*";

}

}

......There's your daily dose of common sense. :cool: - 03-17-2005Hunter2Quote:

Originally Posted by**Krak**

- 03-18-2005VirtualAceQuote:

Also for creating a map or tiles or representing anything in 2 dimensions:

2 variables for x and y, one for offset. 1 loop and faster than a 2D array.

I should suspect the recursive version would be this:

Code:`unsigned long Factorial(int num)`

{

static unsigned long totalvalue=num;

if (num<2) return totalvalue;

totalvalue*=(num-1);

Factorial(num-1);

}