Array in Java

  PROGRAME TO PRINT AN ARRAY USING JAVA 

package com.company;
import java.util.Scanner;
public class array {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.println("enter the values in array:");
int [] array =new int [5];
for(int i=0;i<5;i++)
{
array[i]=s.nextInt();
}
System.out.println("the data stored in the array is:");
for(int i=0;i<5;i++)
{
System.out.println(array[i]);
}

}
}

PROGRAM TO PRINT A 2D ARRAY

package com.company;
import java.util.Scanner;
public class array2d {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);

String [][] roll=new String [3][3];
int j;
for(int i=0; i<3;i++)
{

System.out.println("enter the name, class and mobile no. of student");
for( j=0; j<3;j++)
{
roll[i][j]=s.next();
}
}



for(int i=0; i<3;i++)
{
System.out.printf("roll no. of the student is: %d",i+1);
System.out.println("\n name, class and mobile no. of student");
for( j=0; j<3;j++)
{
System.out.println(roll[i][j]);
}
}

}
}

MATRIX IN ARRAY USING JAVA

package com.company;
import java.util.Scanner;
public class matrix {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
int [][] arg=new int [2][3] ;
System.out.println("enter the value in the array");
for(int i=0;i<2; i++)
{
for(int j=0;j<3; j++)
{
arg[i][j]= s.nextInt();

}
}
for(int i=0;i<2; i++)
{
System.out.println("\n");
for(int j=0;j<3; j++)
{
System.out.print("\t");
System.out.print(arg[i][j]);

}
}

}
}

PROGRAME TO CHECK IF AN NUMBER IS PRESENT IN THE ARRAY OR NOT

package com.company;
import java.util.Scanner;
public class checkIFthevariableispresentINarray {
public static void main(String[] args) {
Scanner s= new Scanner (System.in);
int[] ary = {6, 43, 78, 89, 34, 3, 8, 9, 3, 57, 9, 0, 4};
int n=s.nextInt();
int a = ary.length;
for (int i = 0; i < a; i++) {
if (n== ary[i]) {
System.out.println("the number is present in the array");
break;

}
else if (n!= ary[i]) {
System.out.println("the number is not present in the array");
break;
}

}

}
}

Program to find the largest and smallest number in the array

package com.company;

public class largestNUMBERinarray {
public static void main(String[] args) {
int []s={1,3,3,24,1,676,3,56,87,676,34,344};
int b=s[0];
for(int i=0;i<s.length-1;i++)
{
if (b<s[i+1])
{
b=s[i+1];
}
}
System.out.printf("largest number: %d",b);
for(int i=0;i<s.length-1;i++)
{
if (b>s[i+1])
{
b=s[i+1];
}
}
System.out.printf("\n smallest number: %d",b);
}

}

PROGRAME TO REVERSE AN ARRAY USING JAVA

package com.company;

public class ArrrayINreverse {
public static void main(String[] args) {

int[] a = {1, 2, 3, 4, 5, 6, 7};
int b = a.length;
int t=b;
int s = 0;
for (int i = 0; i < b; i++) {
s=a[i] ;
a[i] = a[b-1];
a[b-1] = s;
b--;
}
for (int i = 0; i <t ; i++) {
System.out.println(a[i]);

}

}
}

Program to sort an array in java


public class sortANarray {
public static void main(String[] args) {
int []s={32,465,98798,321,74,85,29,7,54};
for(int j=0;j<s.length-1;j++) {
for (int i = 0; i < s.length - 1; i++) {
if (s[i] > s[i + 1]) {
int b = s[i];
s[i] = s[i + 1];
s[i + 1] = b;

}
}
}
for (int j : s) {
System.out.println(j);

}
}

}

PROGRAM TO CHECK IF AN ARRAY IS SORTED OR NOT

package com.company;

public class checkIFanARRAYsorted {
public static void main(String[] args) {
int []s={1,2,3,4,5,6};
int b=0;
int c=0;
for(int i=0;i<s.length-1;i++)
{
if (s[i]<=s[i+1])
{
b=b+1;
}
else if(s[i+1]<s[i])
{
c=c+1;
}
b=b+1;
c=c+1;


}
int d=s.length;
d=d-1;
if(d==b || d==c)
{
System.out.println("the given array is sorted");
}
else {
System.out.println("the given array is not sorted");
}
}
}

PROGRAM TO ADD THE ELEMENTS OF ARRAY  USING JAVA

package com.company;

public class addElementsinarray {
public static void main(String[] args) {
float [] add = {3.5f,4.8f,5.7f,6.1f,7.9f,2.3f};
float s= 0.0F;
for (int i = 0, addLength = add.length; i < addLength; i++) {
float k = add[i];
s = s + k;

}
System.out.println(s);
}
}
PROGRAME TO ADD TWO MATRIX USING JAVA LANGUAGE
package com.company;

import java.util.Scanner;

public class addtwo_matrix {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
int [][] arg1=new int [2][3] ;
int [][] arg2=new int [2][3] ;
int [][] arg3=new int [2][3] ;
System.out.println("enter the value in the array1");
for(int i=0;i<2; i++)
{
for(int j=0;j<3; j++)
{
arg1[i][j]= s.nextInt();

}
}
System.out.println("enter the value in the array2");
for(int i=0;i<2; i++)
{
for(int j=0;j<3; j++)
{
arg2[i][j]= s.nextInt();

}
}




for(int i=0;i<2; i++)
{
System.out.println("\n");
for(int j=0;j<3; j++)
{
arg3[i][j]=arg1[i][j]+arg2[i][j];
System.out.print("\t");
System.out.print(arg3[i][j]);

}
}
}
}

No comments:

Post a Comment

    Hello, I'm Vishal Rana  I'm 19 years old  I live in Himachal Pradesh and  currently I'm studying in college for my UG degree...