Friday 18 January 2013

Diamond Pattern in java

import java.util.Scanner;

public class DiamondPattern {
public static void main(String[] args) {
System.out
.print("Enter the number of stars you want in diamond shape: ");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
System.out.println("Diamond of Stars are \n");
for (int i = 1; i <= n; i++) {
for (int j = 0; j < (n - i); j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("*");
for (int k = 1; k < i; k++)
System.out.print("*");
System.out.println();
}

for (int i = n - 1; i >= 1; i--) {
for (int j = 0; j < (n - i); j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("*");
for (int k = 1; k < i; k++)
System.out.print("*");
System.out.println();
}

System.out.println();
}

}

7 comments:

  1. import java.util.Scanner;

    public class DiamondPatternExample {

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter the pattern size odd number ");
    int size = sc.nextInt();
    int half= size/2+1;

    for (int row = 1; row <=size; row++) {
    if(row<=half){
    for(int space = 1; space <=half-row; space++){
    System.out.print(" ");
    }
    for (int star = 1; star <=row; star++) {
    System.out.print("* ");
    }
    System.out.println();
    }else{
    for(int space = row-half; space >=1; space--){
    System.out.print(" ");
    }
    for (int star = size-row; star >= 0; star--) {
    System.out.print("* ");
    }

    System.out.println();
    }
    }
    }

    }

    ReplyDelete
  2. These are nice code but i want these code

    *
    ** **
    *** ***
    ** **
    *


    How to Write this program to Print Star Pattern in Java

    ReplyDelete
    Replies
    1. Really Nice Post to Print Star Pattern in Java Yesterday I visit this blog and i was found lots of helpful information from your side thanks for sharing updated and New Technology related Post.

      Delete
  3. Really Nice Post to Print Star Pattern in Java Yesterday I visit this blog and i was found lots of helpful information from your side thanks for sharing updated and New Technology related Post.

    ReplyDelete
  4. This article regarding Print Star Pattern in Java is common for every java learner and thanks for sharing above codes in java programming.

    * *
    * * * *
    * * * * * * * *
    * * * * * * * * * * * * * * * *

    Please give code for above pattern Print Triangle of Stars in Java

    ReplyDelete
  5. I would like to say that this blog really convinced me to do it! Thanks, very good post. pawn shop in buckeye az

    ReplyDelete