MrDoyle

Do-While) Collatz

Dec 9th, 2020
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         System.out.print("Starting number: ");
  7.         Scanner keyboard = new Scanner(System.in);
  8.         int input = keyboard.nextInt();
  9.  
  10.         int steps = 1;
  11.  
  12.         if (input <10){
  13.             System.out.print(input +"   \t");
  14.         }else if(input <100){
  15.             System.out.print(input +"  \t");
  16.         }else if (input <1000){
  17.             System.out.print(input +" \t");
  18.         } else {
  19.             System.out.print(input + "\t");
  20.         }
  21.  
  22.         do {
  23.             if (input % 2 == 0) {
  24.                 input = input / 2;
  25.                 if (input <10){
  26.                     System.out.print(input +"   \t");
  27.                 }else if(input <100){
  28.                     System.out.print(input +"  \t");
  29.                 }else if (input <1000){
  30.                     System.out.print(input +" \t");
  31.                 } else {
  32.                     System.out.print(input + "\t");
  33.                 }
  34.             } else {
  35.                 input = ((3 * input) + 1);
  36.                 if (input <10){
  37.                     System.out.print(input +"   \t");
  38.                 }else if(input <100){
  39.                     System.out.print(input +"  \t");
  40.                 }else if (input <1000){
  41.                     System.out.print(input +" \t");
  42.                 } else {
  43.                     System.out.print(input + "\t");
  44.                 }
  45.             }
  46.             steps ++;
  47.             if (steps % 10 == 0){
  48.                 System.out.println();
  49.             }
  50.         } while (input != 1);
  51.     }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment