Assignment #76 and Collatz Sequence
Code
/// Name: Graham Pollock
/// Period: 5
/// Program Name: Collatz Sequence
/// File Name: CSequence.java
/// Date Finished: 01/28/2016/
import java.util.Scanner;
public class CSequence
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int n, tries;
tries = 0;
System.out.println("COLLATZ SEQUENCE!");
System.out.print("Starting number: ");
n = keyboard.nextInt();
while (n != 1)
{
if (n % 2 == 0)
{
n = n / 2;
System.out.print(n + " ");
}
else if (n % 2 == 1)
{
n = 3 * n + 1;
System.out.print(n + " ");
}
tries++;
}
System.out.println("");
System.out.println("Terminated after " + tries + " steps.");
}
}
Picture of the output