Java Basics: Encryption and Multiplication Table Examples for Beginners
This tutorial walks through two Java examples—a simple Caesar‑style encryption/decryption program that shifts letters by four positions and a nested‑loop implementation of the 9×9 multiplication table—explaining the logic, code, and output step by step.
1. Translation Cipher (shift by 4)
The program implements a simple substitution cipher that shifts each alphabetic character forward by four positions in the English alphabet, wrapping around at the end. Non‑alphabetic characters are left unchanged. Example: A → E, a → e, 1 → 1.
public class Encryption {
public static void main(String[] args) {
char[] arr = {'I',' ','l','o','v','e',' ','y','o','u','!'};
System.out.println("Before encryption:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
System.out.println();
System.out.println("After encryption:");
for (int i = 0; i < arr.length; i++) {
int val = arr[i];
if ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z')) {
val = val + 4;
// wrap around if past 'Z' or 'z'
if ((val > 'Z' && val <= 'Z' + 4) || val > 'z') {
val = val - 26;
}
System.out.print((char) val);
} else {
System.out.print(arr[i]);
}
}
System.out.println();
}
}Running the program prints the original string I love you! and the encrypted result M pszi csy!. The logic distinguishes uppercase and lowercase letters because their ASCII ranges differ, adds 4 to the code point, and subtracts 26 when the result exceeds the alphabet range.
public class Decryption {
public static void main(String[] args) {
char[] arr = {'M',' ','p','s','z','i',' ','c','s','y','!'};
System.out.println("Before decryption:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
System.out.println();
System.out.println("After decryption:");
for (int i = 0; i < arr.length; i++) {
int val = arr[i];
if ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z')) {
val = val - 4;
// wrap around if before 'A' or 'a'
if ((val < 'a' && val >= 'a' - 4) || val < 'A') {
val = val + 26;
}
System.out.print((char) val);
} else {
System.out.print(arr[i]);
}
}
System.out.println();
}
}The decryption restores the original text I love you! by reversing the shift and applying the same wrap‑around logic.
2. 9×9 Multiplication Table
A double for loop prints the classic multiplication table in a triangular layout. The outer loop iterates rows from 1 to 9; the inner loop iterates columns from 1 up to the current row index, ensuring j <= i so that each row contains only the products up to its diagonal.
public class MultiplicationTable {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) { // rows
for (int j = 1; j <= i; j++) { // columns up to current row
System.out.print(j + "*" + i + "=" + (i * j));
// add a space for alignment; single‑digit results get an extra space
if (i * j < 10) {
System.out.print(" ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}Sample output (first three rows) demonstrates the triangular shape:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
...The condition j <= i is essential for producing the near‑triangular format of the 9×9 table.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Lisa Notes
Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
