0% found this document useful (0 votes)
81 views37 pages

Alpha Series

The document describes source code for drawing star patterns A through G using Java. It includes: 1. Source code for classes Astar through Gstar that take user input for number of rows/columns and use loops and conditional statements to print "*" and " " characters to draw the star pattern. 2. For each class/pattern there is a description of the logic and conditions used to determine where to print "*" vs " ". 3. The main method creates an instance of the class and calls the draw method to display the output for the user-defined size. The document provides detailed source code explanations for programming different star patterns (A-G) in Java by taking input size and using loops and

Uploaded by

ArunRam1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views37 pages

Alpha Series

The document describes source code for drawing star patterns A through G using Java. It includes: 1. Source code for classes Astar through Gstar that take user input for number of rows/columns and use loops and conditional statements to print "*" and " " characters to draw the star pattern. 2. For each class/pattern there is a description of the logic and conditions used to determine where to print "*" vs " ". 3. The main method creates an instance of the class and calls the draw method to display the output for the user-defined size. The document provides detailed source code explanations for programming different star patterns (A-G) in Java by taking input size and using loops and

Uploaded by

ArunRam1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

| 7/8 IT R September 13, 2014 |10-2:14:49 PM |

4-7/8 B.TECH IT-R Page 1


I. ALPHA SERIES PROGRAMMING USING JAVA
1. A STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Astar {
DataInputStream ds=new DataInputStream(System.in);
int n;
public void drawA()throws Exception
{
System.out.print("Enter the number: (e.g.1-10) : ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for 1st column & last column and 1st row
if((j==0)||(j==n-1)||(i==0))
System.out.print("*");
// If Input number is odd
else if((n%2==1)&&(i==n/2))
{
if((j==0)||(j==n-1))
continue;
System.out.print("*");
}
// Else If Input number is even
else if((n%2==0)&&(i==(n/2)-1))
{
if((j==0)||(j==n-1))
continue;
System.out.print("*");
}
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Astar().drawA();
}
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 2
}

(B) OUTPUT


2. B STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Bstar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawB()throws Exception
{
System.out.print("Enter the number: (e.g.1,2,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for 1st column & last column and 1st row & last row
if((j==0)||(j==n-1)||(i==0)||(i==n-1))
System.out.print("*");
// If Input number is odd
else if((n%2==1)&&(i==n/2))
{
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 3
if((j==0)||(j==n-1))
continue;
System.out.print("*");
}
// Else If Input number is even
else if((n%2==0)&&(i==(n/2)-1))
{
if((j==0)||(j==n-1))
continue;
System.out.print("*");
}
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception{
new Bstar().drawB();
}
}

(B) OUTPUT



| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 4
3. C STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Cstar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawC()throws Exception
{
System.out.print("Enter the number: (e.g.1,10,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for 1st column, 1st row & last row else fill empty spaces for all
if((j==0)||(i==0)||(i==n-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Cstar().drawC();
}
}















| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 5
(B) OUTPUT


4. D STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Dstar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawD()throws Exception
{
System.out.print("Enter the number: (e.g.1,10,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for 1st column & last column, 1st row & last row, else fill empty spaces for all
if((j==0)||(j==n-1)||(i==0)||(i==n-1))
System.out.print("*");
else
System.out.print(" ");
}
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 6
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Dstar().drawD();
}
}

(B) OUTPUT












| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 7
5. E STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Estar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawE()throws Exception
{
System.out.print("Enter the number: (e.g.1,10,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for 1st column and 1st row & last row
if((j==0)||(i==0)||(i==n-1))
System.out.print("*");
// If Input number is odd
else if((n%2==1)&&(i==n/2))
{
if((j==0))
continue;
System.out.print("*");
}
// Else If Input number is even
else if((n%2==0)&&(i==(n/2)-1))
{
if((j==0))
continue;
System.out.print("*");
}
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Estar().drawE();
}

}
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 8
(B) OUTPUT


6. F STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Fstar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawF()throws Exception
{
System.out.print("Enter the number: (e.g.1,10,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for 1st column and 1st row
if((j==0)||(i==0))
System.out.print("*");

// If Input number is odd
else if((n%2==1)&&(i==n/2))
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 9
{
if((j==0))
continue;
System.out.print("*");
}
// Else If Input number is even
else if((n%2==0)&&(i==(n/2)-1))
{
if((j==0))
continue;
System.out.print("*");
}
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
new Fstar().drawF();
}
}

(B) OUTPUT



| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 10
7. G STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Gstar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawG()throws Exception
{
System.out.print("Enter the number: (e.g.1,5,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for 1st column, 1st row, last row
if((j==0)||(i==0)||(i==n-1))
System.out.print("*");
// Case (i) Draw Middle Row: If input number is odd
else if((n%2==1)&&(i==n/2))
{
if(j<i)
System.out.print(" ");
else
System.out.print("*");
}
// Case (ii) Draw Middle Row: Else If input number is even
else if((n%2==0)&&(i==(n/2)-1))
{
/*if((j<(n/2)-1)||(j==0))
continue;
*/
if(j<i)
System.out.print(" ");
else
System.out.print("*");
}
// Case (i) Draw Middle Column: If input number is odd
else if((n%2==1)&&(j==n-1))
{
if((i<n/2))
continue;
System.out.print("*");
}
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 11
// Case (ii) Draw Middle Column: If input number is even
else if((n%2==0)&&(j==n-1))
{
if((i<(n/2)-1))
continue;
System.out.print("*");
}

else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Gstar().drawG();
}

}

























| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 12
(B) OUTPUT


8. H STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Hstar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawH()throws Exception
{
System.out.print("Enter the number: (e.g.1,9,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 13
{
for(int j=0;j<n;j++)
{
// draw * for 1st column & last column
if((j==0)||(j==n-1))
System.out.print("*");
// If Input number is odd
else if((n%2==1)&&(i==n/2))
{
if((j==0)||(j==n-1))
continue;
System.out.print("*");
}
// Else If Input number is even
else if((n%2==0)&&(i==(n/2)-1))
{
if((j==0)||(j==n-1))
continue;
System.out.print("*");
}
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Hstar().drawH();
}
}














| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 14
(B) OUTPUT


9. I STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Istar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawI()throws Exception
{
System.out.print("Enter the number: (e.g.1,13,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for 1st row & last row else fill blank spaces for all
if((i==0)||(i==n-1))
System.out.print("*");
// draw middle column
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 15
// case (i): If number is odd, find mid point then draw *
else if((n%2==1)&&(j==n/2))
System.out.print("*");
// case (i): If number is even, find mid point then draw *
else if((n%2==0)&&(j==(n/2)-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Istar().drawI();
}
}

(B) OUTPUT


| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 16
10. J STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Jstar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawJ()throws Exception
{
System.out.print("Enter the number: (e.g.1,13,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for 1st row
if((i==0))
System.out.print("*");
// draw middle column
// case (i): If number is odd, find mid point then draw *
else if((n%2==1)&&(j==n/2))
System.out.print("*");
// case (i): If number is even, find mid point then draw *
else if((n%2==0)&&(j==(n/2)-1))
System.out.print("*");
// draw mid part of last row
// if number is odd
else if((n%2==1)&&(i==n-1))
{
if(j>n/2)
continue;
System.out.print("*");
}
// if number is even
else if((n%2==0)&&(i==n-1))
{
if(j>(n/2)-1)
continue;
System.out.print("*");
}
else
System.out.print(" ");
}
System.out.println();
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 17
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Jstar().drawJ();
}
}

(B) OUTPUT


11. L STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Lstar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawL()throws Exception
{
System.out.print("Enter the number: (e.g.1,10,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 18
// draw * for 1st column, last row else fill blank spaces for all
if((j==0)||(i==n-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Lstar().drawL();
}
}

(B) OUTPUT







| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 19
12. M STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Mstar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawM()throws Exception
{
System.out.print("Enter the number: (e.g.1,10,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for 1st column, last column and 1st row
if((j==0)||(j==n-1)||(i==0))
System.out.print("*");
// Draw Middle Column
// If Input number is odd
else if((n%2==1)&&(j==n/2))
System.out.print("*");
// Else If Input number is even
else if((n%2==0)&&(j==(n/2)-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Mstar().drawM();
}
}








| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 20
(B) OUTPUT


13. N STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Nstar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawN()throws Exception
{
System.out.print("Enter the number: (e.g.1,10,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for 1st column, last column
if((j==0)||(j==n-1))
System.out.print("*");
// Draw Mid part of 1st row
else if(i==0)
{
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 21
// If Input number is odd
if((n%2==1))
{
if(j>n/2)
System.out.print(" ");
else
System.out.print("*");
}
// If Input number is even
else if((n%2==0))
{
if((j>(n/2)-1))
System.out.print(" ");
else
System.out.print("*");
}
}
// Draw mid part of last row
else if(i==n-1)
{
// If Input number is odd
if((n%2==1))
{
if(j<n/2)
System.out.print(" ");
else
System.out.print("*");
}
// If Input number is even
else if((n%2==0))
{
if((j<(n/2)-1))
System.out.print(" ");
else
System.out.print("*");
}
}
// Draw Middle Column
// If Input number is odd
else if((n%2==1)&&(j==n/2))
System.out.print("*");
// Else If Input number is even
else if((n%2==0)&&(j==(n/2)-1))
System.out.print("*");
else
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 22
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Nstar().drawN();
}
}

(B) OUTPUT







| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 23
14. O STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Ostar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawO()throws Exception
{
System.out.print("Enter the number: (e.g.1,4,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for 1st column & last column, 1st row & last row, else fill empty spaces for all
if((j==0)||(j==n-1)||(i==0)||(i==n-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Ostar().drawO();
}
}















| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 24
(B) OUTPUT


15. P STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Pstar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawP()throws Exception
{
System.out.print("Enter the number: (e.g.1,10,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for 1st column
if((j==0)||(i==0))
System.out.print("*");
// draw middle row
// if number is odd
else if((n%2==1)&&(i==n/2))
System.out.print("*");
// if number is even
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 25
else if((n%2==0)&&(i==(n/2)-1))
System.out.print("*");
// draw 1st half of last column
else if(j==n-1)
{
// if number is odd
if((n%2==1)&&(i<n/2))
System.out.print("*");
// if number is even
else if((n%2==0)&&(i<(n/2)-1))
System.out.print("*");
}
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Pstar().drawP();
}
}






















| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 26
(B) OUTPUT


16. S STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Sstar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawS()throws Exception
{
System.out.print("Enter the number: (e.g.1,15,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for 1st row & last row
if((i==0)||(i==n-1))
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 27
System.out.print("*");
// draw middle row
// if number is odd
else if((n%2==1)&&(i==n/2))
System.out.print("*");
// if number is even
else if((n%2==0)&&(i==(n/2)-1))
System.out.print("*");
// draw 1st half of 1st column
else if(j==0)
{
// if number is odd
if(n%2==1)
{
if(i<(n/2))
System.out.print("*");
else
System.out.print(" ");
}
// if number is even
else if((n%2==0))
{ if(i<(n/2)-1)
System.out.print("*");
else
System.out.print(" ");
}
}
// draw 2nd half(lower part) of last column
else if(j==n-1)
{
// Case (i) Draw Middle Column: If input number is odd
if((n%2==1)&&(i>n/2))
System.out.print("*");
// Case (ii) Draw Middle Column: If input number is even
else if((n%2==0)&&(i>(n/2)-1))
System.out.print("*");
}
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 28
new Sstar().drawS();
}
}

(B) OUTPUT


17. T STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Tstar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawT()throws Exception
{
System.out.print("Enter the number: (e.g.1,13,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 29
for(int j=0;j<n;j++)
{
// draw * for 1st row else fill blank spaces for all
if(i==0)
System.out.print("*");
// draw middle column
// case (i): If number is odd, find mid point then draw *
else if((n%2==1)&&(j==n/2))
System.out.print("*");
// case (i): If number is even, find mid point then draw *
else if((n%2==0)&&(j==(n/2)-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Tstar().drawT();
}
}






















| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 30
(B) OUTPUT


18. U STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Ustar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawU()throws Exception
{
System.out.print("Enter the number: (e.g.1,5,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 31
{
// draw * for 1st column & last column, last row,, else fill blank spaces for all
if((j==0)||(j==n-1)||(i==n-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Ustar().drawU();
}
}

(B) OUTPUT








| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 32
19. W STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Wstar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawW()throws Exception
{
System.out.print("Enter the number: (e.g.1,10,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for 1st column, last column and last row
if((j==0)||(j==n-1)||(i==n-1))
System.out.print("*");
// Draw Middle Column
// If Input number is odd
else if((n%2==1)&&(j==n/2))
System.out.print("*");
// Else If Input number is even
else if((n%2==0)&&(j==(n/2)-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Wstar().drawW();
}
}












| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 33
(B) OUTPUT


20. X STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Xstar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawX()throws Exception
{
System.out.print("Enter the number: (e.g.1,13,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for forward diagonal
if(i==j)
System.out.print("*");
// draw * for backward diagonal
else if(j==(n-1)-i)
System.out.print("*");
else
System.out.print(" ");
}
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 34
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Xstar().drawX();
}
}

(B) OUTPUT











| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 35
21. Y STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Ystar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawY()throws Exception
{
System.out.print("Enter the number: (e.g.1,13,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for forward diagonal
// if input number is odd
if(n%2==1)
{
if((i==j)&&(i<n/2))
System.out.print("*");
}
else if(n%2==0)
{
if((i==j)&&(i<(n/2)-1))
System.out.print("*");
}
// draw * for backward diagonal
if(j==(n-1)-i)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Ystar().drawY();
}
}








| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 36
(B) OUTPUT


22. Z STARS
(A) SOURCE CODE
package Alpha;
import java.io.*;
public class Zstar {
DataInputStream ds=new DataInputStream(System.in);
private int n;
public void drawZ()throws Exception
{
System.out.print("Enter the number: (e.g.1,13,etc): ");
n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// draw * for 1st row & last row
if((i==0)||(i==n-1))
System.out.print("*");
// draw * for backward diagonal
else if((j==(n-1)-i))
{
if((i==0)||(i==n-1))
continue;
System.out.print("*");
| 7/8 IT R September 13, 2014 |10-2:14:49 PM |
4-7/8 B.TECH IT-R Page 37
}
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)throws Exception {
// Object Creation
new Zstar().drawZ();
}
}

(B) OUTPUT

You might also like