Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

collatz conjecture

import java.util.Scanner;

public class CollatzConjecture {
    public static void main(String[] args){
        System.out.println("Enter a positive number");
        Scanner userInput = new Scanner(System.in);
        int n = userInput.nextInt();
           while (n!=1) {
               if (n % 2 == 0) {
                  n = n / 2;
               } else if (n % 2 == 1) {
                  n =3 * n + 1;
               }
           }
        System.out.println(n);
    }
}
 
PREVIOUS NEXT
Tagged: #collatz #conjecture
ADD COMMENT
Topic
Name
8+1 =