I want to make a program to print a pattern
can anybody please help me providing code?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I want to make a program to print a pattern
can anybody please help me providing code?
This is my pattern to print
*****
****
***
Please do not post duplicate threads, especially ones that hijack the threads of other users. That's extremely rude. How would you like it if somebody now posted their own question as a reply to this thread?
Nobody is going to write the code for you. That's not how this works, at all. What have you tried? Where are you stuck?
Read the link in my signature on asking questions the smart way. Provide an SSCCE that demonstrates where you're stuck, ask a specific technical question, and we'll go from there.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Do a search on the forums. This question has been answered several times.
When you are on a thread, if you scroll down you will see a 'Similar Threads' box which will provide you with more answers.
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
in the above code i can print 1 space in second line. but i want to print 2 stace in 3rd line and 3 space in 4th lineimport 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,k; System.out.print("How many stars you want to print in first row ? "); n=Integer.parseInt(in.readLine()); for (i = 0; i<n; i=i+2) { for (j=i; j<n;j++) System.out.print ("*"); System.out.println(); if(i<n) System.out.print(" "); } } }
output as follows---
How many stars you want to print in first row ? 7
*******
******
****
**
Moved to Java Theory & Questions
arunjib, just to let you know I have deleted many of your other posts which were duplicates and in the incorrect location. As Kevin mentioned, please refrain from doing this and if you have a question, simply start a new thread.
What do you think your code does? There are a few things that simply make no logical sense. For example, why do you have an if statement that checks whether i is less than n? Your for loop already guarantees that that will always evaluate to true.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
I have made a code to print the pattern as
*****
****
***
as follows
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,k; System.out.print("How many stars you want to print in first row ? "); n=Integer.parseInt(in.readLine()); for (i = 0; i<n; i=i+2) { for (j=i; j<n;j++) System.out.print ("*"); System.out.println(); } } }
but in the above code i cannot print 1 leading space in 2nd line and 2 leading space in 3rd line.
Please help me.
I don't see any code that suggests you're attempting to print a space at all.
Hint: Maybe you should write a method that takes two parameters, a character and an int, that prints out the character that number of times.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
sir i want to print spaces using for loop. but i am unable to do that. as i am not strong on nested for loop. may i get a complete help from this site/thread without thinking more? if so i shall be greatful. if not i just want to quit my question as i am upset on it. if i had much knowledge i must not put a beginner level question. i am sorry if i do anything mistake here in this net.
Sorry, but programming requires thinking. You aren't just going to get a full solution. That's not how this works. That's not learning- it's cheating.
If you aren't strong on nested for loops, take my suggestion of writing a method. That way you don't have to deal with nested for loops directly.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Do you know A, B, C, D .... Z ? Can you remember how first time you learnt alphabet ? Was you thinking or reading reapedly ? No ! you just taken help from your teacher (or mom or dad). So as i cannot do this i need just a solution not a teacher like you. I really cannot fine anything to do this. Moreover i am not cheating anybody.
Drop the attitude.
I have told you what to do, in more than one way. If you don't understand something about it, ask. Have you tried to create a method that prints out a character a certain number of times? If not, then do not complain about your inability to follow instructions. If so, post your attempt and ask a specific question about it.
Or think about it this way- how would you do this "by hand"? Pretend you have a friend who has no idea how to draw the pattern you need. What steps would you give him, so that he could follow them and accomplish your goal? Make sure each step is very specific and very short, otherwise your friend will become confused. When you have those steps written out, you'll have an algorithm that should be pretty easy to translate into code.
If I see another negative comment from you, I'm done trying to help you. There are too many other people, who actually want to learn and are willing to follow directions, that I could be helping instead. Nobody is going to simply hand over the code, and if they did, you wouldn't be learning from it anyway.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
It's fine. But you have to understand that we aren't here to simply write code for you- not only is that cheating, but it doesn't help you anyway. Thee process of working through a problem and hitting that "ah-ha!" moment are really important to learn. I don't try to be mean, but we simply cannot give out answers to homework like that.
I'm glad you eventually got it figured out.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Huh? You don't need to ask permission. It's a public forum. Post away. But keep in mind everything I said- ask a specific question, show your work (in SSCCE form), and be ready to actually learn and work.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
arunjib (March 10th, 2011)
thanks.......................