Skip to content

Instantly share code, notes, and snippets.

@theArjun
Last active January 18, 2019 08:06
Show Gist options
  • Select an option

  • Save theArjun/3fa3aef23f094cff4dede07d03c6e525 to your computer and use it in GitHub Desktop.

Select an option

Save theArjun/3fa3aef23f094cff4dede07d03c6e525 to your computer and use it in GitHub Desktop.
Prints Triangle using Java
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();
}
}
}

Write a program to generate the following triangle using for loop.
1
0 1
1 0 1
0 1 0 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment