Assignemnt #76 Collatz Sequence
Code
///Name: Prooz Fereydouni
///Period: 7
///Project Name: Collatz Sequence
///File Name: CS.java
///Date: 1/27/2016
import java.util.Scanner;
public class CS
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int n, count=0;
System.out.print("Starting number: ");
n = keyboard.nextInt();
while(n != 1)
{
count= count + 1;
if((n/2) * 2 == n)
{
n=n/2;
System.out.print(n + " ");
}
else
{
n= 3*n +1;
System.out.print(n + " ");
}
}
System.out.println("");
System.out.println("Terminated after " + count + " steps.");
}
}
Picture of the output