Java Factorial Program? - Overclock.net - An Overclocking Community

Forum Jump: 

Java Factorial Program?

 
Thread Tools
post #1 of 3 (permalink) Old 02-03-2013, 02:19 PM - Thread Starter
New to Overclock.net
 
Jov4nTh3Yu9o's Avatar
 
Join Date: Jan 2011
Location: Champaign IL
Posts: 1,270
Rep: 20 (Unique: 18)
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?


Jov4nTh3Yu9o is offline  
Sponsored Links
Advertisement
 
post #2 of 3 (permalink) Old 02-03-2013, 03:04 PM
Retired Staff
 
{Unregistered}'s Avatar
 
Join Date: Jun 2010
Posts: 1,563
Rep: 17 (Unique: 15)
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.


Quote:
Originally Posted by andrews2547 go_quote.gif

Women suck at almost everything. The only time they were good at anything was when Cliopatra was in charge and they lived in triangles. Triangles aren't manly.


{Unregistered} is offline  
post #3 of 3 (permalink) Old 02-03-2013, 08:07 PM
New to Overclock.net
 
surfbumb's Avatar
 
Join Date: Jul 2007
Location: San Diego
Posts: 1,231
Rep: 97 (Unique: 79)
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:
#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;
}



surfbumb is offline  
Reply

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the Overclock.net - An Overclocking Community forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
If you do not want to register, fill this field only and the name will be used as user name for your post.
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off