-
April 11th, 2010, 08:33 AM
#1
Starter Java programmer
i wanted this as my program output.
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
n go on n on.
Till now i have programmed this much but not gettin the correct output...!!
import java.util.*;
class pyramid
{
public static void main(String args[])
{
int i,k,j,r;
System.out.print("Enter No. of Rows : ");
Scanner a=new Scanner(System.in);
r=a.nextInt();
for(i=1;i<=r;i++)
{
for(j=1;j<=i;j++)
System.out.print(" "+j);
for(k=i-1;k>=1;k--)
System.out.print(" "+k);
System.out.println(" ");
}
}
}
/* OUTPUT
D:\Salil JAVA>javac pyramid.java
D:\Salil JAVA>java pyramid
Enter No. of Rows : 4
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
D:\Salil JAVA>
*/
I have coded most of it..
Just missin the spaces...!!
and can anyone help me out with
+*
++**
+++***
++++****
+++++*****
thnx waiting eagerly for ur replies...!!
-
April 11th, 2010, 08:36 AM
#2
Re: Starter Java programmer
Oops..Sorry
for the first program the output i want is
1
121
12321
1234321
123454321
-
April 11th, 2010, 08:37 AM
#3
Re: Starter Java programmer
***?
m neither able to post spaces in the main post nor in the quote...what do i do ??? (
-
April 11th, 2010, 08:38 AM
#4
Re: Starter Java programmer
Mods m extremely sorry for multireplying my own thread...
I can't edit my posts..!
Just to tell i wanted the o/p in the screen..!
-
April 11th, 2010, 12:42 PM
#5
Re: Starter Java programmer
Originally Posted by devilasad
***?
m neither able to post spaces in the main post nor in the quote...what do i do ??? (
Put it in [CODE]...[/CODE] tags? Use underscores and say you mean spaces?
Experience is a poor teacher: it gives its tests before it teaches its lessons...
Anon.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
April 11th, 2010, 01:05 PM
#6
Re: Starter Java programmer
Originally Posted by dlorde
Put it in tags? Use underscores and say you mean spaces?
Experience is a poor teacher: it gives its tests before it teaches its lessons...
Anon.
Thnx Anon...helpful..!!
this is Expected o/p
Code:
____1
___121
__12321
_1234321
123454321
__ ==> Spaces
-
April 12th, 2010, 04:06 PM
#7
Re: Starter Java programmer
The code in your first post is adding a space between each digit but the last example output you posted shows no spaces between the digits, so which one do you want?
If you look at the relationship between the number of spaces required in the first row and the number of rows you are printing and also look at how the number of spaces changes with each row you should be able to work out how to write a for loop to add the spaces.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|