Instructions: Complete these exercises on paper first. Then run the code to check your answers. Bring questions about these problems to your next class.
while (n <= 100) {
sum += n*n;
}
while loop to compute factorials.
import javax.swing.*;
public class Factorial {
public static void main(String args[])
{
int n;
int factorial = 1;
String input;
String output;
input = JOptionPane.showInputDialog ("Enter a positive integer: ");
n = Integer.parseInt(input);
output = "" + n;
do {
factorial *= n;
n--;
} while (n > 1);
output += "! = " + factorial;
JOptionPane.showMessageDialog(null, output);
System.exit(0);
} // main
} // Factorial
for loop to compute factorial.for loop into a while loop:
for (int i = 1; i <= n; i++) {
System.out.println(i*i);
}
for (int i = 0; i < 8; i++) {
if (i%2 == 0) System.out.println(i + 1);
else if (i%3 == 0) System.out.println(i * i);
else if (i%5 == 0) System.out.println(2*i - 1);
else System.out.println(i);
} // for
for (int i = 0; i < 8; i++) {
if (i%2 == 0) System.out.println(i + 1);
else if (i%3 == 0) continue;
else if (i%5 == 0) break;
System.out.println("End of Loop");
} // for
System.out.println("End of Program");