fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12.  
  13. System.out.print("Please enter odd number that should be greater than 2");
  14. Scanner sc = new Scanner(System.in);
  15. int n = sc.nextInt();
  16.  
  17. if(n >= 3 && n % 2 == 1){
  18. print(n);
  19. }else {
  20. System.out.print("Error!, Please enter odd number that should be greater than 2");
  21. }
  22.  
  23. }
  24.  
  25. public static void print(int n){
  26.  
  27. int k = (n+1)/2;
  28.  
  29. for(int i = 1; i<= k;i++){
  30. for(int sp = 1; sp <= k-1-(i-1);sp++){
  31. System.out.print(" ");
  32. }
  33.  
  34. for(int j = 1; j<= 2*i-1; j++){
  35. System.out.print("*");
  36. }
  37. }
  38.  
  39.  
  40. }
  41. }
Success #stdin #stdout 0.11s 54380KB
stdin
3
stdout
Please enter odd number that should be greater than 2 ****