Assignemnt #77
Code
/// Name: Connor Siri
/// Period: 7
/// Program Name: Collatz
/// File Name: Collatz.java
/// Date Finished: 11/13/2015
import java.util.Random;
import java.util.Scanner;
public class Collatz
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int x, z;
z = 0;
System.out.print("Starting number: ");
x = keyboard.nextInt();
while (x != 1)
{
if ( x % 2 == 0)
{
x = x / 2;
System.out.print( x + " ");
}
else if (x % 2 == 1)
{
x = 3*x+1;
System.out.print(x+ " ");
}
z++;
}
System.out.println("");
System.out.println("Finished after " + z + " steps");
}
}
Picture of the output