import java.util.Scanner; public class CaesarCipher { static String encode(String text, int key) { String result = ""; for (char ch : text.toCharArray()) { if (ch >= 'A' && ch <= 'Z') { ch = (char) ((ch - 'A' + key) % 26 + 'A'); } else if (ch >= 'a' && ch <= 'z') { ch = (char) ((ch - 'a' + key) % 26 + 'a'); } result += ch; } return result; } static String decode(String text, int key) { String result = ""; for (char ch : text.toCharArray()) { if (ch >= 'A' && ch <= 'Z') { ch = (char) ((ch - 'A' - key + 26) % 26 + 'A'); } else if (ch >= 'a' && ch <= 'z') { ch = (char) ((ch - 'a' - key + 26) % 26 + 'a'); } result += ch; } return result; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter text: "); String text = sc.nextLine(); System.out.print("Enter key: "); int key = sc.nextInt(); key = key % 26; String encodedText = encode(text, key); String decodedText = decode(encodedText, key); System.out.println("\nOriginal Text : " + text); System.out.println("Encoded Text : " + encodedText); System.out.println("Decoded Text : " + decodedText); sc.close(); } }