| | |
help..
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
1
#2 27 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 27 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: 23
Reputation:
Solved Threads: 1
0
#4 27 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: 23
Reputation:
Solved Threads: 1
0
#7 26 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: 23
Reputation:
Solved Threads: 1
0
#10 22 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 |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component database defaultmethod development dice dragging ebook eclipse error event exception formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide image infinite input integer intersect invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie numbers openjavafx parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads time tree windows





