![]() |
[java] need help w/small program
I need to write a program that allows a user to input a number and then output the factorial. (eg. user inputs 5 and program outputs 5!=5*4*3*2*1=120) then the program has to repeat so the user can continue to enter numbers until the user enters -999. This will stop the program.
Any help would be appreciated... I'm really stuck on this one. I don't really have any experience with java. :confused: |
This sounds like a "do my homework for me please" post...
It isn't very difficult if you imlement the factorial computational recursively, but why would any sane man/woman need a factorial calculator in Java? EDIT: actually... thinking about it... I was given an assignment exactly like this in my first year... or was it second? |
How about you post some of your code and tell us where you are stuck at so we can point you in the right direction.
|
hmm common, that's easy
int result = 1; for (int i=n; i>n; i--) result *=i; |
Ok so here is what I have so far... It's not giving me the correct answer and I can't get it to loop... I also have to try to make it so when a user inputs -999 the program will stop...
package proj3laa; import javax.swing.*; import java.text.*; public class Factorial { public static void main(String[] args) { int num; int factorial; factorial=1; num=1; String s1; s1=JOptionPane.showInputDialog("Input a number"); num=Integer.parseInt(s1); while (num>=0 && num!=-999) {//start while factorial= num*(num-1); System.out.print(factorial); num--; }//end while } } *edit- well I figured out how to allow a user to input a number and then the prog outputs all of the numbers below it. (ex user inputs 5 prog outputs: 543231) But i dont know how to get it to multiply those numbers. or anywhere close to what I need it to do besides that.... int factorial; String s1; factorial=1; s1=JOptionPane.showInputDialog("Input a number"); factorial=Integer.parseInt(s1); while (factorial >0) {//start while System.out.print (factorial); factorial--; }//end while :crazy: |
oh wait... ok so I think that I need to use a for loop for this instead... I tried something like what cubal said...
int n; i=n; int total = 1; for (i>n;i>0;i--) total = total*i; but just writing that isn't working... I'm getting a red line for an error under the n in the for loop. is this right? am i missing something? |
Are you writing this program for a class? If so, you should speak to whoever is teaching it because you are making basic syntax mistakes.
http://www.eimc.brad.ac.uk/java/tuto...ect/2/loop.htm Go to that link and read about looping. Go from there. |
Here's your anwer:
int i; for(i = n; i > n; i--) i*=i; System.out.println("Factorial Result: " + i); Here's the problem with your program: Quote:
For(set variables; set limits of the variable; iteration) for example for(int i = 0; i < 20; i++) { System.out.println(i); } I don't understand why you need to assign i = n, nor do I see any need to have total = 1. You can still use i to get the result. Quote:
|
now i'm sure swing was never a first/second term module. i'm pretty sure this is just a command line program that u want, and in that case get rid of your JOptionPane etc.
|
i think it was something like this, any mistakes sorry, i just tried to knock it up quick for u:
if (n <0) { System.out.println("Error, negative number !"); System.exit(-1); } //check exit condition if (n == 999) { System.out.println("Exiting"); System.exit(1); } // Check for zero if (n == 0) { System.out.println("The factorial of zero is one."); System.exit(-2); } // Compute for ( count = 1; count <= n; count++) { fact*= count; } // print result System.out.println("The factorial of " + n + " is : " + fact + "."); all u need to do is take in the user input from the command line with args[0] and check what the number is |
thanks a lot for your help everyone! I finally figured out my program! :D
package proj3; import javax.swing.*; import java.text.*; public class factorial { public static void main(String[] args) { String s1; int num1,total; num1=0; total=1; s1=JOptionPane.showInputDialog("Enter a number to calculate its factorial.\nEnter -999 to exit the program"); num1=Integer.parseInt(s1); while(num1!=-999) {//start while loop 1 System.out.print(num1 + "!="); while ((num1 > 1) && (num1 != -999)) { //start of while statement 2 System.out.print(num1 + "*"); total = (total * num1); num1--; } //end of while statement 2 System.out.print("1="); System.out.println(total); System.exit(0); }//end while loop 1 } } |
All times are GMT -8. The time now is 03:52 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project