Thursday, January 22, 2015

Star Pattern in Java


Problem:

In the program the following star pattern will be printed.




The program is shown below :


Shape (a):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class Exercice2a
 
{
 public static void main (String[] args)
 {
  for (int count =0; count < 10; count++)
  {
    for (int j=0; j < count+1; j++)
       System.out.print("*");
    System.out.println();
  }
 }
}


Shape (b):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class Exercice2b
 
{
 public static void main (String[] args)
 {
  for (int count =11; count >= 0; count--)
  {
    for (int j=0; j < count-1; j++)
       System.out.print("*");
    System.out.println();
  }
 }
}


Shape (c):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Exercice2c
 
{
 public static void main (String[] args)
 {
        for(int count = 0; count < 10; count++)
        {
            for(int index=1; index < count+1; index++)
                System.out.print(" ");
 
            for(int star=10; star > count; star--)
                System.out.print("*");
            System.out.println();
        }
    }
}


Shape (d):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Exercice2d
 
 {
 public static void main (String[] args)
  {
        for(int count = 10; count > 0; count--)
        {
            for(int index=0; index < count-1; index++)
                System.out.print(" ");
             
            for(int star=10; star > count-1; star--)
                System.out.print("*");
             
            System.out.println();
        }
 }


Output :



No comments:

Post a Comment