Home CPSC 330

Java Exercise 1

Objective

To become more familiar programming in Java.

Task

For this lab, you will write a simple program to test the collatz conjecture. The conjecture states that if you take any natural number and, it's even, cut it in half, and if it's odd multiply it by 3 and add one, repeatedly, you'll eventually reach 1.


Details

  1. Prompt the user to enter a number.
  2. Use the Scanner class to get an integer as input from the user. Test that it is positive, if not get input again.
  3. Test if the number is even, if so divide it by 2.
  4. Otherwise, multiply by 3 and add 1.
  5. Do this until the number equals 1.
  6. Print out the number at each step.

Submitting

When your program works, email the code to ifinlay@umw.edu.

Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.