w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Print the Floyd's Triangle

C# Sharp For Loop: Exercise-22 with Solution

Write a program in C# Sharp to print the Floyd's Triangle. The Floyd's triangle is as below :

    1 
    01
    101 
    0101 
    10101 
C# Sharp: Print the Floyd's Triangle

Sample Solution:-

C# Sharp Code:

using System;  
public class Exercise22  
{  
    public static void Main()
{
    int i,j,n,p,q;
   
	Console.Write("\n\n");
    Console.Write("Print the Floyd's Triangle:\n");
    Console.Write("-----------------------------");
    Console.Write("\n\n");     
   
   Console.Write("Input number of rows : ");
   n= Convert.ToInt32(Console.ReadLine());     
   for(i=1;i<=n;i++)
   {
     if(i%2==0)
     { p=1;q=0;}
     else
     { p=0;q=1;}
      for(j=1;j<=i;j++)
	 if(j%2==0)
	    Console.Write("{0}",p);
	 else
	    Console.Write("{0}",q);
     Console.Write("\n");
   }
  } 
} 

Sample Output:

Print the Floyd's Triangle:                                                                                                         
-----------------------------                       
Input number of rows : 8                                                                                                         
1                                                                                                         
01                                                                                                         
101                                                                                                         
0101                                                                                                         
10101                                                                                                         
010101                                                                                                         
1010101                                                                                                         
01010101  

Flowchart:

Flowchart: Print the Floyd's Triangle

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to display the sum of the series [ 9 + 99 + 999 + 9999 ...].
Next: Write a program in C# Sharp to display the sum of the series [ 1+x+x^2/2!+x^3/3!+....].

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming