I want to make a program to print a pattern
can anybody please help me providing code?
Printable View
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.
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.
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 lineCode :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(); 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.
I have made a code to print the pattern as
*****
****
***
as follows
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,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.
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.
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.
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.
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.
thanks.......................