X-ICSE_Chapter 2_Library classes
6. Write a program to input a letter. Find its ASCII code. Reverse the ASCII code and display the
equivalent character.
import java.util.Scanner;
public class ReverseAsciiCharacter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Step 1: Input a character
System.out.print("Enter a single letter: ");
char inputChar = sc.next().charAt(0);
// Step 2: Get ASCII code
int ascii = (int) inputChar;
System.out.println("ASCII code of '" + inputChar + "' is: " + ascii);
// Step 3: Reverse the ASCII code
int reversedAscii = 0, temp = ascii;
while (temp > 0) {
int lastDigit = temp % 10;
reversedAscii = reversedAscii * 10 + lastDigit;
temp = temp / 10;
}
// Step 4: Display equivalent character (if valid)
if (reversedAscii >= 0 && reversedAscii <= 127) {
char reversedChar = (char) reversedAscii;
System.out.println("Reversed ASCII code: " + reversedAscii);
System.out.println("Character of reversed ASCII code: '" + reversedChar + "'");
} else {
System.out.println("Reversed ASCII code " + reversedAscii + " is out of valid ASCII
range.");
}
}
}
11. Using switch case statement, write a menu driven program to perform the following tasks:
a) To generate and print the letters from A to Z along with their Unicode.
b) To generate and print the letters from z to a along with their Unicode:
import java.util.Scanner;
public class UnicodeLettersMenu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
System.out.println("Menu:");
System.out.println("1. Print letters from A to Z with Unicode");
System.out.println("2. Print letters from z to a with Unicode");
System.out.print("Enter your choice (1 or 2): ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Letters from A to Z with Unicode:");
for (char ch = 'A'; ch <= 'Z'; ch++) {
System.out.println(ch + " : " + (int) ch);
}
break;
case 2:
System.out.println("Letters from z to a with Unicode:");
for (char ch = 'z'; ch >= 'a'; ch--) {
System.out.println(ch + " : " + (int) ch);
}
break;
default:
System.out.println("Invalid choice! Please enter 1 or 2.");
}
}
}
(OR)
import java.util.Scanner;
public class UnicodeLettersMenu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
// Display menu
System.out.println("Menu:");
System.out.println("1. Print letters from A to Z with Unicode");
System.out.println("2. Print letters from z to a with Unicode");
System.out.print("Enter your choice (1 or 2): ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Letters from A to Z with Unicode:");
for (int i = 65; i <= 90; i++) { // ASCII values of A to Z
System.out.println((char) i + " : " + i);
}
break;
case 2:
System.out.println("Letters from z to a with Unicode:");
for (int i = 122; i >= 97; i--) { // ASCII values of z to a
System.out.println((char) i + " : " + i);
}
break;
default:
System.out.println("Invalid choice! Please enter 1 or 2.");
}
}
}
12. Write a program in Java to display the following patterns:
a) ABCDE
ABCD
ABC
AB
A
public class AlphabetPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 0; i < rows; i++) {
// Print characters from 'A' up to the required letter
for (char ch = 'A'; ch <= 'E' - i; ch++) {
System.out.print(ch);
}
// Move to the next line
System.out.println();
}
}
}
b) ABCDE
ABCD
ABC
AB
A
public class AlphabetPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 0; i < rows; i++) {
// Print leading spaces
for (int s = 0; s < i; s++) {
System.out.print(" ");
}
// Print characters from 'A' up to the required letter
for (char ch = 'A'; ch <= 'E' - i; ch++) {
System.out.print(ch);
}
// Move to the next line
System.out.println();
}
}
}
c) A
BC
DCF
GHIJ
KLMNO
public class AlphabetLeftAligned {
public static void main(String[] args) {
int rows = 5;
char ch = 'A';
for (int i = 1; i <= rows; i++) {
// Print characters with a space
for (int j = 1; j <= i; j++) {
System.out.print(ch + " ");
ch++;
}
System.out.println();
}
}
}
c) A B C D E
BCDE
CDE
DE
E
public class AlphabetLeftShifted {
public static void main(String[] args) {
int rows = 5;
for (int i = 0; i < rows; i++) {
// Print characters from (A + i) to E
for (char ch = (char) ('A' + i); ch <= 'E'; ch++) {
System.out.print(ch + " ");
}
System.out.println();
}
}
}
d) A*B*C*D*E*
A*B*C*D*
A*B*C*
A*B*
A*
public class AlphabetStarPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; i--) {
char ch = 'A';
for (int j = 1; j <= i; j++) {
System.out.print(ch + "*");
ch++;
}
System.out.println();
}
}
}
e)A A A A A
BBBBB
CCCCC
DDDDD
EEEEE
public class AlphabetSquarePattern {
public static void main(String[] args) {
int rows = 5;
int cols = 5;
char ch = 'A';
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
System.out.print(ch + " ");
}
System.out.println();
ch++; // Move to next alphabet
}
}
}
f) A B C D E
ABCDA
ABCAB
ABABC
AABCD
public class CustomAlphabetPattern {
public static void main(String[] args) {
int rows = 5;
char[][] pattern = new char[rows][rows];
// Fill first row with A to E
for (int i = 0; i < rows; i++) {
pattern[0][i] = (char) ('A' + i);
}
// Fill remaining rows based on custom pattern
for (int i = 1; i < rows; i++) {
for (int j = 0; j < rows; j++) {
pattern[i][j] = pattern[i - 1][(j + 1) % rows]; // Shift and wrap
}
}
// Print the pattern
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
System.out.print(pattern[i][j] + " ");
}
System.out.println();
}
}
}
13. Write a program in java to generate a triangle or an inverted triangle till n terms based upon
the users choice of the triangle to be displayed
Pattern 1: * * * * *
****
***
**
*
Pattern 2: A B C D E
ABCD
ABC
AB
A
import java.util.Scanner;
public class TrianglePattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Choose the pattern to display:");
System.out.println("1: Inverted triangle of *");
System.out.println("2: Inverted triangle of A B C...");
System.out.print("Enter your choice (1 or 2): ");
int choice = sc.nextInt();
System.out.print("Enter number of terms (n): ");
int n = sc.nextInt();
switch (choice) {
case 1:
System.out.println("\nPattern 1: Inverted triangle of *\n");
for (int i = n; i >= 1; i--) {
// Print leading spaces
for (int s = 0; s < n - i; s++) {
System.out.print(" "); // 2 spaces for alignment
}
// Print asterisks
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
break;
case 2:
System.out.println("\nPattern 2: Inverted triangle of alphabets\n");
for (int i = n; i >= 1; i--) {
// Print leading spaces
for (int s = 0; s < n - i; s++) {
System.out.print(" ");
}
// Print letters from A
char ch = 'A';
for (int j = 1; j <= i; j++) {
System.out.print(ch + " ");
ch++;
}
System.out.println();
}
break;
default:
System.out.println("Invalid choice! Please enter 1 or 2.");
}
}
}