Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java Factorial Program?
New Posts  All Forums:Forum Nav:

Java Factorial Program?

post #1 of 3
Thread Starter 
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?

El Computron
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 3570k Asus P8Z77V-LK 980 ti SLI Gskill Ripjaws 16gb  
Hard DriveHard DriveOptical DriveCooling
1 TB (2X500 RAID 0) Seagate Barracuda OCZ Vector 150 120 gb 2 LG DVD drives XSPC rasa 240 
OSMonitorKeyboardPower
Windows 7 Ultimate 64 Asus VG278 144hz Razer BlackWidow Cooler Master Silent Pro M 1000w 
CaseMouseMouse PadAudio
HAF X Razer Gaming Mouse Raytheon Pad Asus Xonar DX 7.1 
  hide details  
Reply
El Computron
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 3570k Asus P8Z77V-LK 980 ti SLI Gskill Ripjaws 16gb  
Hard DriveHard DriveOptical DriveCooling
1 TB (2X500 RAID 0) Seagate Barracuda OCZ Vector 150 120 gb 2 LG DVD drives XSPC rasa 240 
OSMonitorKeyboardPower
Windows 7 Ultimate 64 Asus VG278 144hz Razer BlackWidow Cooler Master Silent Pro M 1000w 
CaseMouseMouse PadAudio
HAF X Razer Gaming Mouse Raytheon Pad Asus Xonar DX 7.1 
  hide details  
Reply
post #2 of 3
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.
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #3 of 3
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;
}



Edited by surfbumb - 2/3/13 at 8:29pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java Factorial Program?