Task 1
import java.util.Scanner;
public class T1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int summation= 0;
int minimum = Integer.MAX_VALUE;
int maximum = Integer.MIN_VALUE;
int c = 0;
System.out.println("Enter 10 numbers:");
for (int j = 0; j < 10; j++) {
int number = sc.nextInt();
if (number> 0 && number % 2 != 0) {
summation += number;
c++;
if (number < minimum) {
minimum = number;
}
if (number>maximum) {
maximum = number;
}
}
}
if (c > 0) {
double average = (double) summation / c;
System.out.println("Sum = " + summation);
System.out.println("Minimum = " + minimum);
System.out.println("Maximum = " + maximum);
System.out.println("Average = " + average);
} else {
System.out.println("No odd positive numbers found");
}
}
}
Task 2
import java.util.Scanner;
public class T2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = sc.nextInt();
System.out.print("Enter the second number: ");
int num2 = sc.nextInt();
if (num1 > num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
int primeC = 0;
for (int j= num1; j <= num2; j++) {
if (j <= 1) {
continue;
}
boolean isPrime = true;
for (int i = 2; i<= Math.sqrt(i); i++) {
if (j % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primeC++;
}
}
System.out.println("There are " + primeC + " prime numbers between " + num1
+ " and " + num2+ ".");
}
}
Task 3
import java.util.Scanner;
public class T3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first string: ");
String s1 = sc.nextLine();
System.out.print("Enter the second string: ");
String s2 = sc.nextLine();
String stringConcatenation = s1 + " " + s2;
System.out.println("Concatenated String: " + stringConcatenation);
int sumAscii = 0;
for (int i = 0; i < stringConcatenation.length(); i++) {
char ch = stringConcatenation.charAt(i);
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
sumAscii += ch;
}
}
System.out.println("Sum of ASCII values of letters: " + sumAscii);
}
}
Task 4
import java.util.Scanner;
public class T4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string in lowercase letters: ");
String in = sc.nextLine();
String res = "";
for (int j = 0; j < in.length(); j++) {
char ch = in.charAt(j);
if (ch >= 'a' && ch <= 'z') {
if (ch == 'a') {
res += 'z';
} else {
res += (char) (ch - 1);
}
} else {
res += ch;
}
}
System.out.println("Output: " + res);
}
}
Task 5
import java.util.Scanner;
public class T5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length of array: ");
int len = sc.nextInt();
int[] arr = new int[len];
System.out.println("Enter " + len + " elements:");
for (int j = 0; j < len; j++) {
arr[j] = sc.nextInt();
}
for (int j = 0; j < len / 2; j++) {
int temp = arr[j];
arr[j] = arr[len - 1 - j];
arr[len- 1 - j] = temp;
}
System.out.println("Reversed array:");
for (int j = 0; j < len; j++) {
System.out.print(arr[j] + " ");
}
}
}
Task 6
import java.util.Scanner;
public class T6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements ");
int num1 = sc.nextInt();
int[] num = new int[num1];
System.out.println("Enter " + num1 + " numbers:");
for (int j = 0; j < num1; j++) {
num[j] = sc.nextInt();
}
boolean[] isCounted = new boolean[num1];
for (int j = 0; j< num1; j++) {
if (!isCounted[j]) {
int c = 1;
for (int k = j + 1; k < num1; k++) {
if (num[j] == num[k]) {
c++;
isCounted[k] = true;
}
}
System.out.println(num[j] + " - " + c + " times");
}
}
}
}
Task 7
import java.util.Scanner;
public class T7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements (N): ");
int num1 = sc.nextInt();
double[] originalArr = new double[num1];
System.out.println("Enter the elements of the array:");
for (int j = 0; j < num1; j++) {
originalArr[j] = sc.nextDouble();
}
double[] arrayTemp = new double[num1];
int newArrLen = 0;
arrayTemp[newArrLen++] = originalArr[0];
for (int j = 1; j < num1; j++) {
if (originalArr[j] != originalArr[j - 1]) {
arrayTemp[newArrLen++] = originalArr[j];
}
}
int remEle = num1- newArrLen;
System.out.print("New Array: ");
for (int j = 0; j < newArrLen; j++) {
System.out.print(arrayTemp[j] + " ");
}
System.out.println();
System.out.println("Removed elements: " + remEle);
}
}
Task 8
import java.util.Scanner;
public class T8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int r = sc.nextInt();
System.out.print("Enter the number of columns: ");
int c = sc.nextInt();
int[][] arr2 = new int[r][c];
System.out.println("Enter the elements of the array:");
for (int j = 0; j < r; j++) {
for (int k = 0; k< c; k++) {
arr2[j][k] = sc.nextInt();
}
}
System.out.println("2D Array:");
for (int j = 0; j < r; j++) {
for (int k = 0; k < c; k++) {
System.out.print(arr2[j][k] + " ");
}
System.out.println();
}
int[] arr1 = new int[r * c];
int in = 0;
for (int j = 0; j < r; j++) {
for (int k = 0; k < c; k++) {
arr1[in] = arr2[j][k];
in++;
}
}
System.out.println("1D Array:");
for (int j = 0; j < arr1.length; j++) {
System.out.print(arr1[j] + " ");
}
}
}
Task 9
public class T9 {
public static void main(String[] args) {
int[][] mat1 = {
{1, 0, 0, 1},
{0, 1, 0, 0},
{1, 0, 1, 0},
{0, 1, 0, 1}
};
int[][] mat2 = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
};
checkIdentityMatrix(mat1);
checkIdentityMatrix(mat2);
}
public static void checkIdentityMatrix(int[][] mat) {
boolean isIdentityMatrix = true;
int num = mat.length;
for (int j = 0; j < num; j++) {
for (int k = 0; k< num; k++) {
if (j == k && mat[j][k] != 1) {
isIdentityMatrix = false;
break;
}
if (j != k && mat[j][k] != 0) {
isIdentityMatrix = false;
break;
}
}
if (!isIdentityMatrix) {
break;
}
}
if (isIdentityMatrix) {
System.out.println("Identity Matrix");
} else {
System.out.println("Not an Identity Matrix");
}
}
}
Task 10
import java.util.Scanner;
public class T10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] arr2D = {
{0, 0, 10, 0, -1},
{0, -1, 0, 0, -1},
{-1, 0, -1, 0, 0},
{0, -1, 7, 0, -1},
{0, -1, 0, -1, 0}
};
System.out.println("Initial Map:");
printMap(arr2D);
int row = arr2D.length;
int col = arr2D[0].length;
int row_pos = -1, col_pos = -1;
for (int j = 0; j < row; j++) {
for (int k = 0; k < col; k++) {
if (arr2D[j][k] == 7) {
row_pos = j;
col_pos = k;
break;
}
}
}
int turns = 5;
boolean treasureDiscovered = false;
while (turns > 0 && !treasureDiscovered) {
int new_row = row_pos,new_col = col_pos;
System.out.printf("Enter move %d (UP, DOWN, LEFT, RIGHT): ", (6 -
turns));
String inp = sc.nextLine().toUpperCase();
if (inp.equals("UP")) {
new_row--;
} else if (inp.equals("DOWN")) {
new_row++;
} else if (inp.equals("LEFT")) {
new_col--;
} else if (inp.equals("RIGHT")) {
new_col++;
} else {
System.out.println("That’s not a valid move. Please try again.");
continue;
}
if (new_row < 0 || new_row >= row || new_col < 0 || new_col >= col) {
System.out.println("You have stepped outside the grid! The game is
over.");
break;
}
if (arr2D[new_row][new_col] == -1) {
System.out.println("You hit a mine! The game is over.");
break;
} else if (arr2D[new_row][new_col] == 10) {
System.out.println("Well done! You discovered the treasure!");
treasureDiscovered = true;
} else {
arr2D[row_pos][col_pos] = 0;
arr2D[new_row][new_col] = 7;
row_pos = new_row;
col_pos = new_col;
System.out.println("Current state:");
printMap(arr2D);
}
turns--;
}
if (!treasureDiscovered && turns == 0) {
System.out.println("Failed to find the treasure.");
}
public static void printMap(int[][] arr) {
for (int[] row : arr) {
for (int element : row) {
System.out.print(element + " ");
}
System.out.println();
}
}
}