Assignemnt #76 and Collatz Sequence
Code
///name: Ben Borglin
///period: 6
///Program name: Collatz Sequence
///file name: CSeq.java
///Date finished: 10/27/15
import java.util.Scanner;
public class CSeq
{
public static void main( String[] args)
{
Scanner keyboard = new Scanner(System.in);
int n, tries;
tries = 0;
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