Assignment #76 and CollatzSequence
Code
/// Name: Jason Kim
/// Period: 7
/// Program Name: CollatzSequence
/// File Name: CollatzSequence.java
/// Date Finished:11/17/15
import java.util.Scanner;
public class CollatzSequence
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int number, counter = 0;
System.out.print("Starting number: ");
number = keyboard.nextInt();
System.out.print(number + "\t");
while (number != 1)
{
counter = counter + 1;
if(number%2 == 0)
{
number = number/2;
}
else
{
number = 3*number + 1;
}
System.out.print(number + "\t");
}
System.out.println("\n\nTerminated after " + counter + "steps.");
}
}
Picture of the output