I am trying to write a program which can calculate factorial from 1-9 by iteration, but I encounter some problems while I am trying to. Please help me figure out my problems in my program, I am just learning programming.
The following is my program, please tell me what’s wrong with it:
public class iterative {
static int ans=1;
public static void iteration() {
System.out.println("n n!");
for (int n=1; n<10; n++) {
while ((n-1)>0)
ans=n*(n-1);
System.out.println(n + " " + ans);
}
}
public static void main(String[] args) {
iteration();
}
}
The answers above is close perfect,you also get it with the recursion:
here is code: