0% found this document useful (0 votes)
25 views11 pages

Subject:: Java Programming

The document contains 6 Java programming assignments: 1) Write a program to sort an array in ascending order. 2) Write a program to check if a given number is present in a 1D array. 3) Write a program to insert an element into a 1D array at a specific index. 4) Write a program to multiply two 2D arrays. 5) Write a program to calculate the transpose of a 2D array. 6) Write a program to display a non-rectangular pattern using a non-rectangular 2D array.

Uploaded by

VAISHALI CHANDEL
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
25 views11 pages

Subject:: Java Programming

The document contains 6 Java programming assignments: 1) Write a program to sort an array in ascending order. 2) Write a program to check if a given number is present in a 1D array. 3) Write a program to insert an element into a 1D array at a specific index. 4) Write a program to multiply two 2D arrays. 5) Write a program to calculate the transpose of a 2D array. 6) Write a program to display a non-rectangular pattern using a non-rectangular 2D array.

Uploaded by

VAISHALI CHANDEL
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

Subject : Java Programming

Assignment : 6
1.Write a program to sort array elements into ascending orders.
import java.util.*;
class Sorting
{
public static void main(String args[])
{
int []a;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements:");
int n=sc.nextInt();
a = new int[n];
System.out.println("Enter the array element:");
for (int i=0; i<a.length; i++)
{
a[i]=sc.nextInt();
}
System.out.println("\nBefore sorting array element are :");
for (int i=0; i<a.length; i++)
{
System.out.print(a[i]+" ");
}
for (int i=0; i<a.length-1; i++)
{
for (int j=i+1; j<a.length; j++)
{
if (a[i]>a[j])
{
int temp;
temp = a[i];
a[i] = a[j];
a[j]=temp;
}
}
}
System.out.println();
System.out.println("\nAfter sorting array element are:");
for (int i =0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
}
}

186150316006 Page no : 1
Subject : Java Programming

Output :

2.Write a program to check whether given no is present in 1D-array or not?

import java.util.*;
class Present
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
int a[],i,n,k,flag=0;
System.out.println("Enter Number of array element");
n = sc.nextInt();
a = new int[n];
System.out.println("Enter array elements :");
{
for(i=0;i<n;i++)
{
a[i] = sc.nextInt();
}
}

186150316006 Page no : 2
Subject : Java Programming
System.out.println("Enter the number which you want to find :");
int key = sc.nextInt();
for(i=0;i<n;i++)
{
if(a[i]==key)
{
flag=1;
break;
}
else
{
flag=0;
}
}
if(flag==1)
{
System.out.println("Number " +key+ " is found at " +(i+1)+"th position ");
}
else
{
System.out.println("Not Find");
}
}
}

Output :

186150316006 Page no : 3
Subject : Java Programming

3.Write a program to insert an element in given 1-D array at specific index.

import java.util.*;
class Insertion
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int a[],i;
System.out.println("Enter Number of element:");
int n = sc.nextInt();
a= new int[n+1];
System.out.println("Enter Array elements:");
for( i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Enter number which you want to insert :");
int x = sc.nextInt();
System.out.println("Enter position which you want to insert :");
int pos = sc.nextInt();
for (i = (n-1); i >= (pos-1); i--)
{
a[i+1] = a[i];
}
a[pos - 1] = x;
System.out.println("After Inserting");
for (i = 0; i <n; i++)
{
System.out.println(a[i]+" ");
}
System.out.print(a[n]);
}
}

Output :

186150316006 Page no : 4
Subject : Java Programming

4.Write a program to find out Matrix multiplication of 2-D array.

import java.util.*;
class Multiplication
{
public static void main(String args[])
{
int [][] a = new int[3][3];
int [][] b = new int[3][3];
int [][] c = new int[3][3];
Scanner sc = new Scanner (System.in);
System.out.println("Enter the elements of matrix A:");
for (int i=0; i<3; i++)
{

186150316006 Page no : 5
Subject : Java Programming
for(int j=0; j<3; j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("\nEnter the elements of matrix B:");
for (int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
b[i][j]=sc.nextInt();
}
}
System.out.println("\nMatrix of A:");
for (int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("\nMatrix of B:");
for (int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}
System.out.println("\nMultiplication of Matrix A and B:");
for ( int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}

186150316006 Page no : 6
Subject : Java Programming
Output :

186150316006 Page no : 7
Subject : Java Programming
5.Write a program to find out Transpose of given 2-D array..

import java.util.*;
class Transpose
{
public static void main(String args[])
{
int a[][] = new int[3][3];
int b[][] = new int[3][3];
Scanner sc = new Scanner (System.in);
System.out.println("Enter the elements of matrix:");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("\nMatrix Before transpose:");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("\nMatrix After Transpose:");
for(int i = 0;i<3;i++)
{
for(int j = 0;j<3;j++)
{
b[i][j] = 0;
for(int k = 0;k<3;k++)
{
b[i][j]=a[j][i];
}
System.out.print(b[i][j]+" ");
}
System.out.println();
}
}
}

Output :

186150316006 Page no : 8
Subject : Java Programming

6.Write a program to display following pattern using the concept of non-rectangular


array.
1
2 3
4 5 6
7 8 9 10
import java.util.*;
class NonRect
{
public static void main(String args[])
{
int[][] a = new int[4][];
a[0] = new int[1];
a[1] = new int[2];
a[2] = new int[3];
a[3] = new int[4];
System.out.println("No of rows:"+a.length);
System.out.println("No of elements in 0th rows:"+a[0].length);
System.out.println("No of elements in 1th rows:"+a[1].length);

186150316006 Page no : 9
Subject : Java Programming
System.out.println("No of elements in 2th rows:"+a[2].length);
System.out.println("No of elements in 3rd rows:"+a[3].length);
Scanner sc = new Scanner (System.in);
for (int i=0; i<4; i++)
{
for(int j=0; j<=i; j++)
{
System.out.println("Enter the elements for "+i+"th row:");
a[i][j]=sc.nextInt();
}
}
System.out.println("\nPrinting Pattern using Non rectangular array:");
for (int i=0; i<4; i++)
{
for(int j=0; j<=i; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}

186150316006 Page no : 10
Subject : Java Programming

186150316006 Page no : 11

You might also like