import java.util.Scanner;
public class CaesarCipher {
// Method to encrypt text using Caesar Cipher
public static String encrypt(String text, int shift) {
StringBuilder encrypted = new StringBuilder();
for (char character : text.toCharArray()) {
if (Character.isLetter(character)) {
char base = Character.isUpperCase(character) ? 'A' : 'a';
char newChar = (char) ((character - base + shift) % 26 + base);
encrypted.append(newChar);
} else {
encrypted.append(character); // Keep non-alphabetic characters as is
}
}
return encrypted.toString();
}
// Method to decrypt text using Caesar Cipher
public static String decrypt(String text, int shift) {
return encrypt(text, 26 - (shift % 26)); // Reversing the shift
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the text:");
String text = scanner.nextLine();
System.out.println("Enter the shift value:");
int shift = scanner.nextInt();
// Encrypt the text
String encryptedText = encrypt(text, shift);
System.out.println("Encrypted Text: " + encryptedText);
// Decrypt the text
String decryptedText = decrypt(encryptedText, shift);
System.out.println("Decrypted Text: " + decryptedText);
scanner.close();
}
}
# Initial value assignment
a = 10
print("Initial value of a:", a)
# Add and assign
a += 5
print("After a += 5:", a)
# Subtract and assign
a -= 3
print("After a -= 3:", a)
# Multiply and assign
a *= 2
print("After a *= 2:", a)
# Divide and assign
a /= 4
print("After a /= 4:", a)
# Modulus and assign
a %= 3
print("After a %= 3:", a)
# Floor divide and assign
a = 10
a //= 3
print("After a //= 3:", a)
# Exponentiate and assign
a **= 2
print("After a **= 2:", a)
# Bitwise AND and assign
a &= 6
print("After a &= 6:", a)
# Bitwise OR and assign
a |= 2
print("After a |= 2:", a)
# Bitwise XOR and assign
a ^= 3
print("After a ^= 3:", a)
# Bitwise left shift and assign
a <<= 1
print("After a <<= 1:", a)
# Bitwise right shift and assign
a >>= 2
print("After a >>= 2:", a)
No comments:
Post a Comment