Finding the factors of a number

Hi, I'm trying to write a program that can find out what the factors are for a certain number. We have to use the for loop and user defined functions are also part of what we are learning, but in this case I can't figure out how to define mt variables. The code I have written is not complete, it's just what I think is right. Any and all help is appreciated thanks!

/*In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,
6! = 6 x 5 x 4 x 3 x 2 x 1 = 720
The value of 0! is 1, according to the convention for an empty product.
Write a function to find the factorial of a number using the for loop.
Your function should take an integer n as input and returns n! as the result. Test your function by calling it in your main program.*/
#include <iostream>
#include <cmath>

using namespace std;
int factorial(int n)
int main()
{
int n;
cout << "Enter a whole number to find out numbers it can factor into:";
cin >>
factorial(n);
return 0;
}
int factorial(int n)
{
for(n <= )
}
Use [code][/code] tags when posting code (this is the 2nd time of asking).

Start with something simple.
1
2
3
4
5
6
7
int main ( ) {
  int factorial = 1;  // 0!
  for ( i = 1 ; i <= 6 ; i++ ) {
    factorial = factorial * i;
  }
  cout << "6! = " << factorial << endl;
}


Now, you can
- allow the user to choose 'n' rather than hard-coded 6
- make the calculation a function.
just about every second or third person doing their first factorial problem tries to compute a value that is too large to fit in their data type and then thinks it is producing the 'wrong' answer. Keep in mind that even 100! (a relatively small input) is extremely large and won't fit in a standard integer.
100! ... won't fit in a standard integer

That's an understatement! :-)

$ bc<<<'2^32-1'
4294967295
$ bc<<<'n=1;for(i=2;i<=13;++i)n*=i;n'
6227020800
$ bc<<<'2^64-1'
18446744073709551615
$ bc<<<'n=1;for(i=2;i<=21;++i)n*=i;n'
51090942171709440000
$ bc<<<'2^128-1'                          ## gcc __uint128_t 
340282366920938463463374607431768211455
$ bc<<<'n=1;for(i=2;i<=35;++i)n*=i;n'
10333147966386144929666651337523200000000

$ bc<<<'n=1;for(i=2;i<=100;++i)n*=i;n'
93326215443944152681699238856266700490715968264381621468592963895217
59999322991560894146397615651828625369792082722375825118521091686400
0000000000000000000000                    ## That's 9.3e157

It still provides an example of recursion anyway:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

string factorial( int n )
{
   if ( n <= 1 ) return "1";
   else          return to_string( n ) + " x " + factorial( n - 1 );
}

int main()
{
   int n;
   cout << "Enter n ( > 0 ): ";   cin >> n;
   cout << factorial( n ) << '\n';
}


Enter n ( > 0 ): 100
100 x 99 x 98 x 97 x 96 x 95 x 94 x 93 x  .... lots of numbers deleted ... x 7 x 6 x 5 x 4 x 3 x 2 x 1
Topic archived. No new replies allowed.