0% found this document useful (0 votes)
26 views6 pages

Cse111 Lab 1

The document consists of ten Java tasks, each implementing different functionalities such as calculating sums and averages of odd positive numbers, counting prime numbers, manipulating strings, reversing arrays, counting occurrences of elements, removing consecutive duplicates, flattening a 2D array, checking for identity matrices, and more. Each task includes user input and output to demonstrate its functionality. The tasks are designed to practice various programming concepts including loops, conditionals, and array manipulations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

Cse111 Lab 1

The document consists of ten Java tasks, each implementing different functionalities such as calculating sums and averages of odd positive numbers, counting prime numbers, manipulating strings, reversing arrays, counting occurrences of elements, removing consecutive duplicates, flattening a 2D array, checking for identity matrices, and more. Each task includes user input and output to demonstrate its functionality. The tasks are designed to practice various programming concepts including loops, conditionals, and array manipulations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

TASK1

import java.util.Scanner;

public class LabTask1 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0, min = Integer.MAX_VALUE, max = Integer.MIN_VALUE, count = 0;

for (int i = 0; i < 10; i++) {


int n = sc.nextInt();
if (n > 0 && n % 2 == 1) {
sum += n;
count++;
if (n < min) {
min = n;
}
if (n > max) {
max = n;
}
}
}

if (count == 0) {
System.out.println("No odd positive numbers found");
} else {
double average = (double) sum / count;
System.out.println("Sum = " + sum);
System.out.println("Minimum = " + min);
System.out.println("Maximum = " + max);
System.out.println("Average = " + average);
}
sc.close();
}
}

TASK2
import java.util.Scanner;

public class LabTask2 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
sc.close();

if (a > b) {
int temp = a;
a = b;
b = temp;
}

int primeCount = 0;
for (int i = a; i <= b; i++) {
if (isPrime(i)) {
primeCount++;
}
}

System.out.println("There are " + primeCount + " prime numbers between " +


a + " and " + b + ".");
}
public static boolean isPrime(int num) {
if (num < 2) {
return false;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}

TASK3
import java.util.Scanner;

public class LabTask3 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter first word: ");


String firstWord = scanner.next();

System.out.print("Enter second word: ");


String secondWord = scanner.next();
String concatenatedString = firstWord + " " + secondWord;

int sum = 0;
for (int i = 0; i < concatenatedString.length(); i++) {
sum += (int) concatenatedString.charAt(i);
}

System.out.println(concatenatedString);
System.out.println(sum);

scanner.close();
}
}

TASK4
import java.util.Scanner;

public class AlphabetSequence {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a word: ");
String input = scanner.next();
scanner.close();

String result = "";

for (int i = 0; i < input.length(); i++) {


char ch = input.charAt(i);
if (ch > 'a' && ch <= 'z') {
result += (char) (ch - 1);
} else if (ch == 'a') {
result += 'z';
} else {
result += ch;
}
}

System.out.println("Modified word: " + result);


}
}

TASK5
import java.util.Scanner;

public class ReverseArray {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter the length of the array: ");


int n = sc.nextInt();
int[] arr = new int[n];

for (int i = 0; i < n; i++) {


arr[i] = sc.nextInt();
}

for (int i = 0, j = n - 1; i < j; i++, j--) {


int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

for (int i = 0; i < n; i++) {


System.out.print(arr[i] + " ");
}

sc.close();
}
}

TASK6
import java.util.Scanner;

public class CountOccurrences {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of elements: ");


int N = scanner.nextInt();

int[] numbers = new int[N];

System.out.println("Enter " + N + " numbers:");


for (int i = 0; i < N; i++) {
numbers[i] = scanner.nextInt();
}

boolean[] checked = new boolean[N]; // To keep track of counted numbers

for (int i = 0; i < N; i++) {


if (!checked[i]) {
int count = 1;
for (int j = i + 1; j < N; j++) {
if (numbers[i] == numbers[j]) {
count++;
checked[j] = true;
}
}
System.out.println(numbers[i] + " - " + count + " times");
}
}

scanner.close();
}
}

TASK7
import java.util.Scanner;

public class RemoveConsecutiveDuplicates {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of elements in the array: ");


int N = scanner.nextInt();

double[] originalArray = new double[N];


System.out.println("Please enter the elements of the array:");

for (int i = 0; i < N; i++) {


originalArray[i] = scanner.nextDouble();
}

double[] tempArray = new double[N];


int index = 0;
tempArray[index++] = originalArray[0];

int removedCount = 0;

for (int i = 1; i < N; i++) {


if (originalArray[i] != originalArray[i - 1]) {
tempArray[index++] = originalArray[i];
} else {
removedCount++;
}
}

System.out.print("New Array: ");


for (int i = 0; i < index; i++) {
System.out.print(tempArray[i] + " ");
}
System.out.println();

System.out.println("Removed elements: " + removedCount);

scanner.close();
}
}

TASK8
import java.util.Scanner;

public class Flatten2DArray {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter number of rows: ");


int rows = scanner.nextInt();
System.out.print("Enter number of columns: ");
int columns = scanner.nextInt();

int[][] array2D = new int[rows][columns];

System.out.println("Enter elements of the array:");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array2D[i][j] = scanner.nextInt();
}
}

System.out.println("2D Array:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(array2D[i][j] + " ");
}
System.out.println();
}

int[] array1D = new int[rows * columns];


int index = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array1D[index] = array2D[i][j];
index++;
}
}

System.out.println("1D Array:");
for (int i = 0; i < array1D.length; i++) {
System.out.print(array1D[i] + " ");
}

scanner.close();
}
}

TASK9
import java.util.Scanner;

public class IdentityMatrixChecker {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter matrix size: ");


int n = scanner.nextInt();

int[][] matrix = new int[n][n];

System.out.println("Enter matrix elements:");


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = scanner.nextInt();
}
}

boolean identity = true;

for (int i = 0; i < n; i++) {


for (int j = 0; j < n; j++) {
if ((i == j && matrix[i][j] != 1) || (i != j && matrix[i][j] != 0))
{
identity = false;
}
}
}

if (identity) {
System.out.println("Identity matrix");
} else {
System.out.println("Not an Identity matrix");
}

scanner.close();
}
}

TASK10

You might also like