i want to print the following pattern, please help me
*******
******
*****
****
Printable View
i want to print the following pattern, please help me
*******
******
*****
****
What do you have so far? What are your current ideas?
I just tried the program as below
the output is ---Code:import java.io.*;
public class pattern1
{
public static void main (String[] args) throws IOException
{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
int i,j,n=0;
System.out.print("How many stars you want to print in first row ? ");
n=Integer.parseInt(in.readLine());
for (i = 0; i<n; i++)
{
for (j=i; j<n;j++)
System.out.print ("*");
System.out.println();
}
}
}
How many stars you want to print in first row ? 5
*****
****
***
**
*
......... i just cannot put space left side or can say that i cannot centred the outputs
Your code above is unreadable. Please edit your post above so that your code is indented and readable and place the tag [code] above your code block and the tag [/code] below your code block so that the forum software will maintain the formatting.
Try defining a static helper to print a single line.
after designing the code think of a way to work it in and perform what you want.Code:public String printRow(int n)
Do you know what a static method is? Create one to do the printing of a single line.
You can use this method and a loop to solve this problem.Code:printRow(5)
// produces *****
Also look into string formatting
You can answer that: Are all your programs written fully left justified -- all lines start in the 0th column?
Anyway, regarding the logic, you want to System.out.print(" ") in a for loop before your starts, the number of which will depend on some simple math. Try and play with the numbers and you'll figure it out. Half the fun is in the discovery. Luck!
I told you what to search for, I'd rather not give too much away, check out the tutorials and API. See if you can find something that does what you want. By the way, this isn't the only way to handle this problem, just one.
In the posted solution, tell me how many leading spaces are on each line? What variable changes, and which doesn't? Any way you can do math to figure something out?
Take some time and think about how to do this, it is trivial for us, but since it's a challenge for you it is a great learning experience. try to work on small pieces of the puzzle and build up to the solution.
Think a bit and don't guess; if you have to print n rows, the first row (row #0) contains 2*n-1 stars, and no leading space; the last line (row #n-1) contains 1 star and n-1 spaces. Line k prints 2*(n-k)-1 stars and k spaces. Use the following little method to print your stars:
kind regards,Code:private static void printRow(char c, int n) {
for (int i= 1; i <= n; i++)
System.out.print(c);
}
Jos
Don't feel shy. We want to help you we are just trying to stay somewhat vague so you have to put in more work. It's hard to grasp this stuff when you are new but if you step back and think about it, it helps a lot. This isn't too challenging button are still a beginner so it should present a challenge. My method was to use string format to produce the answer, try googling string.format. Or use jos's more mathy approach. Either works, just sit back and think.