Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 03-22-2004, 09:14 PM   #1 (permalink)
Crazy
 
Location: Long Island, NY
[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.
__________________
"Can't help it if I space in a daze, my eyes tune out the other way... I may switch off and go in a daydream... in this head my thoughts are deep, Sometimes I can't even speak, would someone be and not pretend, I'm off again in my world"
daydream is offline  
Old 03-22-2004, 11:25 PM   #2 (permalink)
Follower of Ner'Zhul
 
RelaX's Avatar
 
Location: Netherlands
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?
__________________
The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in; we're computer professionals. We cause accidents.
- Nathaniel Borenstein
RelaX is offline  
Old 03-23-2004, 07:45 AM   #3 (permalink)
"Officer, I was in fear for my life"
 
hrdwareguy's Avatar
 
Location: Oklahoma City
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.
__________________
Gun Control is hitting what you aim at

Aim for the TFP, Donate Today
hrdwareguy is offline  
Old 03-23-2004, 01:20 PM   #4 (permalink)
Insane
 
Location: Belgium
hmm common, that's easy

int result = 1;
for (int i=n; i>n; i--)
result *=i;
__________________
Let's GO
Cuball is offline  
Old 03-23-2004, 02:31 PM   #5 (permalink)
Crazy
 
Location: Long Island, NY
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
__________________
"Can't help it if I space in a daze, my eyes tune out the other way... I may switch off and go in a daydream... in this head my thoughts are deep, Sometimes I can't even speak, would someone be and not pretend, I'm off again in my world"

Last edited by daydream; 03-23-2004 at 03:43 PM..
daydream is offline  
Old 03-23-2004, 04:25 PM   #6 (permalink)
Crazy
 
Location: Long Island, NY
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?
__________________
"Can't help it if I space in a daze, my eyes tune out the other way... I may switch off and go in a daydream... in this head my thoughts are deep, Sometimes I can't even speak, would someone be and not pretend, I'm off again in my world"
daydream is offline  
Old 03-23-2004, 04:40 PM   #7 (permalink)
Crazy
 
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.
__________________
"Even if you prove me wrong, I'm not going to believe you." - A. McGill
firebirdta is offline  
Old 03-24-2004, 11:00 AM   #8 (permalink)
Free Mars!
 
feelgood's Avatar
 
Location: I dunno, there's white people around me saying "eh" all the time
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:
Originally posted by daydream
int n;
i=n;
int total = 1;

for (i>n;i>0;i--)
total = total*i;
You need to assign the i value. When creating the for loop statement, it works as this way
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:
Originally posted by daydream
package proj3laa;
import javax.swing.*;
import java.text.*;
public class Factorial
{
public static void main(String[] args)
{
int num; Not needed
int factorial; Not needed
factorial=1; Not needed
num=1; Not needed
String s1;
s1=JOptionPane.showInputDialog("Input a number");
num=Integer.parseInt(s1);

while (num>=0 && num!=-999) You need to take a look at other forms of loop statements and see which are the best
{//start while
factorial= num*(num-1);
System.out.print(factorial);
num--;
}//end while
}
}
__________________
Looking out the window, that's an act of war. Staring at my shoes, that's an act of war. Committing an act of war? Oh you better believe that's an act of war
feelgood is offline  
Old 03-29-2004, 02:17 PM   #9 (permalink)
dnd
Psycho
 
Location: London...no longer a student
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.
__________________
"Never underestimate a dumb question"-- Brandon Boyd
dnd is offline  
Old 03-29-2004, 02:22 PM   #10 (permalink)
dnd
Psycho
 
Location: London...no longer a student
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
__________________
"Never underestimate a dumb question"-- Brandon Boyd
dnd is offline  
Old 04-02-2004, 12:03 PM   #11 (permalink)
Crazy
 
Location: Long Island, NY
thanks a lot for your help everyone! I finally figured out my program!

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
}
}
__________________
"Can't help it if I space in a daze, my eyes tune out the other way... I may switch off and go in a daydream... in this head my thoughts are deep, Sometimes I can't even speak, would someone be and not pretend, I'm off again in my world"
daydream is offline  
 

Tags
java, program, w or small


Posting Rules
You may not post new threads
You may not 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 On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 12:10 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360