-2

So I am making a factor recurring program for Uni and I haven't used for loops very often,

The idea is that you input a positive number and it does the calculation for you, e.g. if the number 5 was chosen it would do the following:

1*2*3*4*5 = 120, it is working for most of my inputs but it doesn't get it right when I go pass 1

Just wondering if anyone knows what is wrong with my foor loop.

#include <iostream>
using namespace std;

int main() {


    int i, n, factorial = 1;

    cout<<"Enter a positive integer: ";
    cin>>n;
    if (n<=0) {

        cout << "Please enter a non-negative number!!!\n";
    }

    else {

    for (i = 1; i <=n; ++i) {
        factorial *= i;   // factorial = factorial * i;
    }

    cout<< "Factorial of "<<n<<" = "<<factorial;
    return 0;

    }
}

I am also planning so that when the number gets too big it goes into scientific notation, but I need to get this working first.

When I input 9 it does the calculation correctly:

1*2*3*4*5*6*7*8*9 = 362880 

The way I have got it to be printed is:

Enter a non-negative integer:
9! = 362880

When I enter a number greater than 12, e.g. 13 I get

Enter a non-negative integer:
13! = 1932053504

The answer is wrong and also I need it to be:

Enter a non-negative integer:
13! = 6.22702e+09

Cheers.

rge5890
  • 13
  • 1
  • 4
  • 5
    `but it doesn't get it right when I go pass 1` Give some sample inputs, expected and actual outputs. – John3136 Aug 10 '16 at 05:16
  • Your code work fine for Input 1 , factorial comes out to be 1. What output you are looking for. – Ajay Aug 10 '16 at 05:20
  • For this to be a correct factorial calculator, remember that `0! = 1` – naffarn Aug 10 '16 at 05:24
  • I have just found out that i need to use long int for the variable but still stuck on the scientific notation bit. – rge5890 Aug 10 '16 at 05:25
  • It is happening because, for 13! the result value is more than max int can handle. – Ajay Aug 10 '16 at 05:26
  • You still haven't explained what you mean by: `"when I go pass 1"`? – Disillusioned Aug 10 '16 at 05:29
  • 4
    How on earth is this getting upvoted??? The first upvote was even before sample input and output had been added. – Disillusioned Aug 10 '16 at 05:31
  • This question has heaps of duplicates and your problem has nothing to do with the for loop: http://stackoverflow.com/questions/23463794/factorial-overflow Also: http://stackoverflow.com/search?q=%5Bc%2B%2B%5D+factorial – Disillusioned Aug 10 '16 at 05:42
  • Possible duplicate of [How to detect integer overflow in C/C++?](http://stackoverflow.com/questions/199333/how-to-detect-integer-overflow-in-c-c) – Disillusioned Aug 10 '16 at 05:45
  • Possible duplicate of [Factorial in C family languages](http://stackoverflow.com/questions/17136119/factorial-in-c-family-languages) – phuclv Aug 10 '16 at 06:09

4 Answers4

3

Look at this link to see each data type range (Min & Max values for type int): http://www.tutorialspoint.com/cplusplus/cpp_data_types.htm For scientific notation check this: http://www.cplusplus.com/reference/ios/scientific/ Also check this link to understand the concept of overflow: http://www.cplusplus.com/articles/DE18T05o/

Ehsan Shekari
  • 876
  • 2
  • 10
  • 19
1

It is because the output value for 13 exceeds the MAX value for a signed int (4 bytes) and causes integer overflow.

Consider changing int to long long int (8 bytes)

#include <iostream>
using namespace std;

int main() {

    long long int i, n, factorial = 1;

    cout<<"Enter a positive integer: ";
    cin>>n;
    if (n<=0) {
        cout << "Please enter a non-negative number!!!\n";
    }

    else {

    for (i = 1; i <=n; ++i) {
        factorial *= i;   // factorial = factorial * i;
    }

    cout<< "Factorial of "<<n<<" = "<<factorial;
    return 0;

    }
}
Thirupathi Thangavel
  • 2,418
  • 3
  • 29
  • 49
0

You can use long int and you will get correct values up to 20! or so.

Michel
  • 133
  • 1
  • 8
0

Use unsigned long long int.

#include <iostream>
using namespace std;

// Input validation
int getNumber() {
    int n;
    do {
        cout << "Enter a non-negative number!\n";
        cin >> n;
    } while (cin && n < 0);
    return n;
}

int main() {
    int n = getNumber();
    unsigned long long factorial = 1;   // Maximum possible
    for (int i = 1; i <= n; ++i)
        factorial *= i;
    cout<< n << "! = " << factorial;
    return 0;
}

Input

13

Output

13! = 6227020800

See Ideone DEMO.

EDIT: If you want scientific notation, use long double or stream format flags like std::scientific. You can also set precision using cout.precision method.

Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
  • thankyou so much, just one question, do you know how I can make the outuput in scientific notation, just the outpup so not the "13!" just the "6227020800" I wish to express it as "13! = 6.22702e+09" I want this to be only for the values greater or equal to 10, would i use an if statement – rge5890 Aug 10 '16 at 05:49