hpilo

Chapter5_Arrays_Ex12

Dec 15th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /*
  4.     =====================================================
  5.                    chapter 5: Arrays
  6.  
  7.       Ex12: Star Graph
  8.     =====================================================
  9. */
  10. public class MyProgram {
  11.     public static void main(String[] args) {
  12.  
  13.         //variables
  14.         final int SIZE=4;
  15.         int arr[]=new int[SIZE];
  16.         int max=0;  //represent max value in array
  17.  
  18.         Scanner s=new Scanner(System.in);
  19.         System.out.println("Enter "+SIZE+" numbers: ");
  20.        
  21.         //user input → init array, init max value
  22.         for(int i=0;i<arr.length;i++){
  23.             arr[i]=s.nextInt();
  24.             if(arr[i]>max)
  25.                 max=arr[i];
  26.         }
  27.        
  28.         //display Star Graph
  29.         for(int row=0;row<max;row++){
  30.             for(int col=0;col<arr.length;col++) {
  31.                 if (max - arr[col] > row)
  32.                     System.out.print("   ");
  33.                 else
  34.                     System.out.print(" * ");
  35.             }
  36.             System.out.println();
  37.         }
  38.  
  39.         //aesthetic gap
  40.         System.out.println("------------");
  41.  
  42.         //display array
  43.         for(int i=0;i<arr.length;i++){
  44.             System.out.print(" "+arr[i]+" ");
  45.         }
  46.  
  47.  
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment