
Java Programming
June 10, 2025 at 12:39 PM
💻 *Java Coding Challenges – Part 2* 🔥
*6️⃣ 🔄 Swap Two Numbers Without Using 3rd Variable*
*Task:* Write a function to swap two numbers without a temp variable.
```
public static void swap(int a, int b) {
System.out.println("Before Swap: a = " + a + ", b = " + b);
a = a + b;
b = a - b;
a = a - b;
System.out.println("After Swap: a = " + a + ", b = " + b);
}
```
*7️⃣ 📊 Count Vowels and Consonants in a String*
*Task:* Count the number of vowels and consonants in a given string.
```
public static void countVC(String str) {
str = str.toLowerCase();
int vowels = 0, consonants = 0;
for (char c : str.toCharArray()) {
if (Character.isLetter(c)) {
if ("aeiou".indexOf(c) != -1) vowels++;
else consonants++;
}
}
System.out.println("Vowels: " + vowels + ", Consonants: " + consonants);
}
```
*8️⃣ 📐 Fibonacci Series Using Recursion*
*Task:* Generate nth Fibonacci number using recursion.
```
public static int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
```
*9️⃣ 🧮 Check Prime Number*
*Task:* Determine if a number is prime.
```
public static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
```
*🔟 🔢 Sum of Digits of a Number*
*Task:* Find the sum of all digits in an integer.
```
public static int sumDigits(int n) {
int sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
```
*Double Tap ❤️ for more!*
❤️
👍
❤
🙏
♥
🇮🇳
👏
😮
80