In this post, I’ll walk you through 10 simple but powerful Java mini programs. Each one targets a specific concept, and together they’ll help you think like a programmer, not just a code writer.
Let’s dive in! 🚀
1️⃣ Reverse a String (Manual Way):
public class ReverseString {
public static void main(String[] args) {
String str = "Java";
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i);
}
System.out.println(reversed); // Output: avaJ
}
}
✅ Explanation: Loops from the last character to the first, building a new reversed string.
✅ Enforces: Loops, string indexing, and concatenation.
2️⃣ Check if a Number is Prime
public class PrimeCheck {
public static void main(String[] args) {
int num = 29, count = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) count++;
}
System.out.println(count == 2 ? "Prime" : "Not Prime");
}
}
✅ Explanation: A prime number has exactly 2 divisors: 1 and itself.
✅ Enforces: Loops and conditional logic.
3️⃣ Fibonacci Series
public class Fibonacci {
public static void main(String[] args) {
int n = 10, a = 0, b = 1;
for (int i = 0; i < n; i++) {
System.out.print(a + " ");
int sum = a + b;
a = b;
b = sum;
}
}
}
✅ Explanation: Generates the first n numbers in the Fibonacci sequence.
✅ Enforces: Iteration and updating variables.
4️⃣ Factorial of a Number
public class Factorial {
public static void main(String[] args) {
int n = 5, fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
System.out.println("Factorial: " + fact); // Output: 120
}
}
✅ Explanation: Multiplies numbers from 1 to n.
✅ Enforces: Looping and accumulator logic.
5️⃣ Palindrome Check
public class Palindrome {
public static void main(String[] args) {
String str = "level";
String reversed = new StringBuilder(str).reverse().toString();
System.out.println(str.equals(reversed) ? "Palindrome" : "Not Palindrome");
}
}
✅ Explanation: A palindrome reads the same forwards and backwards.
✅ Enforces: String comparison and reversal.
6️⃣ Sum of Digits
public class DigitSum {
public static void main(String[] args) {
int num = 1234, sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum of digits: " + sum); // Output: 10
}
}
✅ Explanation: Extracts digits using % and /.
✅ Enforces: Loops and modular arithmetic.
7️⃣ Find Largest Element in an Array
public class LargestInArray {
public static void main(String[] args) {
int[] arr = {2, 9, 5, 1, 7};
int max = arr[0];
for (int n : arr) {
if (n > max) max = n;
}
System.out.println("Largest: " + max);
}
}
✅ Explanation: Iterates through array to find maximum value.
✅ Enforces: Arrays and enhanced for-loops.
8️⃣ Count Vowels in a String
public class VowelCount {
public static void main(String[] args) {
String str = "Hello Java";
int count = 0;
for (char c : str.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(c) != -1) count++;
}
System.out.println("Vowels: " + count); // Output: 4
}
}
✅ Explanation: Checks if characters belong to the vowel set.
✅ Enforces: Character iteration and string operations.
9️⃣ Simple Number Guessing Game
import java.util.*;
public class GuessGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = new Random().nextInt(10) + 1;
System.out.print("Guess a number (1-10): ");
int guess = sc.nextInt();
if (guess == number) {
System.out.println("Correct!");
} else {
System.out.println("Wrong! Number was " + number);
}
}
}
✅ Explanation: Random number generation with user input.
✅ Enforces: Scanner, randomness, and conditionals.
🔟 Matrix Addition
public class MatrixAddition {
public static void main(String[] args) {
int[][] a = {{1,2},{3,4}};
int[][] b = {{5,6},{7,8}};
int[][] sum = new int[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
for (int[] row : sum) {
for (int val : row) System.out.print(val + " ");
System.out.println();
}
}
}
✅ Explanation: Adds two 2×2 matrices element by element.
✅ Enforces: Nested loops and multidimensional arrays.
🎯 Wrapping Up
These mini-programs might look simple, but together they cover:
Loops & conditionals
Arrays & strings
Methods & modular arithmetic
Randomness & user input
💬 Your turn:
Which one of these programs have you already mastered?
Which one do you want me to expand into a full breakdown?
Drop your thoughts in the comments! 🚀
📚 Resources to Practice More:
Top comments (0)