Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Ulam {
- public static void main(String[] args) {
- int num, print; // declare variable to store results
- for (num = 1; num <= 25; num++) { // initialize for loop
- print = num; // initialize results to 1, which is the value of "i"
- System.out.printf("\n%,d: ", print);
- do {
- if (print % 2 == 0) { // check if number is even
- print /= 2;
- } else { // if odd
- print = (print * 3) + 1;
- }
- System.out.printf("%,d ", print); // append number on the same line
- } while (print != 1);
- System.out.println(); // PRINTS AN EMPTY SPACE
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement