RobertQuitt

FunLoops

Oct 7th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1. public class FunLoops {
  2.     private static void magicSquare ( int count) {
  3.         long sum = 1;
  4.         long square = 1;
  5.         int squareCount = 1;
  6.         int sumCount = 1;
  7.         while (count>0) {
  8.             while (sum != square) {
  9.                 sumCount++;
  10.                 sum += sumCount;
  11.                 while (sum>square) {
  12.                     squareCount++;
  13.                     square = (long) Math.pow(squareCount,2);
  14.                 }
  15.             }
  16.             System.out.println(sum);
  17.             sumCount++;
  18.             sum += sumCount;
  19.             count--;
  20.         }
  21.     }
  22.     private static int lcm (int input1, int input2) {
  23.         double multiple = input1;
  24.         while (multiple % input2 !=0) {
  25.             multiple += input1;
  26.         }
  27.         return (int)multiple;
  28.     }
  29.     private static int reverse (int input) {
  30.         int digit;
  31.         int output = 0;
  32.         while (input != 0) {
  33.             digit = input%10;
  34.             output*=10;
  35.             output+=digit;
  36.             input/=10;
  37.         }
  38.         return output;
  39.     }
  40.     public static void main(String[] args) {
  41.           magicSquare(4);
  42.           System.out.println("12345 reversed ---> " + reverse (12345));
  43.           System.out.println("10001 reversed ---> " + reverse (10001));
  44.           System.out.println("1200 reversed ---> " + reverse (1200));
  45.           System.out.println("5 reversed ---> " + reverse (5));
  46.           System.out.println();
  47.           System.out.println("LCM (15,18) = " + lcm (15,18));
  48.           System.out.println("LCM (40,12) = " + lcm (40,12));
  49.           System.out.println("LCM (2,7) = " + lcm (2,7));
  50.           System.out.println("LCM (100,5) = " + lcm (100,5));
  51.           System.out.println();
  52.           }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment