For Loop

Java Pyramid 6 Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

51 Comments

    • public class patern1

      {

      public static void main(String[] args)

      {

      int k=1;

      for(int i=5; i>0 ;i–)

      {

      for(int j=i; j >0; j–)

      {

      System.out.print(k++);

      }

      k=k-i;

      k++;

      System.out.println(“”);

      }

      }

      }

      /*

      Output of the patern1 would be

      12345
      2345
      345
      45
      5*/

    • public static void main(String[] args) {

      for( int i=1; i<6; i++){

      for( int j=i; j<6; j++){

      System.out.print(j);

      }

      System.out.print(” \n”);

      }

      }

    • public class patern

      {

      public static void main(String[] args)

      {

      for(int i=5; i>0 ;i–)

      {

      for(int j=i; j >0; j–)

      {

      System.out.print(j);

      }

      System.out.println(“”);

      }

      }

      /*Output of the patern would be

      54321
      4321
      321
      21
      1*/

    • int i,j,k;

      for(i=1;i<5;i++)
      {System.out.println();
      for(j=i;j>=1;j–)
      {
      System.out.print(“o”);

      }

      System.out.print(” “);

      for(k=i;k>=1;k–)
      {
      System.out.print(“o”);

      }

      }

    • for(int i=1;i<5;i++)
      {
      for(int j=0;j<i;j++){ system.out.print(“o”);=”” }=”” system.out.println(“”);=”” }=””>

    • public static void main(String[] args) {

      for(int i=1;i<=5;i++) {
      for(int j=1;j<=5-i;j++) {
      System.out.print(“_”);
      }
      for(int k=1;k<=i;k++) {
      System.out.print(“o”);
      }
      System.out.println();
      }

      }

    • class P1

      {

      public static void main(String[] args)
      {

      int y = 0;

      for(int i=1; i<= 4 ;i++)

      {

      for(int j=1; j<=2 ; j++)

      {

      System.out.print(y);

      }

      System.out.println(“”);

      }

      }

      }

  • how about this ??
    9 8 7 6 5 4 3 2 1 0
    8 7 6 5 4 3 2 1 0
    7 6 5 4 3 2 1 0
    6 5 4 3 2 1 0
    5 4 3 2 1 0
    5 3 2 1 0
    3 2 1 0
    2 1 0
    1 0
    0

    thnks

    • public static void main(String[] args) {
      int i,j;
      for(i=9;i>=0;i–)
      { System.out.println();
      for(j=i;j>=0;j–)
      {
      System.out.print(j);
      }
      }
      }

  • Can anyone have solution for this?

    –*–
    -***-
    *****
    -***-
    –*–
    Other than this:

    class PyramidStar{

    public static void main(String []args){

    System.out.println(” * “);

    System.out.println(” *** “);

    System.out.println(“*****”);

    System.out.println(” *** “);

    System.out.println(” * “);

    }

    }

    • public static void main(String[] args) {
      int i,j,k,l;
      for(i=1;i<=5;i+=2)
      { System.out.println();
      for(j=i;j<5;j+=2)
      {
      System.out.print(“_”);
      }
      for(k=i;k>=1;k–)
      {
      System.out.print(“*”);
      }
      for(l=i;l<5;l+=2)
      {
      System.out.print(“_”);
      }

      }
      for(i=3;i>=1;i-=2)
      { System.out.println();
      for(j=i;j<=3;j+=2)
      {
      System.out.print(“_”);
      }
      for(k=i;k>=1;k–)
      {
      System.out.print(“*”);
      }
      for(l=i;l<=3;l+=2)
      {
      System.out.print(“_”);
      }
      }
      System.out.println();

      }

    • int k = 0;

      for(int i = 1; i <= 3; ++i) {//rows=3=i
      for(int j = 1; j <= 3 – i; ++j) {
      System.out.print(“_”);
      }

      while(k != 2 * i – 1) {
      System.out.print(“*”);
      ++k;
      }
      for(int j = 1; j <= 3 – i; ++j) {
      System.out.print(“_”);
      }

      k=0;
      System.out.println();
      }
      for(int i = 2; i >= 1; –i) { //rows=2=i
      for(int j = 1; j <= 3 – i; ++j) {
      System.out.print(“_”);
      }

      while(k != 2 * i – 1) {
      System.out.print(“*”);
      ++k;
      }
      for(int j = 1; j <= 3 – i; ++j) {
      System.out.print(“_”);
      }

      k=0;
      System.out.println();
      }
      }

  • How about this code Please ??? its view should look like a normal triangle??? .

    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

    • public class Ques {
      public static void main(String[] args) {
      int a,b;
      for(a=1;a<=3;a++)
      {System.out.println();
      for(b=a;b<7-a;b++)
      {
      System.out.print(b);
      }
      }
      }
      }

  • how about this code ..? And I need to take this numbers from a arraylist..
    can anybody provide this code ..urgent

    1 2 3 4 5 6 7 8 9 10
    11 12 13 14 15
    16 17 18
    19 20
    21

    • int x,y,z,w;
      for(x=1;x<8;x+=2)
      {System.out.println();
      for(y=x;y>1;y-=2)
      {
      System.out.print(“_”);
      }
      for(z=x;z<8;z++)
      {
      System.out.print(“*”);
      }

      for(w=x;w>1;w-=2)
      {
      System.out.print(“_”);
      }
      }
      for(x=1;x<6;x+=2)
      {
      System.out.println();
      for(y=x;y<5;y+=2)
      {
      System.out.print(“_”);
      }
      for(z=1;z<=x+2;z++)
      {
      System.out.print(“*”);
      }
      for(w=x;w<5;w+=2)
      {
      System.out.print(“_”);
      }

      }

  • 0
    1 0
    0 1 0
    1 0 1 0
    0 1 0 1 0
    i dont understand my mistake regarding this how do i get that i mean whats the mistake in my code

    public static void main(String abc[]) {

    int i,j,l=5;

    for(i=1;i<=5;i++) {

    for(j=i;j>=1;j–)

    System.out.print(j%2);

    System.out.println();

    }

    }

    • public class Pyramid1 {

      public static void main(String[] args) {

      for(int i=1;i<=5;i++){
      for(int j=1;j<=i;j++){
      if((j-i)%2==0){
      System.out.print(‘0’);
      }else{
      System.out.print(‘1’);
      }
      }
      System.out.println(” “);
      }

      }

      }

  • 1
    2 2 2
    3 3 3 3 3
    4 4 4 4 4 4 4
    5 5 5 5 5 5 5 5 5
    6 6 6 6 6 6 6 6 6 6 6

    Can someone help me to get this output? how about the code in java program?

  • ———–1
    ——–2 2 2
    ——3 3 3 3 3
    —-4 4 4 4 4 4 4
    —5 5 5 5 5 5 5 5 5
    6 6 6 6 6 6 6 6 6 6 6

    Can someone help me to get this output? how about the code in java program?

    • int C=1;
      int m=1;
      for(int i = 1; i <= 6; ++i) {
      for(int j = 1; j <= 6 – i; ++j) {
      System.out.print(“_”);
      }

      for(int j=1; j<=m; j++){
      System.out.print(C);
      }
      m+=2;

      C++;
      System.out.println();
      }

Sponsors

Facebook Fans