Advertisement
wabbitgurl

LoopyCollatz

Mar 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. package com.company;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6. private static Scanner input = new Scanner(System.in);
  7.  
  8. public static void main(String[] args) {
  9.  
  10. int number;
  11. int count = 0;
  12.  
  13. System.out.println("Please enter a whole number: ");
  14. number = input.nextInt();
  15.  
  16. while (number < 1) {
  17.  
  18. System.out.println(number + " is less than 1. Please re-enter a new number:");
  19. number = input.nextInt();
  20. }
  21.  
  22. while (number > 1) {
  23. if (number % 2 == 0)
  24. number = number / 2;
  25. else number = (3 * number) + 1;
  26.  
  27. System.out.println(number);
  28. System.out.println();
  29. count++;
  30.  
  31. }
  32. System.out.println("It takes " + count + " steps to reach 1");
  33.  
  34. //System.out.println("Would you like to continue? (y or n)");
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement