Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Pattern printing in Java

printing

  • Please log in to reply
6 replies to this topic

#1 arunjib

arunjib

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 77 posts
  • Programming Language:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript
  • Learning:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript

Posted 10 March 2011 - 06:53 AM

i want to get following out for a java program... how i should start my study?


output of the program:

How many stars you want to print in 1st row ? 7

*******
******
*****
****
*****
******
*******
  • 0

#2 eafkuor

eafkuor

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 214 posts

Posted 10 March 2011 - 07:35 AM

If the user inputs n you will have to do something like this:

print '*' n times;

for(int i=0;i<n-2;i++)
   print a line;

print '*' n more times;

Now, think about that "print a line" statement.
If you need some more help just ask =)
  • 0

#3 arunjib

arunjib

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 77 posts
  • Programming Language:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript
  • Learning:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript

Posted 11 March 2011 - 03:50 AM

i applied the code as follows---
for(i=0;i<n;i++)
System.out.print("*");
for(i=0;i<n-2;i++)
System.out.println();
System.out.print("*");
but output is not come. i need some spaces to print please give me more clue.

If the user inputs n you will have to do something like this:

print '*' n times;

for(int i=0;i<n-2;i++)
   print a line;

print '*' n more times;

Now, think about that "print a line" statement.
If you need some more help just ask =)


  • 0

#4 eafkuor

eafkuor

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 214 posts

Posted 11 March 2011 - 04:17 AM

String y=JOptionPane.showInputDialog("Number: ");

		int n=Integer.parseInt(y);

		

		for(int i=0;i<n;i++)System.out.print("*");

		System.out.println();

		

		int j;

		for(j=1;j<n/2;j++){

			int spaces=j;int middle=n-2*spaces-2; //you put the first '*' after j spaces, the second one after another n-2spaces-2

			for(int k=0;k<spaces;k++)System.out.print(" ");

			System.out.print("*");

			for(int k=0;k<middle;k++)System.out.print(" ");

			System.out.println("*");

		}

		if(n%2!=0){//you print the middle '*' only if n is odd

			for(int k=0;k<j;k++)System.out.print(" ");

			System.out.println("*");

		}

		for(j--;j>0;j--){

			int spaces=j;int middle=n-2*spaces-2;

			for(int k=0;k<spaces;k++)System.out.print(" ");

			System.out.print("*");

			

			for(int k=0;k<middle;k++)System.out.print(" ");

			System.out.println("*");

		}

		

		for(int i=0;i<n;i++)System.out.print("*");

	}

  • 1

#5 arunjib

arunjib

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 77 posts
  • Programming Language:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript
  • Learning:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript

Posted 11 March 2011 - 04:52 AM

Thank you very much for the code. There is only one logical error [if we input even number the shape will not come] in the above code. I just applied the following code
if(n%2==0)
n++; 

String y=JOptionPane.showInputDialog("Number: ");
		int n=Integer.parseInt(y);
		
		for(int i=0;i<n;i++)System.out.print("*");
		System.out.println();
		
		int j;
		for(j=1;j<n/2;j++){
			int spaces=j;int middle=n-2*spaces-2; //you put the first '*' after j spaces, the second one after another n-2spaces-2
			for(int k=0;k<spaces;k++)System.out.print(" ");
			System.out.print("*");
			for(int k=0;k<middle;k++)System.out.print(" ");
			System.out.println("*");
		}
		if(n%2!=0){//you print the middle '*' only if n is odd
			for(int k=0;k<j;k++)System.out.print(" ");
			System.out.println("*");
		}
		for(j--;j>0;j--){
			int spaces=j;int middle=n-2*spaces-2;
			for(int k=0;k<spaces;k++)System.out.print(" ");
			System.out.print("*");
			
			for(int k=0;k<middle;k++)System.out.print(" ");
			System.out.println("*");
		}
		
		for(int i=0;i<n;i++)System.out.print("*");
	}


  • 0

#6 eafkuor

eafkuor

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 214 posts

Posted 11 March 2011 - 05:13 AM

Thank you very much for the code. There is only one logical error [if we input even number the shape will not come] in the above code. I just applied the following code

if(n%2==0)
n++; 


Oh well, you didn't specify the behaviour for even numbers so I just assumed it :D
  • 1

#7 arunjib

arunjib

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 77 posts
  • Programming Language:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript
  • Learning:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript

Posted 11 March 2011 - 05:15 AM

yes that was my fault that i did not specified...

Oh well, you didn't specify the behaviour for even numbers so I just assumed it :D


  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download