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

Assignment 7 ArianAlNahin

The document contains multiple Java programs that demonstrate various array manipulations, including resizing arrays, removing duplicates, reversing arrays, modifying elements based on conditions, searching for elements, calculating statistics, and sorting arrays. Each program prompts the user for input and performs specific operations on the arrays based on the provided data. The overall focus is on basic array handling techniques in Java.

Uploaded by

arian.al.nahin
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)
1 views7 pages

Assignment 7 ArianAlNahin

The document contains multiple Java programs that demonstrate various array manipulations, including resizing arrays, removing duplicates, reversing arrays, modifying elements based on conditions, searching for elements, calculating statistics, and sorting arrays. Each program prompts the user for input and performs specific operations on the arrays based on the provided data. The overall focus is on basic array handling techniques in Java.

Uploaded by

arian.al.nahin
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/ 7

import java.util.

Scanner;
public class a1{
public static void main(String[]args){
Scanner s = new Scanner(System.in);
System.out.print("N = ");
int m = s.nextInt();
int[] x = new int[m];
for (int i = 0; i < m; i++){
System.out.print("Enter a number: ");
x[i] = s.nextInt();
}
System.out.print("Enter another number: ");
int y = s.nextInt();
System.out.println("The elements of the array are:");
for (int j = 0; j < m; j++){
System.out.println(j + ": " + x[j]);
}
int[] z = new int[m + 1];
for (int k = 0; k < m; k++){
z[k] = x[k];
}
z[m] = y;
System.out.println("After resizing the array:");
for (int p = 0; p < z.length; p++){
System.out.print(z[p] + " ");
}
}
}

import java.util.Scanner;
public class a2{
public static void main(String[]args){
Scanner s = new Scanner(System.in);
int n;
System.out.print("Enter array size: ");
n = s.nextInt();
int[] a;
a = new int[n];
int i = 0;
while (i < n){
System.out.print("Enter a number: ");
a[i] = s.nextInt();
i = i + 1;
}
System.out.println("Before removing duplicates:");
i = 0;
while (i < n){
System.out.print(a[i] + " ");
i = i + 1;
}
System.out.println();
int j;
i = 0;
while (i < n){
j = i + 1;
while (j < n){
if (a[i] == a[j]){
a[j] = 0;
}
j = j + 1;
}
i = i + 1;
}
System.out.println("After replacing duplicates with 0:");
i = 0;
while (i < n){
System.out.print(a[i] + " ");
i = i + 1;
}
}
}

import java.util.Scanner;
public class a3{
public static void main(String[]args){
Scanner s = new Scanner(System.in);
System.out.print("Enter the length of the array: ");
int n = s.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter a number: ");
a[i] = s.nextInt();
}
int[] b = new int[n];
for (int i = 0; i < n; i++){
b[i] = a[n - 1 - i];
}
System.out.println("Reversed using a new array:");
for (int i = 0; i < n; i++) {
System.out.print(b[i] + " ");
}
System.out.println();
for (int i = 0; i < n / 2; i++){
int temp = a[i];
a[i] = a[n - 1 - i];
a[n - 1 - i] = temp;
}
System.out.println("Reversed the original array:");
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
}
}

import java.util.Scanner;
public class a4{
public static void main(String[]args){
Scanner s = new Scanner(System.in);
System.out.print("Enter the length of the array (N): ");
int N = s.nextInt();
int[] a = new int[N];
int i = 0;
while (i < N){
System.out.print("Enter a number: ");
a[i] = s.nextInt();
i++;
}
System.out.println("Original array: ");
i = 0;
while (i < N){
System.out.print(a[i] + " ");
i++;
}
System.out.println();
i = 0;
while (i < N){
if (a[i] > 0){
a[i] = 1;
} else if (a[i] < 0){
a[i] = 0;
}
i++;
}
System.out.println("After modifying: ");
i = 0;
while (i < N){
System.out.print(a[i] + " ");
i++;
}
}
}

import java.util.Scanner;
public class a5{
public static void main(String[]args){
Scanner s = new Scanner(System.in);
System.out.print("Enter the length of the array (N): ");
int N = s.nextInt();
int[] a = new int[N];
for (int i = 0; i < N; i++){
System.out.print("Enter a number: ");
a[i] = s.nextInt();
}
System.out.print("Enter the number to find: ");
int ff = s.nextInt();
boolean flag = false;
for (int i = 0; i < N; i++){
if (a[i] == ff) {
System.out.println(ff + " is at index " + i);
flag = true;
break;
}
}
if (flag == false){
System.out.println("Element not found");
}
}
}
import java.util.Scanner;
public class a6{
public static void main(String[]args){
Scanner s = new Scanner(System.in);
System.out.print("Enter the length of the array: ");
int N = s.nextInt();
double[] a = new double[N];
for (int i = 0; i < N; i++){
System.out.print("Enter a number: ");
a[i] = s.nextDouble();
}
double sum = a[0], max = a[0], min = a[0];
int maxInd = 0, minInd = 0;
for (int i = 1; i < N; i++){
sum += a[i];
if (a[i] > max){
max = a[i];
maxInd = i;
}
if (a[i] < min){
min = a[i];
minInd = i;
}
}
double average = sum / N;
System.out.println("Maximum element " + max + " found at index " + maxInd);
System.out.println("Minimum element " + min + " found at index " + minInd);
System.out.println("Summation: " + sum);
System.out.println("Average: " + average);
}
}

import java.util.Scanner;
public class a7{
public static void main(String[]args){
Scanner s = new Scanner(System.in);
System.out.print("Enter the length of the array: ");
int n = s.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++){
arr[i] = s.nextInt();
}
System.out.print("Input array: ");
for (int i = 0; i < n; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.print("New array: ");
for (int i = 0; i < n; i++){
boolean flag = false;
for (int j = 0; j < i; j++){
if (arr[i] == arr[j]){
flag = true;
break;
}
}
if (!flag){
System.out.print(arr[i] + " ");
}
}
}
}

import java.util.Scanner;
public class a8{
public static void main(String[]args){
Scanner s = new Scanner(System.in);
System.out.print("Please enter the length of array 1: ");
int n1 = s.nextInt();
int[] arr1 = new int[n1];
System.out.println("Please enter the elements of the arr1:");
for (int i = 0; i < n1; i++){
arr1[i] = s.nextInt();
}
System.out.print("Please enter the length of array 2: ");
int n2 = s.nextInt();
int[] arr2 = new int[n2];
System.out.println("Please enter the elements of the arr2:");
for (int i = 0; i < n2; i++){
arr2[i] = s.nextInt();
}
int count = 0;
for (int i = 0; i < n2; i++){
for (int j = 0; j < n1; j++){
if (arr2[i] == arr1[j]){
count++;
break;
}
}
}
if (count == n2){
System.out.println("Array 2 is a subset of Array 1.");
}
else{
System.out.println("Array 2 is not a subset of Array 1.");
}
}
}

import java.util.Scanner;
public class a9{
public static void main(String[]args){
Scanner s = new Scanner(System.in);
System.out.print("N = ");
int n = s.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++){
System.out.print("Enter a number: ");
arr[i] = s.nextInt();
}
System.out.println("Original Array:");
for (int i = 0; i < n; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
for (int i = 0; i < n - 1; i++){
for (int j = 0; j < n - i - 1; j++){
if (arr[j] < arr[j + 1]){
int t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
}
System.out.println("Sorted Array:");
for (int i = 0; i < n; i++){
System.out.print(arr[i] + " ");
}
}
}

import java.util.Scanner;
public class a10{
public static void main(String[]args){
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int n = s.nextInt();
int[] marks = new int[n];
String[] names = new String[n];
System.out.println("Enter the marks:");
for (int i = 0; i < n; i++){
marks[i] = s.nextInt();
}
System.out.println("Enter the names:");
for (int i = 0; i < n; i++){
names[i] = s.next();
}
for (int i = 0; i < n - 1; i++){
for (int j = 0; j < n - i - 1; j++){
if (marks[j] > marks[j + 1]){
int tempMark = marks[j];
marks[j] = marks[j + 1];
marks[j + 1] = tempMark;
String tempName = names[j];
names[j] = names[j + 1];
names[j + 1] = tempName;
}
}
}
System.out.println("Sorted Array:");
for (int i = 0; i < n; i++){
System.out.print(marks[i] + " ");
}
System.out.println();
for (int i = 0; i < n; i++){
System.out.print(names[i] + " ");
}
}
}

You might also like