简体   繁体   中英

Print triangle (Half Pyramid) pattern using Java

I have to print a triangle pattern (Half Pyramid) like

1
0 1
1 0 1
0 1 0 1

I tried with this program

class tri{
 public static void main(String arg[]){
    int i,j,a = 1, b =0, c=0;
    for(i=1; i<=4; i++){

        for(j=1; j<=i; j++){
            System.out.print(a+ " ");
            c = a;
            a = b;
            b = c;              
        }
        System.out.println();
    }
 }
}

but this prints pattern as shown in image

输出上述代码

please if some one could help me editing that code to bring the pattern

You need to set starting values correctly. Because what you are doing is continuously swapping

Say row two 0 1

last element = 1, (a = 1, b = 0) and on swapping (a = 0, b = 1) for next row first element.

However this is incorrect as it was supposed to start with (a = 1) and not (a = 0) from previous state.

        int i,j,a = 1, b =0, c=0;
        for (i = 1; i <= 4; i++){
            if (i % 2 == 0) {
                a = 0;
                b = 1;          
            } else {
                a = 1;
                b = 0;          
            } 
            for(j=1; j<=i; j++) {
                System.out.print(a+ " ");
                c = a; 
                a = b;
                b = c;                           
            }           
            System.out.println();
        }

You can also switch between 0 and 1 using XOR :

int i, j, a = 1;
for (i = 1; i <= 4; i++){
    a = i % 2;          
    for(j=1; j<=i; j++) {
        System.out.print(a+ " ");
        a = a ^ 1;                         
    }           
    System.out.println();
}

However Shorter solution would be :

String str = "";
for (int i = 1; i <= 4; i++) {
    str = (i % 2) + " " + str;  
    System.out.println(str);    
} 

output :

1 
0 1 
1 0 1 
0 1 0 1 

The shortest form would be

String str = "";
for (int i = 1; i <= 4; i++) {
    str = (i % 2) + " " + str;  
    System.out.println(str);    
} 

This will give output as you desired

1
0 1
1 0 1
0 1 0 1

You can use boolean flag for this to check if you are current starting at 1 or 0;

sample:

boolean flag = true;
    for(int i=1; i<=4; i++){

        for(int j=1; j<=i; j++){
            if(flag)
                System.out.print("1 ");
            else
                System.out.print("0 ");
            flag = !flag;
        }
        if((i % 2) == 0)
            flag = true;
        else
            flag = false;
        System.out.println();
    }

result:

1 
0 1 
1 0 1 
0 1 0 1 
   int x=1,y=1;
   for(int i=1;i<8;i++){
       for(int k=0;k<i;k++){
           y=(k==0) ? x:y;
           System.out.print(y+" ");
           y=(y==1) ? 0:1;
       }
       System.out.println("");
       x=(x==1) ? 0:1;
   }

output--- 在此输入图像描述

write my anwser here, seen @sujithvm solution is more short and efficient.

    int sideLength = 4;
    for(int i = 0 ; i < sideLength ; i++)
    {
        for(int j = 0 ; j <= i ; j++)
        {
            System.out.print((i + j + 1) % 2 + " ");
        }
        System.out.println();
    }

Complete Java program for beginners:

public class PrintPattern15

{

public static void main(String args[])
{
    int n = 5;
    PrintPattern15 d = new PrintPattern15();
    d.printPattern(n);
}
public void printPattern(int noOfRows)
{   
    for(int i = 1; i <= noOfRows; i++ )
    {
        printRows(i);
    }
}
public void printRows(int startPt)
{   
    for(int i = startPt ; i >= 1; i--)
    {
        System.out.print(i%2);
    }
        System.out.println();

}

}

This Works for me:

public class DrawPattern {

    public static void main(String[] args) {
        int i, j;
        int num = 7;
        for (i = 0; i <num; i++) {
            for (j = 0; j < i; j++) {
                if (isConditionMatch(num, i, j)) {
                    System.out.print("0");
                } else {
                    System.out.print("1");
                }
            }
            System.out.println();
        }

    }

    private static boolean isConditionMatch(int num, int i, int j) {
        return (i%2 != 0 && j%2 !=0 || (i%2 == 0 && j%2==0));
    }
}

Output:

1
01
101
0101
10101
010101

Code

public class pra1 {
    public static void main(String[] args) {
        int  space, rows=11, k=0;
          //  Scanner scan = new Scanner(System.in);
          //  System.out.print("Enter Number of Rows : ");
          //  rows = scan.nextInt();
        //  rowa=6;

            for(int i=1; i<=rows; i++)
            {
                for(space=1; space<=(rows-i); space++)
                {
                    System.out.print("  ");
                }
                while(k != (2*i-1))
                {  // int p=k;
                    if(k%2==0)
                    {

                    System.out.print("1 ");
                   //  System.out.print("  ");
                    }if(k%2!=0 )
                    {

                    System.out.print("0 ");
                   // System.out.print("  ");
                    }

                    else{
                    System.out.print("");
                    }
                 //   p--;
                    k++;
                }
                k = 0;
                System.out.println();
            }

    }

}

Output

                    1 
                  1 0 1 
                1 0 1 0 1 
              1 0 1 0 1 0 1 
            1 0 1 0 1 0 1 0 1 
          1 0 1 0 1 0 1 0 1 0 1 
        1 0 1 0 1 0 1 0 1 0 1 0 1 
      1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 
    1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 
  1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 

This is how I solved the problem:

int k = 0;    
for (i = 1; i <= 4; i++){
k = (i%2 == 0)? 1:0;

for(j=1; j<=i; j++) {
    System.out.print(k+ " ");
    k = k==0?1:0;                       
}           
System.out.println();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM