Array Concept in Java,Array in Java,What is Array?,Jagged Array,Multidimensional array in java?

78

Array Concept in Java, Array in Java, What is Array? Jagged Array, Multidimensional array in java?

What is an Array:-

It is a collection of elements of similar data type, array store element using proper sequence, base index of the array will be started from 0 to size-1.
The array is also called the primary data structure of Java, Array is used to provide data arrangement using the linear data structure.
Syntax of Array:-
Datatype arrayname[] = new Datatype[size]
Datatype [] arrayname = new Datatype[size]
Datatype arrayname[] = {value1,value2,value,....}
int arr[] = new int[5];
int [] arr = new int[5];
Array basic program to fixed elements size?
1)  Integer Type Array:-
class ArrExample
{
   public static void main(String args[])
   {
       int [] arr = {12,23,34,11,67};
       for(int i=0;i<arr.length;i++)
       {
           System.out.println(arr[i]);
       }  
       }
}
2)  Char Type Array:-
class ArrExample
{
   public static void main(String args[])
   {
       char [] arr = {'1','A','@','$','_'};
       for(int i=0;i<arr.length;i++)
       {
           System.out.println(arr[i]);
       }  
       }
}
User Input 
import java.util.Scanner;
class DynamicArray
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in); 
        System.out.println("Enter size of array");
        int num;
        num = sc.nextInt();
        char arr[] = new char[num];
        for(int i=0;i<num;i++)
        {
            System.out.println("Enter element for "+i+ " index");
            arr[i]=sc.next().charAt(0); 
        }
        System.out.println("Array elements are");
        for(int i=0;i<num;i++)
        {
            System.out.println(arr[i]);
              }
            }
}
3) String type Array
class ArrExample
{
   public static void main(String args[])
   {
       String [] arr = {"C","Java","PHP","iOS","Android"};
       for(int i=0;i<arr.length;i++)
       {
           System.out.println(arr[i]);
       }      
   }
}
With User Input
import java.util.Scanner;
class DynamicArray
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in); 
        System.out.println("Enter size of array");
        int num;
        num = sc.nextInt();
        String arr[] = new String[num];
        for(int i=0;i<num;i++)
        {
            System.out.println("Enter element for "+i+ " index");
            arr[i]=sc.next(); 
        }
        System.out.println("Array elements are");
        for(int i=0;i<num;i++)
        {
            System.out.println(arr[i]);
            }        
    }
}
4)  Object type Array Example in Java:-
class ArrExample
{
   public static void main(String args[])
   {
        Object [] arr = {"C",1,true,12.0,1.2F};
       for(int i=0;i<arr.length;i++)
       {
           System.out.println(arr[i]);
       }  
       }
}
Advantage of the array:-
1) We can easily search and sort the elements of the array because the array stores data using the proper sequence.
2)  Array provides contiguous memory allocation hence we can reduce the extra memory gap.
3)  We can store multiple elements using a single array variable which reduces the extra business code of an application.

The disadvantage of the array:-
1)  Array size is fixed
2)  we can not add, and destroy array elements dynamically.
these limitation has been solved by the Collection Framework.
Type of array:-
1 Single Dimension:-    
We can store elements using rows, the base index of the array element will be started from 0 to size-1.
Datatype arrayname[] = new Datatype[];
Datatype [] arrayname = new Datatype[];
Program of Array:-;
Question WAP to store and display elements into the array?
Answer:-
import java.util.Scanner;
class ArrDemo
{
   public static void main(String args[])
   {
        Scanner sc = new Scanner(System.in);
        int size;
        System.out.println("Enter size of array");
        size=sc.nextInt();
        int arr[] = new int[size];
        for(int i=0;i<size;i++)
        {
          System.out.println("enter element for "+i+" index");
          arr[i]=sc.nextInt();
        }
        for(int i=0;i<size;i++)
        {
          System.out.println(arr[i]);
                 }
   }
WAP to calculate the sum of array elements?
import java.util.Scanner;
class ArrExample
{
    public static void main(String args[])
    {
          int size;
          int sum=0;
          System.out.println("Enter size");
          Scanner sc = new Scanner(System.in);
          size=sc.nextInt();
          int arr[] = new int[size];
          for(int i=0;i<size;i++)
          {
               System.out.println("Enter element for "+i+" index");
               arr[i]=sc.nextInt();
          }
          
          for(int i=0;i<size;i++)
          {
               sum=sum+arr[i];
               System.out.println(arr[i]);
          } 
          System.out.println(sum);
     }
}
ASSIGNMENT:-

1) WAP to sort the elements of the array in ascending order?

class SortArr
{

    public static void main(String args[])
    {
        int [] arr = {12,23,45,10,11,89,21};
        int num;
        for(int i=0;i<arr.length;i++)
        {
            for(int j=i+1;j<arr.length;j++)
            {
                    if(arr[i]>arr[j])
                    {
                       num = arr[i];
                       arr[i]=arr[j];
                       arr[j] = num;
                        
                    }
            }
            System.out.println(arr[i]);   
        } 
    }
}
2)  WAP to find the max and second max element in an array?
3)  WAP to merge two arrays in one array?
4)  WAP to display duplicate elements in an array?
1,1,2,3,4,5,2,7,11
class UniqueArr
{
public static void main(String args[])
{
int arr[] = {1, 2, 1, 2,1, 3, 4, 5, 2, 5, 2};

for(int i=0;i<arr.length-1;i++)
{
   int c=0;
   for(int k=0;k<i;k++)
   {
       if(arr[i]==arr[k])
       {
        c++;
        break;
       } 
   }
   if(c==0)
   {
   for(int j=i+1;j<arr.length;j++)
   {
      if(arr[i]==arr[j] )
      {
          
           System.out.println(arr[i]);
           break;
                  
      } 
    }
   }
   }
}
}
1 2
5) WAP to display unique elements in an array?
2 3 4 5 7 11 11 2 5    o/p   3 4 7
6) WAP to display prime elements in the array?
The solution to this program:-
import java.util.Scanner;
class ArrDemo
{
   public static void main(String args[])
   {
       int size;
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter size");
       size=sc.nextInt();
       int arr[] = new int[size];
       for(int i=0;i<size;i++)
       {
            System.out.println("Enter elements ");
            arr[i]=sc.nextInt();
       }
               for(int i=0;i<size;i++)
       {
            int c=0;
           for(int j=1;j<=arr[i];j++)
           {
                if(arr[i]%j==0)
                  c++;
           } 
          if(c==2)
          System.out.println(arr[i]);
              }
  }
}
Optimize program of Prime Number?
import java.util.Scanner;
class ArrDemo
{
   public static void main(String args[])
   {
       int size,i,j;
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter size");
       size=sc.nextInt();
       int arr[] = new int[size];
       for(i=0;i<size;i++)
       {
            System.out.println("Enter elements ");
           arr[i]=sc.nextInt();
       }       
        for(i=0;i<size;i++)
       {
          for(j=2;j<arr[i]/2;j++)
           {
                if(arr[i]%j==0)
                  break;
           } 
          if(j==(int)(arr[i]/2))
          System.out.println(arr[i]);
            }
  }
}
2)Multi-Dimension:-  
using this array we can store elements using rows and columns, it provides a matrix pattern to display elements.
It will be used to arrange data based on rows and columns, for example, if we have 3 different sections and we want to evaluate the marks of students for each section then we prefer a multidimensional array.
Syntax of MultiDimension Array;-
Datatype arrayname[][] = new Datatype[row][col];
int arr[][] = new int[3][2];
Program Explanation of multidimensional array;-

