.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> OOPS >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Java program to print 0,1 in diamond shape.

Posted By:laptop charles       Posted Date: July 17, 2010    Points: 10    Category: OOPS    URL: http://www.dotnetspark.com  

To print the 0,1 in diamond shape.
 

Using a class and then pass the value "7".
Print the diamond shape in center of the console.
It has used constructor then pass the value and used for loop
then print the 0's and 1's in following output.
class charles
{
	int n;
charles(int n)
{
this.n=n;
}
	void display()
                       {
		int a=40;
                                System.out.print(" ");		
                        for(int i=1;i<=n;i++)
		           {
			    for(int k=0;k=1;i--)
		{
			for(int k=a+2;k>=1;k--)			
	                                      {		
				System.out.print(" ");			
			}
			a=a+1;
			if((i%2)!=0)
			{
				for(int s=1;s<=i;s++)
				{
                               	System.out.print(s%2+" ");
				}
			}
	        System.out.print("\n ");
		}
	} 
}

public class diam
{
public static void main(String arg[])
{
charles n=new charles(7);
n.display();
}
}


/*output 
	    1
	1   0   1
     1  0   1   0   1
   1 0  1   0   1   0 1
     1  0   1   0   1
        1   0   1
            1*/

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend