Write a program to generate the following triangle using for loop.
1
0 1
1 0 1
0 1 0 1
Last active
January 18, 2019 08:06
-
-
Save theArjun/3fa3aef23f094cff4dede07d03c6e525 to your computer and use it in GitHub Desktop.
Prints Triangle using Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Five{ | |
| public static void main(String[] args){ | |
| for(int i = 1; i <= 4; i++){ // First we are running loop from 1 to 4. | |
| for(int j = 0; j < i; j++){ // Then we are running loop from 0 until i. | |
| System.out.print((j+i)%2+" "); // Sum of i and j modulus of 2 equals the alternate of 1 and 0. | |
| } | |
| System.out.println(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment