help..

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2009
Posts: 17
Reputation: alreem is an unknown quantity at this point 
Solved Threads: 0
alreem alreem is offline Offline
Newbie Poster

help..

 
0
  #1
25 Days Ago
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:


+
++
+++
++++
+++++
++++
+++
++
+
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 794
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster
 
1
  #2
25 Days Ago
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...
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 17
Reputation: alreem is an unknown quantity at this point 
Solved Threads: 0
alreem alreem is offline Offline
Newbie Poster
 
0
  #3
24 Days Ago
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;

}

}

}
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 20
Reputation: AndreiDMS is an unknown quantity at this point 
Solved Threads: 0
AndreiDMS AndreiDMS is offline Offline
Newbie Poster
 
0
  #4
24 Days Ago
Hi alreem,
please next time use the [code] tags to enclose your code,

  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 17
Reputation: alreem is an unknown quantity at this point 
Solved Threads: 0
alreem alreem is offline Offline
Newbie Poster
 
0
  #5
24 Days Ago
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; 24 Days Ago at 4:25 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 17
Reputation: alreem is an unknown quantity at this point 
Solved Threads: 0
alreem alreem is offline Offline
Newbie Poster
 
0
  #6
23 Days Ago
>>?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 20
Reputation: AndreiDMS is an unknown quantity at this point 
Solved Threads: 0
AndreiDMS AndreiDMS is offline Offline
Newbie Poster
 
0
  #7
23 Days Ago
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:
  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 "+"
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 17
Reputation: alreem is an unknown quantity at this point 
Solved Threads: 0
alreem alreem is offline Offline
Newbie Poster
 
0
  #8
20 Days Ago
emm

how I do it ?
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 17
Reputation: alreem is an unknown quantity at this point 
Solved Threads: 0
alreem alreem is offline Offline
Newbie Poster
 
0
  #9
19 Days Ago
Plz , help me ...


I did not know how to do it . . ?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 20
Reputation: AndreiDMS is an unknown quantity at this point 
Solved Threads: 0
AndreiDMS AndreiDMS is offline Offline
Newbie Poster
 
0
  #10
19 Days Ago
Try this:
  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. }
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC