ch
Feedback
Tech Jargon - Decoded

Tech Jargon - Decoded

前往频道在 Telegram

Confused by tech terms? Don’t worry, we’ve got you 🤝 We make things simple, one concept at a time. Learn daily Easy & clear Turn Confusion into clarity. #tech #it #softwareengineer #cs #development

显示更多
2 015
订阅者
-224 小时
-67
-4130
帖子存档
Decimal to Binary, Octal, Hexadecimal Conversion in Java
public class DecimalConverter {
    public static void main(String[] args) {
        int decimal = 255;

        String binary = Integer.toBinaryString(decimal);
        String octal = Integer.toOctalString(decimal);
        String hexadecimal = Integer.toHexString(decimal);

        System.out.println("Decimal: " + decimal);
        System.out.println("Binary: " + binary);
        System.out.println("Octal: " + octal);
        System.out.println("Hexadecimal: " + hexadecimal);
    }
}

#Java #Algorithm

Find Digital Root
public class DigitalRoot {

    public static int digitalRoot(int n) {
        while (n >= 10) {
            int sum = 0;
            while (n > 0) {
                sum += n % 10;
                n /= 10;
            }
            n = sum;
        }
        return n;
    }

    public static void main(String[] args) {
        System.out.println(digitalRoot(16));
        System.out.println(digitalRoot(942));
        System.out.println(digitalRoot(493193));
    }
}

#Java #BigInteger

Factorial using BigInteger
import java.math.BigInteger;

public class FactorialBigInteger {

    public static BigInteger factorial(int n) {
        BigInteger result = BigInteger.ONE;
        for (int i = 2; i <= n; i++) {
            result = result.multiply(BigInteger.valueOf(i));
        }
        return result;
    }

    public static void main(String[] args) {
        int number = 50;
        BigInteger fact = factorial(number);
        System.out.println("Factorial of " + number + " is: " + fact);
    }
}

#Java #Recursion #Algorithm

Sum of Digits (Recursive & Iterative)
public class SumOfDigits {

    public static int sumDigitsIterative(int n) {
        int sum = 0;
        while (n > 0) {
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }

    public static int sumDigitsRecursive(int n) {
        if (n == 0) {
            return 0;
        }
        return (n % 10) + sumDigitsRecursive(n / 10);
    }

    public static void main(String[] args) {
        int number = 12345;
        System.out.println("Iterative Sum: " + sumDigitsIterative(number));
        System.out.println("Recursive Sum: " + sumDigitsRecursive(number));
    }
}

#Java #Algorithm #Math

Sum of First N Even/Odd Numbers
public class SumEvenOdd {
    public static int sumOfFirstNEven(int n) {
        return n * (n + 1);
    }

    public static int sumOfFirstNOdd(int n) {
        return n * n;
    }

    public static void main(String[] args) {
        int n = 5;
        System.out.println("Sum of first " + n + " even numbers: " + sumOfFirstNEven(n));
        System.out.println("Sum of first " + n + " odd numbers: " + sumOfFirstNOdd(n));
    }
}

#Java #Algorithms

Sum of First N Natural Numbers
public class SumOfN {
    public static int sum(int n) {
        if (n <= 0) {
            return 0;
        }
        return n * (n + 1) / 2;
    }

    public static void main(String[] args) {
        int number = 5;
        int result = sum(number);
        System.out.println("Sum of first " + number + " natural numbers: " + result);
    }
}

#Java #ReverseNumber #CodingInterview

Reverse a Number
public class ReverseNumber {

    public static int reverse(int number) {
        int reversed = 0;
        while (number != 0) {
            int digit = number % 10;
            reversed = reversed * 10 + digit;
            number /= 10;
        }
        return reversed;
    }

    public static void main(String[] args) {
        int num = 12345;
        int reversedNum = reverse(num);
        System.out.println("Original Number: " + num);
        System.out.println("Reversed Number: " + reversedNum);
    }
}

#Java #Algorithm #Digits

Count Digits Without % or /
public class DigitCounter {
    public static int countDigits(int num) {
        if (num == 0) {
            return 1;
        }
        int count = 0;
        int temp = Math.abs(num);
        while (temp > 0) {
            temp = temp - (int)Math.pow(10, (int)(Math.log10(temp)));
            count++;
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(countDigits(12345));
        System.out.println(countDigits(-987));
        System.out.println(countDigits(0));
    }
}

#Java #Algorithms #CodingInterview

Swap Two Numbers Without Temp Variable
public class SwapNumbers {
    public static void main(String[] args) {
        int a = 5;
        int b = 10;
        a = a + b;
        b = a - b;
        a = a - b;
        System.out.println("a = " + a);
        System.out.println("b = " + b);
    }
}

#Java #Algorithms

Sum of First N Natural Numbers
public class SumOfN {
    public static int sum(int n) {
        if (n <= 0) {
            return 0;
        }
        return n * (n + 1) / 2;
    }

    public static void main(String[] args) {
        int number = 5;
        int result = sum(number);
        System.out.println("Sum of first " + number + " natural numbers: " + result);
    }
}

#Java #ReverseNumber #CodingInterview