With user input:-
import java.util.Scanner;
class ArrDemo
{
   public static void main(String args[])
   {
        Scanner sc = new Scanner(System.in);
        int r,c;
        System.out.println("Enter size of row");
        r=sc.nextInt();
        System.out.println("Enter size of column");
        c=sc.nextInt();
        int arr[][] = new int[r][c];
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
             System.out.println("enter element for "+i+j+" index");
             arr[i][j]=sc.nextInt();
            }
        }
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
             System.out.print(arr[i][j]+" ");
              }
           System.out.println();
        }
   }
}
Without user input:-
class Matrix
{
    public static void main(String args[])
    {
           int arr[][] = {{1,2},{3,5}};
           for(int i=0;i<2;i++)
           {
               for(int j=0;j<2;j++)
               {
                  System.out.print(arr[i][j]);
               }
               System.out.println();
          }
   }
}
Program of MultiDimenion Array;-
1)  WAP to perform multiplication of the matrix?
2)  WAP to count the total prime element in the matrix?
3)  WAP to find the max element of each row of the matrix?
The solution of max element for each row in the matrix:-
package oops;
import java.util.Scanner;
public class ArrayExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r,c;
System.out.println("Enter rows");
r = sc.nextInt();
System.out.println("Enter column");
c=sc.nextInt();
int arr[][] = new int[r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.println("Enter element for "+i+j+ "index");
arr[i][j] = sc.nextInt();
}
}
System.out.println("Matrix elements is");
for(int i=0;i<r;i++)
{
int max=0;
for(int j=0;j<c;j++)
{
if(max<arr[i][j])
{
max=arr[i][j];
}
System.out.print(arr[i][j]);
}
System.out.println("maximum elements is "+max);
System.out.println();
}
}
}
Limitation of Multi dimension array:-
The number of row elements and column elements will be equal to the matrix. if we want to store a dissimilar number of elements for each row then a multi-dimension array will be failed the problem will solve by a Jagged Array.
3  Jagged Array:-
Jagged is the collection of heterogeneous rows, we can provide a different number of column elements in the jagged row.
Java provides a Jagged array Structure similar to a multidimensional array but Jagged array contains a different number of column elements for each row but multi-dimension array contains the same number of column elements for each row.
Example  of a jagged array
1
2 3 4
2 3
5 6 7 8
in this example, a Jagged array contains four rows and for the first row one element, the second row three elements, the third row only two elements, and the fourth row contain four elements.
Syntax of Jagged Array:-
Datatype arrayname[][] = new Datatype[row][];
int arr[][] = new int[4][];
arr[0] = new int[1];
arr[1] = new int[3];
arr[2]= new int[2];
arr[3]= new int[4];
Normal Jagged Array Example:-
Example of Jagged Array:-
import java.util.Scanner;
class JaggedArr
{
   public static void main(String args[])
   {
        int r,c;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number of rows");
        r=sc.nextInt();
        int arr[][] = new int[r][];
        for(int i=0;i<r;i++)
        { 
            System.out.println("enter number of column elements for row "+i);
            c=sc.nextInt();
            arr[i]=new int[c];
            for(int j=0;j<c;j++)
            {
                System.out.println("enter element for "+i+j+" index");
                arr[i][j]=sc.nextInt();
            }
        }

        for(int i=0;i<r;i++)
        {         
            for(int j=0;j<arr[i].length;j++)
            {
                System.out.print(arr[i][j]+" ");
               
            }
            System.out.println();
        }
   }
}
4 Object Array:-
The object is the superclass in java hence it can contain all data type elements using proper sequence.
It is the collection of heterogeneous elements using this array we can store dissimilar types of elements 
Object arrayname [] = {value1,value2,value3,....}
Object arr[] = {12,"hello",12.34,true};
class ObjectArr
{
   public static void main(String args[])
   {
     Object arr[] = {1001,"manish","CS",45000};
     for(int i=0;i<arr.length;i++)
     {
        System.out.println(arr[i]);
     }
     for(Object o:arr)
     {
          System.out.println(o);
     }
   }
}
Multidimensional Array:-
class ObjectArr
{
   public static void  main(String args[])
   {
         Object arr[][] = {{true,12,12.34},{12.34F,"Abc",'a'}};
         for(int i=0;i<2;i++)
         {
              for(int j=0;j<3;j++)
              {
                 System.out.print(arr[i][j] + " ");
              }
              System.out.println();
         }  
   }
}
Foreach Loop in Java:-
Note:-  for each is used to display the elements of array and collection only .it is the modified form of for loop.
for(normalvar : arrayvariable)
{
   System.out.println(normalvar);
}
array value will be copied to a variable.
for each loop aways will be used to display the element of array and collection. it is also called modified for a loop.
Example of Foreach Loop:-
class ObjectArr
{
   public static void  main(String args[])
   {
       int arr[] = {1,34,45,56,67,89,11};
       for(int k:arr)
       {
            System.out.println(k);
       } 
         Object arr1[][] = {{true,12,12.34},{12.34F,"Abc",'a'}};
         for(Object k[]:arr1)
         {
              for(Object l:k)
              {
                 System.out.print(l + " ");
              }
              System.out.println();
         } 
   }
}
Array Practice Program?
Q1)  WAP to find max and second max char in String?
     String s = "hello";   output o is the max char and l is the second max
Q2)  WAP to find second max prime elements in the jagged array?
Q3)   WAP to sort only odd elements in an array?
      12 21 34 56 67 89 3 88 7
  o/p    12 3  34 56 7  21 67 88
MOST Important Array Interview Questions for Interview?
Check if a key is present in every segment of size k in an array
Find the minimum and maximum element in an array
Write a program to reverse the array
Write a program to sort the given array
Find the Kth largest and Kth smallest number in an array
Find the occurrence of an integer in the array
Sort the array of 0s, 1s, and 2s
Range and Coefficient of array
Move all the negative elements to one side of the array
Find the Union and Intersection of the two sorted arrays
Level 2
Write a program to cyclically rotate an array by one
Find the missing integer
Count Pairs with given sum
Find duplicates in an array
Sort an Array using the Quicksort algorithm
Find common elements in three sorted arrays
Find the first repeating element in an array of integers
Find the first non-repeating element in a given array of integers
Find the largest three elements in an array Time
Rearrange the array in alternating positive and negative items
Find if there is any subarray with sum equal to zero
Find Largest sum contiguous Subarray
Find the factorial of a large number
Find Maximum Product Subarray
Find longest consecutive subsequence
Find the minimum element in a rotated and sorted array
Find all elements that appear more than N/K times
GCD of given index ranges in an array
Minimize the maximum difference between the heights
Minimum number of jumps to reach the end
Find the two repetitive elements in a given array
Find a triplet that sums to a given value
Construct a N*M matrix from the user input
Find the row with the maximum number of 1’s
Print the matrix in a Spiral manner
Find whether an array is a subset of another array
Implement two Stacks in an array
Majority Element
Wave Array
Trapping Rainwater
Level 3
Maximum Index
Max sum path in two arrays
Find Missing And Repeating
Stock buy and sell Problem
Pair with given sum in a sorted array
Chocolate Distribution Problem
Longest Consecutive Subsequence
Print all possible combinations of r elements in a given array

Tags

Post a Comment

78Comments

POST Answer of Questions and ASK to Doubt

  1. public class UniqueElementInArray {

    public static void main(String[] args) {
    int arr[]= {1,1,2,3,4,4,4,5,6,7,8,8};
    for(int i=0;i<arr.length;i++)
    {
    int c=0;
    for(int j=0;j<arr.length;j++)
    {
    if(arr[i]==arr[j]&&i!=j)
    c++;

    }
    if(c==0)
    System.out.println(arr[i]);
    }

    }

    }

    ReplyDelete
  2. WAP to perform multiplication of the matrix?

    public class MultiMatrix {

    public static void main(String[] args) {
    int a[][]= {{1,1,1},{2,2,2},{3,3,3}};
    int b[][]= {{1,1,1},{2,2,2},{3,3,3}};
    int c[][]=new int[3][3];
    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();


    }

    }
    }

    ReplyDelete
  3. WAP to find the max and second max element in an array?

    public class MaxAndSecondMaxInArray {

    public static void main(String[] args) {
    int nums[]= {4,34,75,3,45,1,100,23};
    int maxOne = 0;
    int maxTwo = 0;
    for(int i=0;i<nums.length; i++)

    {
    if(maxOne<nums[i])
    {
    maxTwo = maxOne;
    maxOne = nums[i];

    }
    else if(maxTwo<nums[i])
    {
    maxTwo = nums[i];
    }
    }
    System.out.println("max number:" + maxOne);
    System.out.println("second max number" + maxTwo);
    }

    }


    ReplyDelete
  4. WAP to sort the elements of the array in ascending order?

    public class AssendingArray {

    public static void main(String[] args) {
    int[] arr=new int[] {5,2,8,7,1};
    int temp=0;
    System.out.println("element of array");
    for(int i=0;iarr[j])
    {
    temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    }
    }

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

    }
    }
    }

    ReplyDelete
  5. import java.util.Scanner;

    class arr
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the size of the array");
    int size=sc.nextInt();
    int arr[]=new int[size];

    for(int i=0; i<size; i++)
    {
    System.out.println("enter the elements for the size"+i);
    arr[i]=sc.nextInt();
    }
    for(int j=0; j<size; j++)
    {
    System.out.println("array is" +arr[j]);
    }
    }
    }

    ReplyDelete
  6. import java.util.Scanner;
    class arr
    {
    public static void main(String[]arg)
    {
    int sum=0;
    Scanner sc=new Scanner(System.in);
    System.out.println("ENTER THE ARRAY NO");
    int size=sc.nextInt();

    int arr[]=new int[size];

    for(int i=0; i<size; i++)
    {
    System.out.println("enter the array no for size=" +i);
    arr[i]=sc.nextInt();

    }
    for(int j=0; j<size; j++)
    {
    System.out.println("array =" +arr[j]);

    sum=sum+arr[j];
    }
    System.out.println("sum of array=" +sum);
    }
    }

    ReplyDelete
  7. /* WAP to find the max and second max element in an array*/
    import java.util.Scanner;
    class SecondMax
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enetr size of array ");
    int size = sc.nextInt();
    int arr[] = new int[size];

    for(int i = 0; i i; i++)
    {
    if(arr[i]>max)
    max = arr[i];
    }
    System.out.println("max = " +max);
    int smallest = arr[0];
    for(int i =0; size> i; i++)
    {
    if(arr[i]<smallest)
    smallest = arr[i];
    }
    System.out.println("smallest = " +smallest);

    int secondmax = smallest;
    for(int i =0; size> i; i++)
    {
    if(arr[i] != max && arr[i]>secondmax)
    secondmax = arr[i];
    }
    System.out.println("secondmax = " +secondmax);
    }
    }

    ReplyDelete
  8. /* WAP to sort the elements of the array in ascending order?*/
    class Asending
    {
    public static void main(String args[])
    {
    int a[] = {63,45,12,16,25,28,48,42};
    int temp;
    for(int i =0; ia[j])
    {
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;

    }
    }

    System.out.println(" " +a[i]);
    }

    }
    }

    ReplyDelete
  9. /* WAP to perform multiplication of the matrix?*/
    import java.util.Scanner;
    class ArrayMultiplication
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    int arr1[][] = new int[3][3];
    int arr2[][] = new int[3][3];
    int mul[][] = new int[3][3];
    System.out.println("Enter element for first matrix");
    for(int i = 0; i<3; i++)
    {
    for(int j =0; j<3; j++)
    {
    arr1[i][j] = sc.nextInt();
    //System.out.println("["+i+"]["+j+"]" );// this line show the position for arr1
    }
    }
    System.out.println("First Matrix");

    for(int i = 0; i<3; i++) // print matrix for arr1
    {
    for(int j =0; j<3; j++)
    {
    System.out.print(arr1[i][j] + " ");
    }
    System.out.println();

    }
    //code for second matrix
    System.out.println("Enter element for Second matrix");

    for(int i = 0; i<3; i++)
    {
    for(int j =0; j<3; j++)
    {
    arr2[i][j] = sc.nextInt();
    //System.out.println("["+i+"]["+j+"]" );// this line show the position for arr2
    }
    }

    System.out.println("Second matrix");
    for(int i = 0; i<3; i++) // print matrix for arr2
    {
    for(int j =0; j<3; j++)
    {
    System.out.print(arr2[i][j] + " ");
    }
    System.out.println();
    }
    // code for addition of two matrix
    for(int i = 0; i<3; i++)
    {
    for(int j =0; j<3; j++)
    {
    mul[i][j] = arr1[i][j]*arr2[i][j];
    }
    }
    System.out.println("Matrix Multiplication = ");
    for(int i = 0; i<3; i++)
    {
    for(int j =0; j<3; j++)
    {
    System.out.print(mul[i][j] + " ");
    }
    System.out.println();
    }
    }
    }


    ReplyDelete
  10. import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args) {
    int size;
    Scanner sc = new Scanner(System.in);
    System.out.println("enter the size ");
    size=sc.nextInt();
    int arr[]=new int[size];
    for(int i=0;i<=size;i++)
    {
    System.out.println("enter the elementof"+i+"index");
    arr[i]=sc.nextInt();

    }
    for(int i=0;i<=size;i++)
    {

    System.out.println(arr[i]);
    }
    }

    ReplyDelete
  11. /******************************************************************************

    Online Java Compiler.
    Code, Compile, Run and Debug java program online.
    Write your code in this editor and press "Run" button to execute it.

    *******************************************************************************/

    import java.util.Scanner;
    class Main
    {

    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    int size,sum=0;
    System.out.println("Enter size of array");
    size=sc.nextInt();
    int arr[] = new int[size];
    for(int i=0;i<size;i++)
    {
    System.out.println("enter element for "+i+" index");
    arr[i]=sc.nextInt();
    }
    for(int i=0;i<size;i++)
    {
    int c=0;
    for(int j=1;j<=arr[i];j++)
    {
    if(arr[i]%j==0)
    c++;

    }
    if(c==2)
    System.out.println(arr[i]);
    }

    }

    }

    ReplyDelete
  12. public class Main
    {
    public static void main(String[] args) {
    int arr[]={2,4,5,1,2,5,64,4};
    for(int i=0;i<arr.length;i++)
    {
    int c=0;
    for(int k=0;k<i;k++){

    if(arr[i]==arr[k]){
    c++;
    }
    }
    for(int j=i+1;j<arr.length;j++)
    {
    if(arr[i]==arr[j]){
    c++;
    if(c==1)
    {
    System.out.print(arr[i]);

    }
    System.out.println();
    }


    }

    }
    }
    }

    ReplyDelete
  13. java.util.Scanner;
    public class Main
    {
    public static void main(String[] args) {
    int r,c;
    Scanner sc=new Scanner (System.in);
    System.out.println("enter the row");
    r=sc.nextInt();
    System.out.println("enter the colume");
    c=sc.nextInt();
    int arr[][]=new int [r][c];
    for(int i=0;i<r;i++)
    {
    for(int j=0;j<c;j++)
    {
    System.out.println("enter the element if"+i+j+"index");
    arr[i][j]=sc.nextInt();
    }

    }

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

    }
    System .out.println(" ");
    }
    }

    }

    ReplyDelete
  14. public class Main
    {
    public static void main(String[] args) {
    int x[][]={{1,1,1},{2,2,2},{3,3,3}};
    int y[][]={{1,1,1},{2,2,2},{3,3,3}};
    int z[][]= new int[3][3];
    for(int i=0;i<3;i++)
    {
    for(int j=0;j<3;j++)
    {
    z[i][j]=0;

    for(int k=0;k<3;k++){
    z[i][j]=x[i][k]*y[k][j];
    }
    System.out.print(z[i][j]+" ");
    }
    System.out.println(" ");
    }
    }
    }

    ReplyDelete
  15. WAP to find the max and second max and third max and fourth element in an array?

    //
    import java.util.Scanner;
    class Array
    {
    public static void main(String args[])
    {
    int max =0;
    int smax =0;
    int tmax=0;
    int fmax = 0;
    Scanner sc = new Scanner(System.in);
    int size;
    System.out.println("Enter size of array");
    size=sc.nextInt();
    int arr[] = new int[size];
    for(int i=0;i<size;i++)
    {
    System.out.println("enter element for "+i+" index");
    arr[i]=sc.nextInt();
    }
    for(int i=0;i<size;i++)
    {
    if(max<arr[i])
    {
    fmax=tmax;
    tmax=smax;
    smax=max;
    max=arr[i];


    }
    else if(smax<arr[i])
    {
    fmax=tmax;
    tmax=smax;
    smax=arr[i];

    }
    else if(tmax<arr[i])
    {
    fmax = tmax;
    tmax=arr[i];
    }

    else if(fmax<arr[i])
    {

    fmax=arr[i];

    }
    }

    System.out.println("max "+max);
    System.out.println("secondmax "+smax);
    System.out.println("thirdmax "+tmax);
    System.out.println("fourthmax "+fmax);

    }
    }

    ReplyDelete
  16. WAP to find the maximum second maximum third maximum and fourth maximum number??


    //
    import java.util.Scanner;
    class Fmax
    {
    public static void main(String[] args)
    {

    int size;
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the size of arrray ");
    size=sc.nextInt();
    int ar[]=new int[size];
    int max=0;
    int secmax=0;
    int tmax=0;
    int fmax=0;
    for(int i=0; i<size; i++)
    {
    System.out.println("enter the elements of array ");
    ar[i]=sc.nextInt();
    }
    for(int i=0; i<size; i++)
    {
    if(max<ar[i])
    {
    fmax=tmax;
    tmax=secmax;
    secmax=max;
    max=ar[i];
    }
    else if(secmax<ar[i])
    { fmax=tmax;
    tmax=secmax;
    secmax=ar[i];

    }
    else if(tmax<ar[i])
    {
    fmax=tmax;
    tmax=ar[i];
    }
    else if(fmax<ar[i])
    {
    fmax=ar[i];
    }
    }
    System.out.println("the maximum number is "+max);
    System.out.println("the second maximum number is "+secmax);
    System.out.println("the third maximum number is "+tmax);
    System.out.println("the fourth maximum number is "+fmax);
    }


    }

    ReplyDelete
  17. //(yash panjre) WAP to calculate sum of Array

    import java.util.Scanner;
    class ArraySum
    {
    public static void main(String []args)
    { Scanner sc=new Scanner(System.in);
    System.out.print("Enter size of Array");
    int size=sc.nextInt();
    int sum=0;
    int arr[]=new int[size];
    for(int i=0;i<size;i++)
    {
    System.out.print("enter at index "+i+"=");
    arr[i]=sc.nextInt();
    sum=sum+arr[i];
    }
    System.out.println("sum of array is "+sum);


    }
    }

    ReplyDelete
  18. import java.util.Scanner;
    class ArraySum
    {
    public static void main(String []args)
    { Scanner sc=new Scanner(System.in);
    System.out.print("Enter size of Array");
    int size=sc.nextInt();
    int sum=0;
    int arr[]=new int[size];
    for(int i=0;i<size;i++)
    {
    System.out.print("enter at index "+i+"=");
    arr[i]=sc.nextInt();
    sum=sum+arr[i];
    }
    System.out.println("sum of array is "+sum);


    }
    }

    ReplyDelete
  19. // WAP to sort the elements of the array in ascending order?

    // ADITYA BAGHEL

    import java.util.*;
    class Array21
    {
    static public void main(String ar[])
    {
    int sum=0;
    Scanner sc=new Scanner(System.in);
    int x=sc.nextInt();
    int array[]=new int[x];

    for(int i=0;iarray[j])
    {
    sum=array[i];
    array[i]=array[j];
    array[j]=sum;
    }
    }
    }
    for(int i=0;i<x;i++)
    {
    System.out.print(array[i]+" ");
    }




    }
    }

    ReplyDelete
  20. // WAP to sort the elements of the array in descending order?

    // ADITYA BAGHEL

    import java.util.*;
    class Array21
    {
    static public void main(String ar[])
    {
    int sum=0;
    Scanner sc=new Scanner(System.in);
    int x=sc.nextInt();
    int array[]=new int[x];

    for(int i=0;i<x;i++)
    {
    array[i]=sc.nextInt();
    }
    for(int i=0;i<x;i++)
    {
    for(int j=i+1;j<x;j++)
    {
    if(array[i]<array[j])
    {
    sum=array[i];
    array[i]=array[j];
    array[j]=sum;
    }
    }
    }
    for(int i=0;i<x;i++)
    {
    System.out.print(array[i]+" ");
    }




    }
    }

    ReplyDelete
  21. WAP to sort the elements of the array in ascending order?
    //Abhishek chauhan

    import java.util.Scanner;

    public class AscendingOrder
    {
    public static void main(String[] args)
    {
    int x, temp;

    Scanner s = new Scanner(System.in);
    System.out.print("Enter no. of elements you want in array:");
    x = s.nextInt();
    int a[] = new int[x];
    System.out.println("Enter all the elements:");
    for (int i = 0; i < x; i++)
    {
    a[i] = s.nextInt();
    }
    for (int i = 0; i < x; i++)
    {
    for (int j = i + 1; j < x; j++)
    {
    if (a[i] > a[j])
    {
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
    }
    }
    }
    System.out.print("Ascending Order:");
    for (int i = 0; i < x - 1; i++)
    {
    System.out.print(a[i] + ",");
    }
    System.out.print(a[x - 1]);
    }
    }

    ReplyDelete
  22. // WAP to merge two arrays in one array?

    // ADITYA BAGHEL

    import java.util.*;
    class Array21
    {
    static public void main(String ar[])
    {
    Scanner sc=new Scanner(System.in);
    int x=sc.nextInt();
    int y=sc.nextInt();

    int array[]=new int[x];
    int array1[]=new int[y];
    int array2[]=new int[10];
    System.out.println("values for array");
    for(int i=0;i<x;i++)
    {
    array[i]=sc.nextInt();
    }


    System.out.println("values for array1");
    for(int i=0;i<y;i++)
    {
    array1[i]=sc.nextInt();
    }

    for(int i=0;i<5;i++)
    {
    array2[i]=array[i];
    array2[i+5]=array1[i];
    }
    for(int i=0;i<10;i++)
    {
    System.out.print(array2[i]+" ");
    }

    }
    }

    ReplyDelete
  23. import java.io.*;

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


    int a[] = { 30, 25, 40 };

    int b[] = { 45, 50, 55, 60, 65 };


    int a1 = a.length;

    int b1 = b.length;


    int c1 = a1 + b1;


    int[] c = new int[c1];



    for (int i = 0; i < a1; i = i + 1) {


    c[i] = a[i];
    }

    for (int i = 0; i < b1; i = i + 1) {



    c[a1 + i] = b[i];
    }



    for (int i = 0; i < c1; i = i + 1) {






    System.out.println(c[i]);
    }
    }
    }

    ReplyDelete
  24. //duplicate element


    //Abhishek singh chauhan


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


    int [] arr = new int [] {1, 2, 3, 4, 2, 7, 8, 8, 3};

    System.out.println("Duplicate elements in given array: ");

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

    ReplyDelete
  25. //WAP to sort the elements of the array in ascending order?
    import java.util.Scanner;
    class SortAccending
    {
    public static void main(String...args)
    { int temp=0;
    Scanner sc=new Scanner(System.in);
    int size=sc.nextInt();
    int arr[]=new int[size];
    for(int i=0;iarr[j])
    {
    temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    }
    }
    }
    for(int i=0;i<size;i++)
    {
    System.out.println(" "+arr[i]);
    }
    }
    }

    ReplyDelete
  26. //WAP to merge two arrays in one array?
    import java.util.Scanner;
    class MergeTwoArray
    {
    public static void main(String...args)
    { Scanner sc=new Scanner(System.in);
    System.out.print("Enter size of 1st array");
    int s1=sc.nextInt();
    System.out.print("Enter size of 2nd array");
    int s2=sc.nextInt();
    int[]arr1 = new int[s1];
    int[]arr2 = new int[s2];
    int[]arr = new int[arr1.length+arr2.length];
    int count = 0;

    for(int i = 0; i < arr1.length; i++) {
    System.out.print("Enter element of 1st array");
    arr1[i]=sc.nextInt();
    arr[i] = arr1[i];
    count++;
    }
    for(int j = 0; j < arr2.length;j++) {
    System.out.print("Enter element of 2nd array");
    arr2[j]=sc.nextInt();
    arr[count++] = arr2[j];
    }
    for(int i = 0;i < arr.length;i++) System.out.print(arr[i]+" ");
    }
    }

    ReplyDelete
  27. //WAP to display duplicate elements in an array?
    import java.util.Scanner;
    class DisplayDuplicateElement
    {
    public static void main(String...args)
    {
    int count=0;
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter size of array=");
    int size=sc.nextInt();
    int arr1[]=new int[size];
    int arr[]=new int[size];
    for(int i=0;i<size;i++)
    arr[i]=sc.nextInt();
    for(int i=0;i<size;i++)
    {
    for(int j=i+1;j<size;j++)
    {
    if(arr[i]==arr[j])
    arr1[count++]=arr[i];
    }
    }
    for(int k=0;k<count;k++)
    System.out.print(arr1[k]+" ");
    System.out.println(" are duplicate numbers ");
    }


    }

    ReplyDelete
  28. //WAP to display duplicate elements in an array?
    import java.util.Scanner;
    class DisplayDuplicateElement
    {
    public static void main(String...args)
    {
    int count=0;
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter size of array=");
    int size=sc.nextInt();
    int arr1[]=new int[size];
    int arr[]=new int[size];
    for(int i=0;i<size;i++)
    arr[i]=sc.nextInt();
    for(int i=0;i<size;i++)
    {
    for(int j=i+1;j<size;j++)
    {
    if(arr[i]==arr[j])
    arr1[count++]=arr[i];
    }
    }
    for(int k=0;k<count;k++)
    System.out.print(arr1[k]+" ");
    System.out.println(" are duplicate numbers ");
    }


    }

    ReplyDelete
  29. pritam
    //1) WAP to sort the elements of the array in ascending order?
    public class Main
    {
    public static void main(String[] args) {
    int a;
    int []arr={12,11,15,18,9};
    for(int i=0;iarr[j])
    {
    a=arr[i];
    arr[i]=arr[j];
    arr[j]=a;
    }

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

    ReplyDelete
  30. pritam
    WAP to display duplicate elements in an array?
    public class Main
    {
    public static void main(String[] args) {
    int a;
    int []arr={1,1,2,3,4,5,5,6,7,8,8};
    for(int i=0;i<arr.length;i++)
    {
    for(int j=i+1;j<arr.length;j++)
    {
    if(arr[i]==arr[j])
    {
    System.out.print(arr[j]);
    }

    }
    }
    }
    }

    ReplyDelete
  31. // write a programe to check that array is Symmetric or Assymetric

    // ADITYA BAGHEL

    import java.util.*;
    class Array21
    {
    static public void main(String ar[])
    {
    int c=0;
    Scanner sc=new Scanner(System.in);
    int x=sc.nextInt();
    int array[]=new int[x];
    int array12[]=new int[x];
    for(int i=0;i<x;i++)
    {
    array[i]=sc.nextInt();
    }
    for(int i=0;i<x;i++)
    {
    array12[i]=sc.nextInt();
    }

    for(int i=0;i<x;i++)
    {

    if(array[i]==array12[i])
    {
    c++;
    }
    else
    {
    c=0;

    }

    }
    if(c==x)
    {
    System.out.println("Symmetric");
    }
    else
    System.out.println("Assymetric");

    }
    }

    ReplyDelete
  32. // Write a programe to merge 4 array into 1 array

    // ADITYA BAGHEL

    import java.util.*;
    class Merge
    {
    static public void main(String ar[])
    {
    Scanner sc=new Scanner(System.in);
    int x=sc.nextInt();

    int c=x+x+x+x;
    int array1[]=new int[x];
    int array2[]=new int[x];
    int array3[]=new int[x];
    int array4[]=new int[x];
    int array5[]=new int[c];
    for(int i=0;i<x;i++)
    {
    array1[i]=sc.nextInt();
    }
    for(int i=0;i<x;i++)
    {
    array2[i]=sc.nextInt();
    }
    for(int i=0;i<x;i++)
    {
    array3[i]=sc.nextInt();
    }
    for(int i=0;i<x;i++)
    {
    array4[i]=sc.nextInt();
    }
    for(int i=0;i<x;i++)
    {
    array5[i]=array1[i];
    array5[i+x]=array2[i];
    array5[i+x+x]=array3[i];
    array5[i+x+x+x]=array4[i];
    }
    for(int i=0;i<c;i++)
    {
    System.out.print(array5[i]+" ");
    }

    }
    }

    ReplyDelete
  33. //WAP to check Array is Symmetric or not.

    import java.util.Scanner;
    class Symmetric
    {
    public static void main(String...args)
    {
    int c=0,i,j;
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the size of Array=");
    int size=sc.nextInt();
    int arr[]=new int [size];
    System.out.println("Enter elements of Array:");
    for( i=0;i<size;i++)
    {
    System.out.print("value at index"+i+"=" );
    arr[i]=sc.nextInt();
    }
    for( i=0;i<arr.length;i++)
    {
    if(arr[i]!=arr[arr.length-i-1]) c++;
    }
    if(c==0) System.out.print("Array is Symmetric");
    else System.out.print("Array is Not Symmetric");

    }
    }

    ReplyDelete
  34. // Write a programe to find array element using binary search algorithm

    // ADITYA BAGHEL


    import java.util.*;
    class Binary
    {
    static public void main(String ar[])
    {

    Scanner sc=new Scanner(System.in);
    int x=sc.nextInt();
    int array[]=new int[x];
    int low=0;
    int high=x-1,mid=0;

    for(int i=0;iy)
    {
    high=mid-1;
    }
    if(low==mid)
    {
    System.out.println(mid);
    break;
    }
    if(array[mid]==y)
    {
    System.out.println(mid);
    break;
    }
    }

    }
    }

    ReplyDelete
  35. //merge 4 array in one array
    import java.util.Scanner;
    class MergeFourArray
    {
    public static void main(String...args)

    { int s1,s2,s3,s4,i,count=0;
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter size of first Array=");
    s1=sc.nextInt();
    System.out.print("Enter size of second Array=");
    s2=sc.nextInt();
    System.out.print("Enter size of third Array=");
    s3=sc.nextInt();
    System.out.print("Enter size of fourth Array=");
    s4=sc.nextInt();
    int arr1[]=new int[s1];
    int arr2[]=new int[s2];
    int arr3[]=new int[s3];
    int arr4[]=new int[s4];
    int arr[]=new int[s1+s2+s3+s4];
    for(i=0;i<s1;i++)
    {
    arr1[i]=sc.nextInt();
    arr[count++]=arr1[i];
    }

    for(i=0;i<s2;i++)
    {
    arr2[i]=sc.nextInt();
    arr[count++]=arr2[i];
    }

    for(i=0;i<s3;i++)
    {
    arr3[i]=sc.nextInt();
    arr[count++]=arr3[i];
    }
    for(i=0;i<s4;i++)
    {
    arr4[i]=sc.nextInt();
    arr[count++]=arr4[i];
    }
    for(i=0;i<count;i++)
    System.out.print(arr[i]+" ");


    }


    }

    ReplyDelete
  36. //Pushpendra Sisodiya


    class SecondElementOfArray
    {
    public static void main(String args[])
    {
    int arr1[]={2,45,67,89,4,309,567,892,1,45};
    int i,j,max1=0,max2=0,max3=0;
    for(i=0;i<arr1.length;i++)
    {
    if(max1<arr1[i])
    {

    max2=max1;
    max1=arr1[i];
    }


    else if(max2<arr1[i]&&arr1[i]<max1)

    {

    max2=arr1[i];
    }


    }

    System.out.println("max1 no is" +max1);
    System.out.println("max2 no is" +max2);
    }
    }

    ReplyDelete

  37. //Pushpendra Sisodiya

    import java.util.Scanner;
    class Sorting
    {
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    int arr1[]={5,7,8,3,1,9,55};
    int i,j,a;
    for(i=0;iarr1[j])
    {
    a=arr1[i];
    arr1[i]=arr1[j];
    arr1[j]=a;
    }

    }

    }
    for(i=0;i<arr1.length;i++)
    {
    System.out.println(arr1[i]);
    }
    }


    }

    ReplyDelete
  38. //Wap to check that the array element is symmetric or not.
    import java.util.Scanner;

    class Array21
    {
    public static void main(String[] args)
    {
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the no. of rows : ");

    int rows = sc.nextInt();

    System.out.println("Enter the no. of columns : ");

    int cols = sc.nextInt();

    int matrix[][] = new int[rows][cols];

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

    for (int i = 0; i < rows; i++)
    {
    for (int j = 0; j < cols; j++)
    {
    matrix[i][j] = sc.nextInt();
    }
    }
    if(rows != cols)
    {
    System.out.println("The given matrix is not a square matrix, so it can't be symmetric.");
    }
    else
    {
    boolean symmetric = true;

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

    if(symmetric)
    {
    System.out.println("The given matrix is symmetric...");
    }
    else
    {
    System.out.println("The given matrix is not symmetric...");
    }
    }

    sc.close();
    }
    }

    ReplyDelete
  39. //WAP to find array element using binary search algorithm
    import java.util.Scanner;
    class BinarySearchExample
    {
    public static void main(String args[])
    {
    int counter, num, item, array[], first, last, middle;
    //To capture user input
    Scanner input = new Scanner(System.in);
    System.out.println("Enter number of elements:");
    num = input.nextInt();


    array = new int[num];

    System.out.println("Enter " + num + " integers");

    for (counter = 0; counter < num; counter++)
    array[counter] = input.nextInt();

    System.out.println("Enter the search value:");
    item = input.nextInt();
    first = 0;
    last = num - 1;
    middle = (first + last)/2;

    while( first <= last )
    {
    if ( array[middle] < item )
    first = middle + 1;
    else if ( array[middle] == item )
    {
    System.out.println(item + " found at location " + (middle + 1) + ".");
    break;
    }
    else
    {
    last = middle - 1;
    }
    middle = (first + last)/2;
    }
    if ( first > last )
    System.out.println(item + " is not found.\n");
    }
    }

    ReplyDelete
  40. //WAP to merge two arrays in one array?
    import java.util.Scanner;
    class MergeArray
    {
    public static void main(String...args)
    { Scanner sc=new Scanner(System.in);
    System.out.print("Enter size of 1st array");
    int a1=sc.nextInt();
    System.out.print("Enter size of 2nd array");
    int a2=sc.nextInt();
    int[]arr1 = new int[a1];
    int[]arr2 = new int[a2];
    int[]arr = new int[arr1.length+arr2.length];
    int count = 0;

    for(int i = 0; i < arr1.length; i++)
    {
    System.out.print("Enter element of 1st array");
    arr1[i]=sc.nextInt();
    arr[i] = arr1[i];
    count++;
    }
    for(int j = 0; j < arr2.length;j++)
    {
    System.out.print("Enter element of 2nd array");
    arr2[j]=sc.nextInt();
    arr[count++] = arr2[j];
    }
    for(int i = 0;i < arr.length;i++)
    System.out.print(arr[i]+" ");
    }
    }

    ReplyDelete
  41. Program to implement matrix multiplication?
    class Matrix
    {
    public static void main(String args[])
    {
    int arr[][] = {{2,3},{4,5}};
    int arr1[][] = {{1,2},{5,6}};
    int arr2[][] = new int[2][2];
    for(int i=0;i<2;i++)
    {
    for(int j=0;j<2;j++)
    {
    int s=0;
    for(int k=0;k<2;k++)
    {
    s = s + arr[i][k]*arr1[k][j];
    }
    arr2[i][j]= s;
    System.out.print(arr2[i][j] + " ");
    }
    System.out.println();

    }



    }



    }

    ReplyDelete
  42. // write a programe of multiplication of matrix with expression

    // ADITYA BAGHEL

    class Demo
    {
    static public void main(String ar[])
    {
    String s1="";
    int array[][]={{1,1},{2,2}};
    int array1[][]={{1,1},{2,2}};
    int array2[][]=new int[2][2];
    for(int i=0;i<2;i++)
    {
    for(int j=0;j<2;j++)
    {

    for(int k=0;k<2;k++)
    {
    array2[i][j]+=array[i][k]*array1[k][j];
    if(k==0)
    {
    s1=s1+array[i][k]+"*"+array1[k][j]+" + ";
    }
    else if(k==1)
    {
    s1=s1+array[i][k]+"*"+array1[k][j]+" = ";
    }
    }
    System.out.println(s1+""+array2[i][j]);
    s1="";
    }
    }
    System.out.println();
    for(int i=0;i<2;i++)
    {
    for(int j=0;j<2;j++)
    {
    System.out.print(array2[i][j]+" ");
    }
    System.out.println();
    }

    }
    }

    ReplyDelete
  43. //AKSHAY JANGDE
    class Arrayasc
    {

    public static void main(String...args)
    {
    int arr[]={5,8,7,12,65,0};


    int temp=0;
    for(int i=0;i arr[j])
    {
    temp=arr[i];
    arr[i] = arr [j];
    arr[j] =temp;
    }
    }


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



    }
    }

    ReplyDelete
  44. Display Prime Elements in Matrix?

    class Demo
    {
    static public void main(String ar[])
    {
    String s1="";
    int arr[][]={{4,3},{2,7}};
    int i,j,k;
    for(i=0;i<2;i++)
    {
    for(j=0;j<2;j++)
    {
    System.out.print(arr[i][j]);
    for(k=2;k<arr[i][j];k++)
    {
    if(arr[i][j]%k==0)
    break;
    }
    if(arr[i][j]==k)
    System.out.print( "Prime is "+ arr[i][j]);

    }
    System.out.println();

    }


    }
    }

    ReplyDelete
  45. //WAP to find the max and second max element in an array?
    class Max2Array
    {
    public static void main(String[] args)
    {
    int arr[]= {5,20,90,3,545,1,300,3};
    int max1 = 0;
    int max2 = 0;
    for(int i=0;i<arr.length; i++)
    {
    if(max1<arr[i])
    {
    max2 = max1;
    max1 = arr[i];

    }
    else if(max2<arr[i])
    {
    max2= arr[i];
    }
    }
    System.out.println("max number is" + max1);
    System.out.println("second max number is" + max2);
    }

    }

    ReplyDelete
  46. //optimize program of unique element in an Array

    class UniqueElement
    {
    public static void main(String[] args)
    {
    int arr[]= {1,1,1,3,4,4,4,5,6,8,8,8};
    for(int i=0;i<arr.length;i++)
    {
    int c=0;
    for(int j=0;j<arr.length;j++)
    {
    if(arr[i]==arr[j]&&i!=j)
    c++;
    }
    if(c==0)
    System.out.println(arr[i]);
    }
    }
    }

    ReplyDelete
  47. //WAP to sort the elements of the array in ascending order?

    class Ascendingorder
    {
    public static void main(String[] args)
    {
    int arr[]={52,7,5,3,1,89,45};
    int i,c=0,j;

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

    }
    }

    ReplyDelete
  48. Find maximum second maximum and third maximum in jagged array ??
    import java.util.Scanner;
    class Multiplemax
    {
    public static void main(String[] args)
    {
    int i,j,col;
    int ar[][]=new int[3][];
    Scanner sc=new Scanner(System.in);
    for(i=0; i<3; i++)
    {
    System.out.println("enter the number of column "+i);
    col=sc.nextInt();
    ar[i]=new int[col];
    for(j=0; jsmax)
    {
    tmax=smax;
    smax=ar[i][j];
    }
    else if(ar[i][j]>tmax)
    {

    tmax=ar[i][j];
    }
    System.out.print(ar[i][j]+" ");
    }
    System.out.println();

    }


    System.out.println("the maximum element is "+max);
    System.out.println("the second maximum element is "+smax);
    System.out.println("the third maximum element is "+tmax);


    }



    }

    ReplyDelete
  49. // WAP to find the max and second max element in an array


    class Question {
    public static void main(String[] args) {
    int i,max1=0,max2=0;
    int a[]= {1,34,54,6,34,3,46,7,1,3,56,7};
    for(i=0;imax1) {
    max2=max1;
    max1=a[i];
    }
    }
    System.out.println("Maximum - "+max1+"\nSecond maximum - "+max2);
    }
    }

    ReplyDelete
  50. // WAP to merge two arrays in one array

    class Question {
    public static void main(String[] args) {
    int i;
    int a[]= {1,34,54,6};
    int b[]= {2,45,7,3};
    int c[]=new int[a.length+b.length];
    for(i=0;i<c.length;i++) {
    if(i<a.length)
    c[i]=a[i];
    else
    c[i]=b[i-a.length];
    System.out.print(c[i]+" ");
    }

    }
    }

    ReplyDelete
  51. // find prime no using jagged array

    class Primejagged
    {

    public static void main(String[] args)
    {
    int arr[][]=new arr[2];

    arr[0] = new int[3];


    arr[1] = new int[2];
    int count = 0;
    for (int i = 0; i < arr.length; i++)
    for (int j = 0; j < arr[i].length; j++)
    arr[i][j] = count++;


    System.out.println(" Jagged Array");

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

    ReplyDelete
  52. //find max,second max and third max element in jagged row without using sorting.
    import java.util.Scanner;

    class Jaggedmax
    {
    public static void main(String args[])
    {
    int r,c=0;
    Scanner sc=new Scanner(System.in);
    System.out.println("enter no of rows");
    r=sc.nextInt();
    int arr[][]=new int[r][];
    for(int i=0;i<r;i++)
    {
    System.out.println("enter no of column elements for row"+i);
    c=sc.nextInt();
    arr[i]=new int[c];
    for(int j=0;j<c;j++)
    {
    System.out.println("enter element for"+i+j+"index");
    arr[i][j]=sc.nextInt();
    }
    }
    for(int i=0;i<r;i++)
    {
    int max=0,max1=0,max2=0;
    for(int j=0;j<arr[i].length;j++)
    {
    if(max<arr[i][j])
    {
    max2=max1;
    max1=max;
    max=arr[i][j];
    }
    else if(max1<arr[i][j])
    {
    max2=max1;
    max1=arr[i][j];
    }
    else if(max2<arr[i][j])
    {
    max2=arr[i][j];
    }
    }
    System.out.println("maximum element is"+max +","+max1 +"," +max2);System.out.println();
    }

    }
    }

    ReplyDelete
  53. pritam
    program to find maximum second maximum and third maximum in 2d array
    import java.util.Scanner;

    public class Main

    {


    public static void main(String[] args)
    {

    int r,c,i,j,f=0,s=0,t=0;

    Scanner sc= new Scanner(System.in);

    r=sc.nextInt();

    c=sc.nextInt();

    int a[][] = new int[r][c];

    for(i=0;if)

    {

    t=s;

    s=f;

    f=a[i][j];

    }

    else if(a[i][j]>s)

    {

    t=s;

    s=a[i][j];

    }

    else if(a[i][j]>t)

    {

    t=a[i][j];

    }


    }

    }

    ystem.out.println(f+" "+s+" "+t);

    }

    }

    ReplyDelete
  54. // programe to find maximum, second maximum and third maximum

    //ADITYA BAGHEL

    import java.util.*;
    class Jagged
    {
    static public void main(String ar[])
    {
    Scanner sc=new Scanner(System.in);
    int r,c,i,j,k,max=0,Smax=0,Tmax=0;
    System.out.println("Enter no.of rows");
    r=sc.nextInt();
    int array[][]=new int[r][];
    for(i=0;imax)
    {
    Tmax=Smax;
    Smax=max;
    max=array[i][j];
    }
    else if(Smax<array[i][j])
    { Tmax=Smax;
    Smax=array[i][j];
    }
    else if(Tmax<array[i][j])
    {
    Tmax=array[i][j];
    }


    }
    System.out.print("This max,Smax,Tmax is for row "+ i +" "+"Maximum No is= " + max +","+"Second Maximum no is= "+ Smax +","+" Third Max No is= "+Tmax);
    System.out.println();


    }




    }
    }

    ReplyDelete
  55. Khushboo Sendre

    import java.util.Scanner;
    public class Array1
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    int size,i;
    System.out.println("Enter the size of array:");
    size = sc.nextInt();
    int arr[] = new int[size];

    for(i=0;i<size;i++)
    {
    System.out.println("Enter the element: "+i+"index");
    arr[i] = sc.nextInt();
    }
    for(i=0;i<size;i++)
    {
    System.out.println(arr[i]);
    }
    }
    }

    ReplyDelete
  56. // WAP to perform multiplication of the matrix

    class Question {
    public static void main(String[] args) {
    int i,k,j,temp;
    int a[][]= {{2,2,2},{2,2,2},{3,3,3}};
    int b[][]= {{1,1,1},{1,1,1},{1,1,1}};
    int c[][]=new int[3][3];
    for(i=0;i<a.length;i++) {
    for(j=0;j<a.length;j++) {
    temp=0;
    for(k=0;k<a.length;k++) {
    temp=temp+a[i][k]*b[k][i];
    }
    c[i][j]=temp;
    System.out.print(c[i][j]+" ");
    }System.out.println();
    }

    }
    }

    ReplyDelete
  57. WAP to calculate the sum of array elements?

    khushboo sendre

    import java.util.Scanner;
    public class ArraySum
    {
    public static void main(String args[])
    {
    int i,size,sum=0;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the size of array:");
    size = sc.nextInt();
    int arr[] = new int[size];
    for(i=0;i<size;i++)
    {
    System.out.println("Enter element of array" +i+ "index");
    arr[i] = sc.nextInt();
    }
    for(i=0;i<size;i++)
    {
    sum= sum+arr[i];
    System.out.println(arr[i]);
    }
    System.out.println(sum);
    }
    }

    ReplyDelete
  58. //wap to display prime element in jagged row?

    import java.util.Scanner;
    class jaggedprime
    {
    public static void main(String args[])
    {
    int r,c=0,i,j,k;
    Scanner sc=new Scanner(System.in);
    System.out.println("enter no of rows");
    r=sc.nextInt();
    int arr[][]=new int[r][];
    for(i=0;i<r;i++)
    {
    System.out.println("enter no of column elements for row"+i);
    c=sc.nextInt();
    arr[i]=new int[c];
    for(j=0;j<c;j++)
    {
    System.out.println("enter element for"+i+j+"index");
    arr[i][j]=sc.nextInt();
    }
    }
    for( i=0;i<r;i++)
    {
    for(j=0;j<arr[i].length;j++)
    {
    System.out.print(arr[i][j]+" ");
    }
    for(k=2;k<j;k++)
    {
    if(arr[i][j]%k==0)
    break;
    }
    if(arr[i][j]==k)
    System.out.print("prime is"+arr[i][j]);
    }
    System.out.println();
    }
    }

    ReplyDelete
  59. // WAP to count the total prime element in the matrix?

    class Question {
    public static void main(String[] args) {
    int i,j,k,count,prime=0,temp;
    int a[][]= {{2,4,2},{2,6,2},{3,3,3}};
    for(i=0;i<3;i++) {
    for(j=0;j<3;j++) {
    count=0;
    temp=a[i][j];
    for(k=1;k<=a[i][j];k++) {
    if(a[i][j]%k==0) {
    count++;
    }

    }
    if(count==2)
    prime++;
    }
    }
    System.out.print("prime elements in array - "+prime);
    }
    }

    ReplyDelete
  60. // WAP to count the total prime element in the matrix?

    class Question {
    public static void main(String[] args) {
    int i,j,k,count,prime=0;
    int a[][]= {{2,4,2},{2,6,2},{3,3,3}};
    for(i=0;i<3;i++) {
    for(j=0;j<3;j++) {
    count=0;
    for(k=1;k<=a[i][j];k++) {
    if(a[i][j]%k==0)
    count++;
    }
    if(count==2)
    prime++;
    }
    }
    System.out.print("prime elements in array - "+prime);
    }
    }

    ReplyDelete
  61. // WAP to find the max element of each row of the matrix?

    class Question {
    public static void main(String[] args) {
    int i,j,k,count,max;
    int a[][]= {{2,4,2},{2,6,2},{3,3,3}};
    for(i=0;i<3;i++) {
    max=0;
    for(j=0;j<3;j++) {
    if(max<a[i][j])
    max=a[i][j];
    }
    System.out.println("Maximum elements in row "+i+" is - "+max);
    }

    }
    }

    ReplyDelete
  62. //WAP to sort array in ascending order
    class Sort
    {
    public static void main(String[] args)
    {

    int[] arr = new int[] {2,4,6,8,10,5,7};
    System.out.println("Array elements after sorting:");

    for (int i = 0; i < arr.length; i++)
    {
    for (int j = i + 1; j < arr.length; j++)
    {
    int tmp = 0;
    if (arr[i] > arr[j])
    {
    tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
    }
    }

    System.out.println(arr[i]);
    }
    }
    }

    ReplyDelete
  63. //WAP to merge two arrays in one array
    class MergeTwoArr {
    public static void main(String[] args)
    {


    int a[] = { 30, 25, 40 };

    int b[] = { 45, 50, 55, 60, 65 };


    int a1 = a.length;

    int b1 = b.length;


    int c1 = a1 + b1;


    int[] c = new int[c1];


    for (int i = 0; i < a1; i = i + 1) {

    c[i] = a[i];
    }


    for (int i = 0; i < b1; i = i + 1) {


    c[a1 + i] = b[i];
    }


    for (int i = 0; i < c1; i = i + 1) {


    System.out.println(c[i]);
    }
    }
    }

    ReplyDelete
  64. WAP to sort the elements of the array in ascending order.

    Khushboo Sendre

    import java.util.Scanner;
    public class Sortarray
    {
    public static void main(String args[])
    {
    int temp=0, size;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the size of element of array:");
    size = sc.nextInt();
    int arr[] = new int[size];
    System.out.println("Enter the element");
    for(int i=0;iarr[j])
    {
    temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    }
    }
    }
    System.out.println("Sorting is:");
    for(int i=0;i<size-1;i++)
    {
    System.out.println(arr[i] + " ");
    }
    System.out.println(arr[size-1]);
    }
    }

    ReplyDelete
  65. class ArrExample
    {
    public static void main(String args[])
    {

    int arr[] = {1,13,11,45,67,89};
    int arr1[] = new int[arr.length];
    boolean c=true;
    for(int i=0,j=arr.length-1,k=5;i<arr.length;i++,j--,k++)
    {
    arr1[i]= arr[j];
    if(arr1[i]!=arr[i])
    {
    c=false;
    }

    }

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

    }
    if(c)
    {
    System.out.println("Array is equall");
    }
    else
    {
    System.out.println("Array is not equall");
    }






    }



    }

    ReplyDelete
  66. Array Example:-
    import java.util.Scanner;
    class ArrayExample
    {
    public static void main(String args[])
    {

    Scanner sc = new Scanner(System.in);
    int arr[] = new int[5];
    for(int i=0;i<arr.length;i++)
    {
    System.out.println("Enter element for "+i+ "index");
    arr[i]=sc.nextInt();
    }

    int esum=0,osum=0,c1=0,c2=0;
    for(int i=0;i<arr.length;i++)
    {
    if(arr[i]%2==0)
    {
    c1++;
    esum = esum +arr[i];
    }
    else
    {
    c2++;
    osum = osum+ arr[i];
    }
    }
    int o[] = new int[c2];
    int e[] = new int[c1];
    int x=0,y=0;
    for(int i=0;i<arr.length;i++)
    {
    if(arr[i]%2==0)
    {
    e[x]=arr[i];
    x++;

    }
    else
    {
    o[y]=arr[i];
    y++;
    }
    }

    for(int i=0;i<e.length;i++)
    {
    System.out.println(e[i]);
    }
    System.out.println("Sum of even are "+esum);
    for(int i=0;i<o.length;i++)
    {
    System.out.println(o[i]);
    }
    System.out.println("Sum of odd are"+osum);


    }


    }

    ReplyDelete
  67. //Find the Maximum Element of Array
    public class Array {
    public static void main(String[] args) {
    int [] arr={2,5,3,7,6,8,4,3,9,1,23};
    int temp=arr[0];
    for(int i=1;itemp)
    {
    temp=arr[i];

    }
    }System.out.println("Maximum element of Array is:"+temp);

    }
    }

    ReplyDelete
  68. //Find the Minimum Element of Array
    public class Array {
    public static void main(String[] args) {
    int [] arr={2,5,3,7,6,8,4,3,9,1,23};
    int temp=arr[0];
    for(int i=1;i<arr.length;i++)
    {
    if(arr[i]<temp)
    {
    temp=arr[i];

    }
    }System.out.println("Minimum element of Array is:"+temp);

    }
    }

    ReplyDelete
  69. Find the minimum and maximum element in an array?

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

    int []a ={3,5,7,8,9,5};
    int max=0;
    int min=a[0];
    for(int i=0;ia[i])
    {
    min=a[i];
    }
    }

    System.out.println("max element is "+max);
    System.out.println("max element is "+min);

    }
    }

    ReplyDelete

  70. Write a program to reverse the array?

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

    int a[] ={1,2,3,4,5,6};

    for(int i=0;i<6;i++)
    {
    System.out.println(a[i]+" ");
    }
    for(int j=6-1;j>=0;j--)
    {
    System.out.print(a[j]+" ");
    }
    }
    }

    ReplyDelete
  71. import java.util.Scanner;
    class SortAsc {
    public static void main(String[] args) {

    Scanner sc=new Scanner(System.in);
    int temp = 0;
    System.out.println("enter size of array");
    int size=sc.nextInt();
    int arr[]=new int[size];
    System.out.println("Enter the Element");

    for (int i = 0; i < arr.length; i++)
    {
    System.out.print( "element of "+ i);
    arr[i]=sc.nextInt();
    }
    for (int i = 0; i < arr.length; i++) {
    for (int j = i+1; j < arr.length; j++) {
    if(arr[i] > arr[j]) {
    temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    }
    }
    }

    System.out.println();
    System.out.println("Elements of array sorted in ascending order: ");
    for (int i = 0; i < arr.length; i++) {
    System.out.print(arr[i] + " ");
    }
    }
    }

    ReplyDelete
  72. import java.util.*;
    import java.util.Scanner;

    public class SecondHighestNumber {

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter array size :");
    int Elements = sc.nextInt();
    int arr[] = new int[Elements];
    System.out.println("Enter array elements :");
    for (int i = 0; i < arr.length; i++) {
    arr[i] = sc.nextInt();
    }
    System.out.println("Array elements are" + Arrays.toString(arr));
    int largest = arr[0];
    int secondLargest = arr[0];
    for (int i = 0; i < arr.length; i++) {
    if (arr[i] > largest) {
    secondLargest = largest;
    largest = arr[i];
    } else if (arr[i] > secondLargest) {
    secondLargest = arr[i];
    }
    }
    System.out.println("Second highest element in array is :" + secondLargest);
    sc.close();
    }
    }

    ReplyDelete
  73. class MergeTwoArr
    {
    public static void main(String args[])
    {
    int[] a={10,20,30};
    int[] b={40,50,60,70,80,90};

    int a_leng=a.length;
    int b_leng=b.length;
    int c_leng= a_leng+b_leng;

    int[] c=new int[c_leng];

    for(int i=0;i<a_leng;i++)
    {
    c[i]=a[i];
    }
    for(int i=0;i<b_leng;i++)
    {
    c[a_leng+i]=b[i];
    }
    for(int i=0;i<c_leng;i++)
    {
    System.out.println(c[i]);
    }
    }
    }

    ReplyDelete
  74. Sandeep kelwa

    class DuplicateElements
    {
    public static void main(String args[])
    {
    int arr[] = {1,2,3,4,4,3,5,5,8};
    System.out.println("Duplicate elements in given array:");
    for(int i=0; i<arr.length; i++)
    {
    for(int j=i+1; j<arr.length; j++)
    {
    if(arr[i]==arr[j])
    System.out.println(arr[j]);
    }
    }
    }
    }

    ReplyDelete
  75. Program of Jagged Array:-
    import java.util.Scanner;
    class ArrayWrapper
    {
    int k;
    public static void main(String args[])
    {

    Scanner sc = new Scanner(System.in);
    int r,c;
    System.out.println("Enter row");
    r = sc.nextInt();
    System.out.println("Enter column");
    c = sc.nextInt();
    Integer arr[][] = null;
    arr= new Integer[r][];

    for(int i=0;i<r;i++)
    {
    System.out.println("Enter column elements");
    int col = sc.nextInt();
    arr[i] = new Integer[col];
    for(int j=0;j<col;j++)
    {
    System.out.println("Enter array item");

    arr[i][j]=sc.nextInt();;

    }

    }

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

    for(Integer x[]:arr)
    {
    for(Integer y:x)
    {
    System.out.print(y);
    }
    System.out.println();
    }
    }

    }

    ReplyDelete
  76. WAP to reverse String Elements:-

    class StringReverse
    {
    public static void main(String args[])
    {
    String arr[] = {"ram","manish","kapil","jaykumar"};
    for(int i=0;i=0;j--)
    {
    d = d + s.charAt(j);
    }
    arr[i]=d;

    }

    for(String data:arr)
    {
    System.out.println(data);
    }

    }


    }

    ReplyDelete
  77. class UniqueNumber {
    public static void main(String args[]){
    int [] arr = {2, 3, 4, 5 ,7, 11, 11, 2, 5 };

    for (int i= 0; i < arr.length; i++){
    int count1 = 0;
    for (int j= 0; j< arr.length; j++){
    if(arr[i] == arr[j] ) {

    count1++;
    }
    }
    if(count1 == 1 ){
    System.out.print( arr[i] +" ");
    }
    }

    }
    }

    ReplyDelete
  78. SANTOSH KUMAR PAL

    ReplyDelete
Post a Comment