Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views7 pages

Array programs class 10 java

Java program class 10

Uploaded by

sidhuprasad242
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Array programs class 10 java

Java program class 10

Uploaded by

sidhuprasad242
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

import java.util.

*;
class Transpose {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int r = sc.nextInt();
System.out.print("Enter number of columns: ");
int c = sc.nextInt();
int arr[][] = new int[r][c];

System.out.println("Enter elements:");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
arr[i][j] = sc.nextInt();
}
}

System.out.println("Transpose:");
for (int i = 0; i < c; i++) {
for (int j = 0; j < r; j++) {
System.out.print(arr[j][i] + " ");
}
System.out.println();
}
}
}

import java.util.*;
class SymmetricCheck {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of square matrix: ");
int n = sc.nextInt();
int arr[][] = new int[n][n];

System.out.println("Enter elements:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}

boolean symmetric = true;


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (arr[i][j] != arr[j][i]) {
symmetric = false;
break;
}
}
if (!symmetric) break;
}

if (symmetric)
System.out.println("The matrix is Symmetric.");
else
System.out.println("The matrix is NOT Symmetric.");
}
}

import java.util.*;
class DiagonalSum {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int m[][] = new int[4][4]; // fixed 4x4 matrix like in book
System.out.println("Enter 16 elements:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
m[i][j] = sc.nextInt();
}
}

int ld = 0, rd = 0;

// Left diagonal
for (int i = 0; i < 4; i++) {
ld += m[i][i];
}

// Right diagonal with k = 3


int k = 3; // hardcoded for 4x4 matrix
for (int i = 0; i < 4; i++) {
rd += m[i][k];
k--;
}
System.out.println("Left Diagonal Sum: " + ld);
System.out.println("Right Diagonal Sum: " + rd);
}
}

import java.util.*;
class RowColSum {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int r = sc.nextInt();
System.out.print("Enter number of columns: ");
int c = sc.nextInt();
int arr[][] = new int[r][c];

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


for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
arr[i][j] = sc.nextInt();
}
}

// Row sums
System.out.println("\nSum of each row:");
for (int i = 0; i < r; i++) {
int rowSum = 0;
for (int j = 0; j < c; j++) {
rowSum += arr[i][j]; // row fixed, loop through columns
}
System.out.println("Row " + (i + 1) + ": " + rowSum);
}

// Column sums (note the swapped indices)


System.out.println("\nSum of each column:");
for (int i = 0; i < c; i++) { // now i = column index
int colSum = 0;
for (int j = 0; j < r; j++) { // j = row index
colSum += arr[j][i]; // accessing by [row][column]
}
System.out.println("Column " + (i + 1) + ": " + colSum);
}
}
}

import java.util.*;
class ReverseArray {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements:");
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter " + n + " numbers:");
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();

System.out.println("Array in reverse order:");


for (int i = n - 1; i >= 0; i--)
System.out.print(arr[i] + " ");
}
}

import java.util.*;
class SortArray {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements:");
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter " + n + " numbers:");
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println("Sorted array:");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}

import java.util.*;
class EvenOddCount {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements:");
int n = sc.nextInt();
int arr[] = new int[n];
int even = 0, odd = 0;
System.out.println("Enter " + n + " numbers:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
if (arr[i] % 2 == 0) even++;
else odd++;
}
System.out.println("Even count: " + even);
System.out.println("Odd count: " + odd);
}
}

import java.util.*;
class LargestSmallest {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements:");
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter " + n + " numbers:");
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
int max = arr[0], min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
System.out.println("Largest: " + max);
System.out.println("Smallest: " + min);
}
}

import java.util.*;
class SearchArray {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements:");
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter " + n + " numbers:");
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
System.out.println("Enter number to search:");
int key = sc.nextInt();
boolean found = false;
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
found = true;
break;
}
}
if (found)
System.out.println("Element found.");
else
System.out.println("Element not found.");
}
}

import java.util.*;
class ArraySum {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements:");
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter " + n + " numbers:");
int sum = 0;
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
sum += arr[i];
}
System.out.println("Sum of elements: " + sum);
}
}

You might also like