| | |
help..
![]() |
1
#2 18 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...
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. •
•
Join Date: Nov 2009
Posts: 17
Reputation:
Solved Threads: 0
0
#3 18 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;
}
}
}
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;
}
}
}
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
0
#4 18 Days Ago
Hi alreem,
please next time use the [code] tags to enclose your code,
anyway I hope I didn't just served the code to you and that you will understand what's modified
GL
please next time use the [code] tags to enclose your code,
Java Syntax (Toggle Plain Text)
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"); // here you should read the number again until it's odd or // use System.exit() to exit the program } else System.out.println(" The number is odd "); count=1; //while (count<= number){ while (count<= (number/2)+1){ // loop until you get to the middle number i=1; while(i<=count){ //System.out.print(count); System.out.print("+"); i=i+1; } System.out.println(); count=count+1; } //count=(number-1); // this will go into an infinite loop //while (count<=(number-1)){ //since the counter will be always // smaller then (number-1) count = (number/2); // now starting from the middle while (count > 0){ // go backward i=1; while(i<=count){ // System.out.print(count); System.out.print("+"); i=i+1; } System.out.println(); count=count-1; } }
anyway I hope I didn't just served the code to you and that you will understand what's modified
GL
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
0
#7 16 Days Ago
You should find a formula for computing the withe spaces for each line and add it to the code.
Something like:
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 "+"
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;
Java Syntax (Toggle Plain Text)
int space_characters = (number + 1) / 2 - count; int plus_characters = count * 2 -1; for (int k = 0; k < space_characters; k++) System.out.println(" "); for (int k = 0; k < plus_characters; k++) System.out.println("+");
though you might not get the same pattern on screen because the width of the " " sign might be smaller than the one of the "+"
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
0
#10 12 Days Ago
Try this:
Java Syntax (Toggle Plain Text)
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"); // here you should read the number again until it's odd or // use System.exit() to exit the program } else System.out.println(" The number is odd "); count=1; //while (count<= number){ while (count<= (number/2)+1){ // loop until you get to the middle number int space_characters = (number + 1) / 2 - count; int plus_characters = count * 2 -1; for (int k = 0; k < space_characters; k++) System.out.print(" "); for (int k = 0; k < plus_characters; k++) System.out.print("+"); } System.out.println(); count=count+1; } //count=(number-1); // this will go into an infinite loop //while (count<=(number-1)){ //since the counter will be always // smaller then (number-1) count = (number/2); // now starting from the middle while (count > 0){ // go backward int space_characters = (number + 1) / 2 - count; int plus_characters = count * 2 -1; for (int k = 0; k < space_characters; k++) System.out.print(" "); for (int k = 0; k < plus_characters; k++) System.out.print("+"); } System.out.println(); count=count-1; } }
![]() |
Other Threads in the Java Forum
- Previous Thread: Strange compiler bug
- Next Thread: Can I write into a jar?
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing system test textfields threads time title tree tutorial-sample ubuntu update windows working





