Program 15:
Write a program to input a sentence and arrange in 2 dimensional Array and fill the
space of the sentences with “*” and empty spaces with “-“
import java.util.*;
public class twoDimensionalCharacterArray
public static void main(String[] args) {
int row = 6, col = 7;
char[][] chars = new char[row][col];
Scanner scan = new Scanner(System.in);
System.out.print("Type in a sentence: ");
String message = scan.nextLine();
char[] messages = message.toCharArray();
int i = 0;
for (int r = 0; r < chars.length; r++) {
for (int c = 0; c < col; c++) {
if (i < messages.length) {
chars[r][c] = messages[i] == ' ' ? '*' : messages[i];
i++;
} else {
chars[r][c] = '-';
for (char[] x : chars) {
System.out.println(Arrays.toString(x));
}
}
Program 19:
Program to print the Calendar by user input of year and month
import java.util.Calendar;
import java.util.Scanner;
public class Calendarpgm
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter year: ");
int year = scanner.nextInt();
System.out.print("Enter month (1-12): ");
int month = scanner.nextInt() - 1; // Adjust for 0-indexed months
printCalendar(year, month);
public static void printCalendar(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, 1);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; // Adjust for 0-
indexed days
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
// Print leading spaces
for (int i = 0; i < firstDayOfWeek; i++) {
System.out.print(" ");
}
// Print days of the month
for (int day = 1; day <= daysInMonth; day++) {
System.out.printf(" %3d ", day);
if ((day + firstDayOfWeek) % 7 == 0) {
System.out.println();
System.out.println();
PROGRAM 20:
Program to print Factorial of a number and to check whether the number is prime as
a Menu Driven.
import java.util.Scanner;
public class MenuProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println("\n=== MENU ===");
System.out.println("1. Calculate Factorial");
System.out.println("2. Check Prime Number");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter a number to find its factorial: ");
int num = sc.nextInt();
long factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
System.out.println("Factorial of " + num + " is " + factorial);
break;
case 2:
System.out.print("Enter a number to check if it's prime: ");
int primeCandidate = sc.nextInt();
boolean isPrime = true;
if (primeCandidate <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(primeCandidate); i++) {
if (primeCandidate % i == 0) {
isPrime = false;
break;
if (isPrime) {
System.out.println(primeCandidate + " is a prime number.");
} else {
System.out.println(primeCandidate + " is not a prime number.");
break;
case 3:
System.out.println("Exiting the program. See you!");
break;
default:
System.out.println("Invalid choice. Try again.");
} while (choice != 3);
sc.close();