Sunday, May 16, 2021

Print Half Pyramid in Java

 In this tutorial, I will show you have to create a print pyramid using the Java programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com





Program Listing

/* Half_Pyramid.java
 * Mr. Jake Rodriguez Pomperada,MAED-IT, MIT
 * www.jakerpomperada.com www.jakerpomperada.blogspot.com
 * jakerpomperada@gmail.com
 * Bacolod City, Negros Occidental Philippines
 */

import java.util.Scanner;

public class Half_Pyramid {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int number = 0;
Scanner input = new Scanner(System.in);

    boolean valid;
    
do {
    System.out.println();
    System.out.print("\tPrint Half Pyramid in Java\n\n");
    System.out.print("Enter Number: ");
     // read the number
   number = input.nextInt();
   valid = (number >= 2   && number <= 10);
               
       if (!valid) {
       
        System.out.println("\nValues less than 2 or more then 10 are invalid input. Try Again. ");
         } 
       else {
       // outer loop for row
          for (int i=1; i <= number; i++) {
             // inner loop for column
             for(int j=1; j <= i; j++) {
                // print star
                System.out.print("* ");
             }
             // new line
             System.out.println();
          }
   }
   }while (!valid);
   System.out.println();
  System.out.println("End of Program");
//close the reader
  input.close();
}

}


No comments:

Post a Comment