Advertisement
pranto_95

LOOP until 1

Aug 7th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. public static void evenoradd(int num) {
  2.  
  3.         int even, odd;
  4.         boolean even1 = false, odd1 = false;
  5.         int result = 0 ;
  6.  
  7.         if (num % 2 == 0) {
  8.             even1 = true;
  9.             result = num / 2;
  10.         } else {
  11.             odd1 = true;
  12.             result = (num * 3) + 1;
  13.         }
  14.  
  15.         for (int i = 0; result != 1; i++) {
  16.  
  17.             if (even1) {
  18.                
  19.                 if (result%2==0) {
  20.                     result=result/2;
  21.                     even1 = true;
  22.                     odd1 = false;
  23.                    
  24.                 } else {
  25.                     result = (result * 3) + 1;
  26.                     even1 = false;
  27.                     odd1 = true;
  28.                 }
  29.                 System.out.println(result);
  30.                
  31.             } else {
  32.                
  33.                 if (result % 2 == 0) {
  34.                     result=result/2;
  35.                     even1 = true;
  36.                     odd1 = false;
  37.                 } else {
  38.                     result = (result * 3) + 1;
  39.                     even1 = false;
  40.                     odd1 = true;
  41.                 }
  42.                 System.out.println(result);
  43.             }
  44.  
  45.         }
  46.  
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement