944,122 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1058
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 7th, 2009
0

help..

Expand Post »
hello

Can u help me ?


write an java program that outputs the following pattern. The program shuold prompt the user an odd number of lines (limit 1 to 11) .

For example, if the user prompt the number of the lines is 9, then the output should be:


+
++
+++
++++
+++++
++++
+++
++
+
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alreem is offline Offline
24 posts
since Nov 2009
Nov 7th, 2009
1
Re: help..
Hi alreem,

We can only help you if you show that you have made some sort of an attempt. Do you have an algorithm or some code that you have tried? We may be able to point you in the right direction if we can see what you are thinking...
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Nov 8th, 2009
0
Re: help..
I tried this one, but there is some thing wrong >!

package javaapplication3;
import java.util.Scanner;
public class main {


public static void main(String[] args) {
Scanner read= new Scanner(System.in);

int count,i,number;
System.out.print("Enter the number: ");
number= read.nextInt();
if (number%2==0)
System.out.println(" Plz enter an odd number");
else
System.out.println(" The number is odd ");

count=1;
while (count<= number)
{
i=1;
while(i<=count)
{
System.out.print(count);
i=i+1;
}
System.out.println();
count=count+1;
}

count=(number-1);
while (count<=(number-1))
{
i=1;
while(i<=count)
{
System.out.print(count);
i=i+1;
}
System.out.println();
count=count-1;

}

}

}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alreem is offline Offline
24 posts
since Nov 2009
Nov 8th, 2009
0
Re: help..
Hi alreem,
please next time use the [code] tags to enclose your code,

Java Syntax (Toggle Plain Text)
  1. public static void main(String[] args) {
  2. Scanner read= new Scanner(System.in);
  3.  
  4. int count,i,number;
  5. System.out.print("Enter the number: ");
  6. number= read.nextInt();
  7. if (number%2==0){
  8. System.out.println(" Plz enter an odd number");
  9. // here you should read the number again until it's odd or
  10. // use System.exit() to exit the program
  11. }
  12. else
  13. System.out.println(" The number is odd ");
  14.  
  15. count=1;
  16. //while (count<= number){
  17. while (count<= (number/2)+1){ // loop until you get to the middle number
  18. i=1;
  19. while(i<=count){
  20. //System.out.print(count);
  21. System.out.print("+");
  22. i=i+1;
  23. }
  24. System.out.println();
  25. count=count+1;
  26. }
  27.  
  28. //count=(number-1); // this will go into an infinite loop
  29. //while (count<=(number-1)){ //since the counter will be always
  30. // smaller then (number-1)
  31. count = (number/2); // now starting from the middle
  32. while (count > 0){ // go backward
  33. i=1;
  34. while(i<=count){
  35. // System.out.print(count);
  36. System.out.print("+");
  37. i=i+1;
  38. }
  39. System.out.println();
  40. count=count-1;
  41. }
  42. }

anyway I hope I didn't just served the code to you and that you will understand what's modified

GL
Reputation Points: 10
Solved Threads: 9
Junior Poster in Training
AndreiDMS is offline Offline
68 posts
since Oct 2009
Nov 8th, 2009
0
Re: help..
hi ,

I understand it ,, but if I want the pattern like this
...............+
.............+++
..........+++++
........+++++++
......... +++++
............+++
..............+
hoW i will do it ...?
WITHOUT THE DOTS.


Thanks So Much 4 helpinG ..
=)
Last edited by alreem; Nov 8th, 2009 at 4:25 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alreem is offline Offline
24 posts
since Nov 2009
Nov 9th, 2009
0
Re: help..
>>?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alreem is offline Offline
24 posts
since Nov 2009
Nov 9th, 2009
0
Re: help..
You should find a formula for computing the withe spaces for each line and add it to the code.
Something like:
  • for the longest line you need 7 "+" signs
  • then for the first line you need 1 "+" sign and 3 spaces in the right side
  • for the second line you need 3 "+" signs and 2 spaces
  • for the third line you need 5 "+" signs and 1 space
    so we have ...
    space_characters = (longest_line + 1) / 2 - line_number;
    plus_characters = line_number *2 - 1;
of course in your code, inside while loops you will use:
Java Syntax (Toggle Plain Text)
  1. int space_characters = (number + 1) / 2 - count;
  2. int plus_characters = count * 2 -1;
  3. for (int k = 0; k < space_characters; k++)
  4. System.out.println(" ");
  5. for (int k = 0; k < plus_characters; k++)
  6. System.out.println("+");
or something like that ...
though you might not get the same pattern on screen because the width of the " " sign might be smaller than the one of the "+"
Reputation Points: 10
Solved Threads: 9
Junior Poster in Training
AndreiDMS is offline Offline
68 posts
since Oct 2009
Nov 12th, 2009
0
Re: help..
emm

how I do it ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alreem is offline Offline
24 posts
since Nov 2009
Nov 13th, 2009
0
Re: help..
Plz , help me ...


I did not know how to do it . . ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alreem is offline Offline
24 posts
since Nov 2009
Nov 13th, 2009
0
Re: help..
Try this:
Java Syntax (Toggle Plain Text)
  1. public static void main(String[] args) {
  2. Scanner read= new Scanner(System.in);
  3.  
  4. int count,i,number;
  5. System.out.print("Enter the number: ");
  6. number= read.nextInt();
  7. if (number%2==0){
  8. System.out.println(" Plz enter an odd number");
  9. // here you should read the number again until it's odd or
  10. // use System.exit() to exit the program
  11. }
  12. else
  13. System.out.println(" The number is odd ");
  14.  
  15. count=1;
  16. //while (count<= number){
  17. while (count<= (number/2)+1){ // loop until you get to the middle number
  18. int space_characters = (number + 1) / 2 - count;
  19. int plus_characters = count * 2 -1;
  20. for (int k = 0; k < space_characters; k++)
  21. System.out.print(" ");
  22. for (int k = 0; k < plus_characters; k++)
  23. System.out.print("+");
  24. }
  25. System.out.println();
  26. count=count+1;
  27. }
  28.  
  29. //count=(number-1); // this will go into an infinite loop
  30. //while (count<=(number-1)){ //since the counter will be always
  31. // smaller then (number-1)
  32. count = (number/2); // now starting from the middle
  33. while (count > 0){ // go backward
  34. int space_characters = (number + 1) / 2 - count;
  35. int plus_characters = count * 2 -1;
  36. for (int k = 0; k < space_characters; k++)
  37. System.out.print(" ");
  38. for (int k = 0; k < plus_characters; k++)
  39. System.out.print("+");
  40. }
  41. System.out.println();
  42. count=count-1;
  43. }
  44. }
Reputation Points: 10
Solved Threads: 9
Junior Poster in Training
AndreiDMS is offline Offline
68 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Strange compiler bug
Next Thread in Java Forum Timeline: Can I write into a jar?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC