View Single Post
Old 03-24-2004, 11:00 AM   #8 (permalink)
feelgood
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  
 

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