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)