| | |
help..
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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...
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 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;
}
}
}
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: 20
Reputation:
Solved Threads: 0
0
#4 24 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: 20
Reputation:
Solved Threads: 0
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:
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: 20
Reputation:
Solved Threads: 0
0
#10 19 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 |
3d 6 @param affinetransform android api applet application arc array arrays automation binary bluetooth bold byte c++ chat class client code color compare component coordinates database detection doctype eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework html ide ideas image ingres input integer internet intersect j2me java java.xls javaexcel javaprojects jni jpanel jtextarea julia keytool keyword linux list loop map method methods mobile netbeans newbie nextline object pong print problem producer program programming project projectideas recursion recursive replaysolutions rim scanner screen sell server set size sms sort sql string swing terminal threads tree web websites windows





