Jump to content

Pattern printing in Java

- - - - -

  • Please log in to reply
6 replies to this topic

#1
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
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

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

#2
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
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 =)

#3
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
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.

eafkuor said:

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 =)


#4
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
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("*");

	}


#5
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
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++; 


eafkuor said:

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("*");

	}


#6
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts

arunjib said:

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

#7
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
yes that was my fault that i did not specified...

eafkuor said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users