Assignment #76
Code
///jack newsom
///period 5
///program Collatz
///filename Collatz.java
///date completed 10/14/15
import java.util.Scanner;
public class Collatz
{
public static void main(String[] args)
{
Scanner bot = new Scanner(System.in);
System.out.println("Starting number: ");
int user = bot.nextInt();
int steps = 0;
int counter = 0;
int ans = 0;
while ( user != 1 )
{
if ( counter == 0 )
System.out.print( user );
if ( user % 2 == 0 )
{
user = user / 2;
steps++;
counter++;
}
else
{
user = user * 3 + 1;
steps++;
counter++;
}
if ( counter % 10 != 0 )
{
System.out.print("\t" + user );
}
else
{
System.out.println( user );
}
}
System.out.println();
System.out.println("Terminated after " + steps + " steps");
}
}
Picture of the output