Assignment #76 Collatz Sequence
Code
///Name: Ian Grant
///Period:5
///Project Name: Collatz Sequence
///File Name:Coolatz.java
///12/08/15
import java.util.Scanner;
public class Coolatz
{
public static void main(String[] args)
{
Scanner stuff=new Scanner(System.in);
int n=0;
int s=0;
System.out.print("Starting number: ");
n=stuff.nextInt();
while (n!=1)
{
if (n%2==0)
{
n=n/2;
s++;
System.out.print(n+"\t");
}
else if (n%2!=0)
{
n=3*n+1;
s++;
System.out.print(n+"\t");
}
}
System.out.println("");
System.out.println("Terminated after "+s+" steps.");
}
}
Output