Do-While Loops part 2 #76 (Ze Module)
Code
/// Name: Amir Salehi
/// Period: 7
/// Program Name:Collatz Sequence
/// File Name:CollatzSequence.java
/// Date Finished: 1/14/2016
import java.util.Scanner;
public class CollatzSequence
{
public static void main( String[] args )
{
Scanner input = new Scanner(System.in);
int x=0;
System.out.print("Starting number: ");
int n = input.nextInt();
while (n != 1) {
if ((n%2) ==0){
n = n/2;
}
else {
n= 3*n+1;
}
System.out.print( + n + " ");
x++;
}
System.out.println("");
System.out.println("");
System.out.println("Terminated after "+ x + " steps.");
}
}
Picture(s) of the output