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
}
}
|