Sei sulla pagina 1di 8

Pyramid Sequence

1
1 2
1 2 3
1 2 3 4
class Pyramid1
{
public static void main(String args[])
{
int i,j;
for(i=1;i< =4;i++)
{
for(j=1;j< =i;j++)
System.out.print(" "+j);
System.out.print("\n");
}
}
}
/********* OUTPUT ********
1
1 2
1 2 3
1 2 3 4 */
Labels: Core Java, Pyramid
This Post has 31 Comments Add your own!
ket - February 18, 2009 8:28 PM
I want the answer of:
1
12
123
12
1
Lionel - February 18, 2009 9:03 PM
here is the code :
class Pyramid1
{
public static void main(String args[])
{
int i,j;
for(i=1;i< =3;i++)
{
for(j=1;j< =i;j++)
System.out.print(" "+j);
System.out.print("\n");
}
for(i=2;i> =1;i--)
{
for(j=1;j< =i;j++)
System.out.print(" "+j);
System.out.print("\n");
}
}
}

calvin - July 8, 2009 6:45 PM


how do you do this?
1
121
12321
1234321
123454321
12345654321
123454321
1234321
12321
121
1
Lionel - July 8, 2009 9:49 PM
@calvin
here is the code :
i think it should work
class Pyramid1
{
public static void main(String args[])
{
int i,k,j;
for(i=1;i< =6;i++)
{
for(j=1;j< =i;j++)
System.out.print(" "+j);
for(k=i-1;k> =1;k--)
System.out.print(" "+k);
System.out.print("\n");
}
for(i=5;i> =1;i--)
{
for(j=1;j< =i;j++)
System.out.print(" "+j);
for(k=i-1;k> =1;k--)
System.out.print(" "+k);
}
}
}
Rajiv - August 21, 2009 1:20 PM
Above code is not proper working..............
aBeY - September 8, 2009 6:21 AM
Can you help me please!!
I want the answer of :
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
Anonymous - October 22, 2009 1:52 AM
Hi! I'm a beginner in Java can you please give a sample code for this:
0

90
890
7890
67890
567890
4567890
34567890
234567890
1234567890
vikas - December 27, 2009 8:34 PM
i want answer of:
*
* *
* * *
* * * *
Anonymous - February 3, 2010 8:15 AM
hi i need
1
2 3 2
3 4 5 4 3
4 5 6 7 6
5 6 7 8 9
Anonymous

code for the output

5 4
8 7 6 5
- March 28, 2010 9:47 PM

hi
i want this output
4
3 4 3
2 3 4 3 2
1 2 3 4 3 2 1
Anonymous - March 28, 2010 9:49 PM
hi
i want this output
4
3 4 3
2 3 4 3 2
1 2 3 4 3 2 1
Anonymous - March 28, 2010 9:49 PM
hi
i want this output
4
3 4 3
2 3 4 3 2
1 2 3 4 3 2 1
Anonymous - May 12, 2010 7:00 PM
write a program to print the following structure
1 2 3 4 5
2 3 4
3
2 3 4
1 2 3 4 5
Anonymous - June 23, 2010 5:55 PM

class Loop
{
public static void main (String args[])
{
for
(int i=1; i<=9; i++)
{
for
(int j=1; j<=i; j++)
{
System.out.print("");
System.out.print(*);
}
System.out.print("\n");
}
System.out.println ("\n");
}
}
Anonymous - July 8, 2010 9:19 PM
i want a program to print
1
2 3
4 5 6
7 8 9 10
Anonymous - August 9, 2010 4:30 PM
Write a java program to print
123
456
789
Anonymous - August 9, 2010 4:32 PM
Write a java program to print
123
456
789
Anonymous - August 24, 2010 3:25 PM
hw if using the 'do while' loop?
Anonymous - September 9, 2010 5:39 PM
I want answer to this pls...
1
222
33333
4444444
555555555
Anonymous - October 13, 2010 8:52 PM
@abey:
class Pyramid{
public static void main(String args[]){
int i,k,j;
for(i=1;i<=128;i= i*2){
for(j=1; j<=i; j=j*2)
System.out.print(j + " ");

for(k=i/2; k>=1; k=k/2)


System.out.print( k + " ");
System.out.print("\n");
}
}
}
Anonymous - October 15, 2010 5:59 PM
how to make as java program using for loop that will look like this
1
121
12321
1234321
123454321
need it on wednesday
Anonymous - October 17, 2010 2:24 PM
hi..i am ikko...
how can i get this result frmo a java program??
----------------------1
--------------------1 2 1
------------------1 2 4 2 1
----------------1 2 4 8 4 2 1
-------------1 2 4 8 16 8 4 2 1
---------1 2 4 8 16 32 16 8 4 2 1
------1 2 4 8 16 32 64 32 16 8 4 2 1
--1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
Jason - October 29, 2010 7:06 PM
public static void count(int n) {
int s = 1;
for (int i = 1; i <= n; i++) {
while (s <= i) {
System.out.print(s);
s++;
}
s--;
while (s > 1) {
System.out.print(--s);
}
System.out.println();
}
}
//output for this one:
1
121
12321
1234321
etc. etc.
public static void value_times_two(int n) {

int s, p;
s = 1;
p = 1;
for (int i = 1; i <= n; i++) {
p = 1;
System.out.print(p);
while (s <= i) {
System.out.print(p *= 2);
s++;
}
while (s > 1) {
System.out.print(p /= 2);
--s;
}
System.out.println();
}
}
//expected output for this one is as follows:
1
121
12421
1248421
etc. etc.
Anonymous - November 3, 2010 3:53 PM
hi..
Can anyone give me code for below output...??
A
A B A
A B C B A
A B C D C B A
A B C D E D C B A
Anonymous - November 28, 2010 1:07 PM
i need a code for this program :]
kindly help me thanks .
1
232
34543
4567654
567898765
tuxedo mask - December 15, 2010 7:13 AM
how to create triangle of numbers in this type of form:
output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Tripta - March 9, 2011 1:34 PM
*
* *
* * * *
* * * * * * * *
Tripta - March 9, 2011 1:35 PM

I want the answer of :


*
* *
* * * *
* * * * * * * *
Tom - May 4, 2011 12:19 PM
i want the answer for this

1
22
333
4444
55555
6666
777
88
9
Anonymous - May 4, 2011 12:22 PM
i need code for this
1
22
333
4444
55555
6666
777
88
9
Anonymous - June 8, 2011 1:53 AM
hey
1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9

tom code fr
3
4 4
5 5 5
6 6
7

class Pyramid1
{
public static void main(String args[])
{
int k;
//for loop for the no of lines to print till 55555
for(int i=1;i<=5;i++)
{
System.out.println();
//for loop for printin da nos
for(int j=1;j<=i;j++)
{
k=i;

System.out.print(k);
}
}
//making k=6 to start printin 6666 after 55555
k=6;
for(int i=1;i<=4;i++)
{
//to change the line
System.out.println();
for(int j=4;j>=i;j--)
{
System.out.print(k);
}
k++;
}
}
}
hope it helps u,i hav tried it nd it works.

Potrebbero piacerti anche