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...
darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
Hi alreem,
please next time use the [code] tags to enclose your code,
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
AndreiDMS
Junior Poster in Training
68 posts since Oct 2009
Reputation Points: 10
Solved Threads: 9
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:
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("+");
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 "+"
AndreiDMS
Junior Poster in Training
68 posts since Oct 2009
Reputation Points: 10
Solved Threads: 9
Try this:
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;
}
}
AndreiDMS
Junior Poster in Training
68 posts since Oct 2009
Reputation Points: 10
Solved Threads: 9
go backward from i = number -1;
while (i > 0)
and replace i++ with i--
i = number - 1;
while (i>0)
{
k=1;
while (k<=(number-i)){
System.out.print(" ");
k++; }
k=1;
while (k<=i){
System.out.print("+");
k++;}
k=i-1;
while (k>=1){
System.out.print("+");
k--;}
System.out.println();
i--;
}
}
AndreiDMS
Junior Poster in Training
68 posts since Oct 2009
Reputation Points: 10
Solved Threads: 9
write aprogramto generate a business travel express attaachment for an income tax return. The program should request ias input the name of the organization visited, the date and location of the visit,and the expenses for meals and entertainment,airplane fare,loding,and taxi fares.(Only 50% of the expenses for meals and entertainment are deductible) . Sub procedures should be used for the input and output .
One, the rule is that we don't help without effort on your part. Two, you should start your own thread.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711