Overclock.net banner
1 - 3 of 3 Posts

·
Premium Member
Joined
·
1,563 Posts
Quote:
Originally Posted by Jov4nTh3Yu9o View Post

I can't seem to get my factorial program working properly. For any input value I get 1!=1 back out. Can anyone help me out?

I don't even understand what you're trying to accomplish with that code but your for loop initialises the iterating variable i = 1 and then ends while i <= 1, therefore, only undergoing one iteration of the code in the loop. So, an output of "1 != 1" seems about right.

What are you trying to do?

Also, it would be better if you posted your code in code tags, instead of posting a screenshot.
 

·
Registered
Joined
·
1,231 Posts
i put this together real quick in c++, seems to work fine. It was your for loop, just use a while loop...much easier. This can calculate the result up to 65 factorial.

Code:

Code:
#include <iostream>

using namespace std;

int main(){

        int num;

        cin >> num;

        unsigned long long int factorial = 1;

        while(num > 1){

             factorial *= num--;

    }
        cout << factorial;

        return 0;
}
 
1 - 3 of 3 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top