Reading the assignment you provided it seems all it asks is to change the program in such a way so it'll output the same result with the given parameters n, p and i.
It's tricky with a do-while loop. Since it always performs the given code once and then later checks the condition. The for and while loop always check first and then run the code.
To get the same functionality, but using a while or for loop, I let the while and for loop run forever but with a condition in the loop to simulate the do-while behavior.
Your code might look like this:
public class MyClass
{
public static void main(String args[])
{
int n = 6;
int p = 1;
int i = 1;
do
{
p *= i;
i++;
}
while (i <= n);
System.out.printf("result = %d%n", p);
n = 6;
p = 1;
i = 1;
while(true)
{
p *= i;
i++;
if (i>n) break;
}
System.out.printf("result = %d%n", p);
n = 6;
p = 1;
i = 1;
for (;;)
{
p *= i;
i++;
if (i>n) break;
}
System.out.printf("result = %d%n", p);
}
}
Which outputs this:
result = 720
result = 720
result = 720
I hope this helps you out.
~Catey