Showing posts with label for. Show all posts
Showing posts with label for. Show all posts

How to find common elements between two arrays in java?

Question:
How to find common elements in two different arrays?

Code:
public class CommonElements {
 
  public static void main(String a[]){
    int[] arr1 = {2,4,6,8};
    int[] arr2 = {1,2,3,4,5,6,7,8,9};
 
    for(int i=0;i<arr1.length;i++){
      for(int j=0;j<arr2.length;j++){
        if(arr1[i]==arr2[j]){
          System.out.println(arr1[i]);
        }
      }
    }
 
  }
}

Output:
$ java CommonElements
2
4
6
8

How to print a hollow triangle in java?

Question:
How to print a hollow triangle in java?

Code:
public class HollowTriangle {
  public static void main(String[] args) {
 
      int height = 10;  
 
      for ( int i=1 ; i<=height ; i++ ) {
         for ( int j=1 ; j <= i ; j++ ) {
            if(i==1 || i==2 || i==height || j==1 | j==i)
              System.out.print("*");
            else 
              System.out.print(" ");
         }
         System.out.println();
      }
   }
}
 

Output:
$ java HollowTriangle 
*
**
* *
*  *
*   *
*    *
*     *
*      *
*       *
**********

Hollow triangle

Question:
How do i prints hollow triangles using asterisks in java?

Code:
public class HollowTriangle {
  public static void main(String[] args) {
 
    int width = 10;
 
    for (int i = 0; i < width; i++) {
      for (int j = 0; j < width; j++) {
        if (i == 0)
          System.out.print("*");
        else if(j == i)
          System.out.print("*");
        else if(j == (width-1))
          System.out.print("*");
        else
          System.out.print(" ");
      }
      System.out.println("");
    }
  }
}

Output:
$ java HollowTriangle 
**********
 *       *
  *      *
   *     *
    *    *
     *   *
      *  *
       * *
        **
         *

Java code that will produces 1 4 9 16 25 36 49 64 81 100

Question:
Need code (a for loop in java) that will produce the output 1 4 9 16 25 36 49 64 81 100

Code:
public class Loop { 
 
  public static void main(String[] args) { 
 
    for(int i=1; i<=10; i++){
      System.out.print(i*i + " " ); 
    }
    System.out.println(""); 
  }
}
 

Output:
$ java Loop
1 4 9 16 25 36 49 64 81 100 

Finding the lowest and highest numbers in a 2D array?

Question:
How do you find the lowest and highest numbers in a 2D array?

Answer:
You can use a nested for loop.

Code:
import java.util.*;
 
public class LowestNumber {
   public static void main(String[] args) {
      int[][] array = new int[5][2];
 
      // populate array
      Random r = new Random();
      for(int i=0;i<array.length;i++){
         for(int j=0;j<array[i].length;j++){
            array[i][j] = r.nextInt(1000);
         }
      }
      System.out.println("Contents: " + Arrays.deepToString(array));
 
      // find smallest and largest numbers
      int small = array[0][0];
      int large = 0;
      for(int i=0;i<array.length;i++){
         for(int j=0;j<array[i].length;j++){
            if(array[i][j] <= small)
               small = array[i][j];
            if(array[i][j] >= large)
               large = array[i][j];
         }
      }
      System.out.println("Smallest number: " + small);
      System.out.println("Largest number: " + large);
 
   }
}

Output:
$ java LowestNumber
Contents: [[658, 462], [594, 729], [412, 18], [575, 66], [224, 666]]
Smallest number: 18
Largest number: 729