Skip to content

Instantly share code, notes, and snippets.

@serdarsen
Last active August 17, 2019 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serdarsen/a578ddd48c8d8429af8dfce1829fc2c2 to your computer and use it in GitHub Desktop.
Save serdarsen/a578ddd48c8d8429af8dfce1829fc2c2 to your computer and use it in GitHub Desktop.

Keep it simple

Find second largest number in the array

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] nums = {1, 4, -3, 20, 7, 10};
        int temp;

        for(int i = 0; i < nums.length; i++){
            for (int j = i + 1; j < nums.length; j++){
                temp = nums[i];
                if(nums[i] < nums[j]){
                    temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                }
            }
        }
        System.out.println("Our sorted array: " + Arrays.toString(nums));
        System.out.println("Second largest number in the array: " + nums[1]);
    }
}

Sum of the digits of the given number

public class Main {
    public static void main(String[] args) {
        int num = 125;
        int sum = 0;
        int digit;
        while(num > 10){
            digit = num % 10; // all digits before last
            sum += digit;
            num = num / 10;
        }
        digit = num; // last digit
        sum += digit;
        System.out.println("Sum of the digits of the given number : " + sum);
    }
}

Check if given number is divisible by 3 or 5 or 15 (FizzBuzz)

public class Main {
    public static void main(String[] args) {
        int num = 30; // 30 is divisible by 15
        if(num % 3 == 0 && num % 5 == 0){
            System.out.println("FizzBuzz");
        }else if(num % 5 == 0){
            System.out.println("Fizz");
        }else if(num % 3 == 0){
            System.out.println("Buzz");
        }
    }
}

Find greatest common divisor of given two numbers

public class Main {
    public static void main(String[] args) {
        int a = 3;
        int b = 6;
        int big;
        if(a > b){
            big = a;
        }else{
            big = b;
        }

        for(int i = big; i > 0; i--){
            if(a % i == 0 && b % i == 0){
                System.out.println("Greatest common divisor of " + a + " and " + b + " is : " + i);
                break;
            }
        }
    }
}

Find least common multiple of given two numbers

public class Main {
    public static void main(String[] args) {
        int a = 6;
        int b = 8;
        int leastCommonMultiple = 1;

        int i = 2;
        while(true){
            if(a % i != 0 && b % i != 0){
                i += 1;
            }

            if(a % i == 0 || b % i == 0){
                leastCommonMultiple *= i;
            }

            if(a % i == 0){
                a /= i;
            }

            if(b % i == 0){
                b /= i;
            }

            if(a == 1 && b == 1){
                break;
            }
        }
        System.out.println("Least common multiple : " + leastCommonMultiple);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment