Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

18BCE2196

Saksham Goyal

Q1

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of elements you want to store: ");
n=sc.nextInt();
int[] array = new int[10];
System.out.println("Enter the elements of the array: ");
for(int i=0; i<n; i++)
{
array[i]=sc.nextInt();
}
System.out.println("Array elements are: ");
for (int i=0; i<n; i++)
{
System.out.println(array[i]);
}
}
}
Q2

public class Main{


public static int linearSearch(int[] arr, int key){
for(int i=0;i<arr.length;i++){
if(arr[i] == key){
return i;
}
}
return -1;
}
public static void main(String a[]){
int[] a1= {10,20,30,50,70,90};
int key = 50;
System.out.println(key+" is found at index: "+linearSearch(a1, key));

}
}
Q3

public class Main {


static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}

}
}

}
public static void main(String[] args) {
int arr[] ={3,60,35,2,45,320,5};

System.out.println("Array Before Bubble Sort");


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

bubbleSort(arr);//sorting array elements using bubble sort

System.out.println("Array After Bubble Sort");


for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}

Q4

class Main
{
static int removeDuplicates(int arr[], int n)
{
if (n==0 || n==1)
return n;

int[] temp = new int[n];

int j = 0;
for (int i=0; i<n-1; i++)
if (arr[i] != arr[i+1])
temp[j++] = arr[i];

temp[j++] = arr[n-1];

for (int i=0; i<j; i++)


arr[i] = temp[i];

return j;
}
public static void main (String[] args)
{
int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5};
int n = arr.length;

n = removeDuplicates(arr, n);

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


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

Q5

public class Main {

static void reverse(int a[], int n)


{
int[] b = new int[n];
int j = n;
for (int i = 0; i < n; i++) {
b[j - 1] = a[i];
j = j - 1;
}

System.out.println("Reversed array is: \n");


for (int k = 0; k < n; k++) {
System.out.println(b[k]);
}
}
public static void main(String[] args)
{
int [] arr = {10, 20, 30, 40, 50};
reverse(arr, arr.length);
}
}

Q6

public class Main


{
public static void main(String[] args) {
int rows, cols;
boolean flag = true;

int a[][] = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
};

rows = a.length;
cols = a[0].length;
if(rows != cols){
System.out.println("Matrix should be a square matrix");
}
else {
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(i == j && a[i][j] != 1){
flag = false;
break;
}
if(i != j && a[i][j] != 0){
flag = false;
break;
}
}
}

if(flag)
System.out.println("Given matrix is an identity matrix");
else
System.out.println("Given matrix is not an identity matrix");
}
}
}

Q7

public class Main{


public static void main(String args[]){
int original[][]={{1,3,4},{2,4,3},{3,4,5}};
int transpose[][]=new int[3][3];

for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
transpose[i][j]=original[j][i];
}
}

System.out.println("Printing Matrix without transpose:");


for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(original[i][j]+" ");
}
System.out.println();
}
System.out.println("Printing Matrix After Transpose:");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(transpose[i][j]+" ");
}
System.out.println();
}
}}

You might also like