Floyd Triangle in C with 0 and 1 Only

Introduction

In this article, I am going to explain how to print Floyd triangle in c programming. In this pattern printing program, we will see two programs for the same topic.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

Program for Floyd Triangle in C

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i,j,n, count=0;
    printf("Enter number of rows\n");
    scanf("%d",&n);
    printf("Floyd Triangle is \n");
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
           count++; 
           printf("%d ",count);
        }
         printf("\n")
    }
   return 0;
}

Output

Enter number of rows
4
Floyd Triangle is 
1
2 3
4 5 6
7 8 9 10

As you can see the output which is given above. For printing this output, we have used the variable count. In this program, we will have to increment the value of count and print this value. We are using two loops for printing Floyd triangle because we need to print rows and columns.

Also Read: Reverse a Number in C

Floyd Triangle in C with 0 and 1

I hope you have understood what is Floyd triangle but that c program is very easy. In most exams, you have to write a c program to print Floyd triangle with 0 and 1 as shown in the following output.

Output

Enter number of rows
4
Floyd Triangle is 
1
0 1
1 0 1
0 1 0 1

In first attempt, every one find it very easy. But, after trying, most of the students get confuse because they don’t understand how print this pattern. First you have to print 1 and then 0 and again 1 and after that 0 and so on. But, we have to print this in a pattern. Therefore, we have kept the same logic with little modification.

Also Read: Perfect Number in C Programming using All Loops

Now, see the following program.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i,j,n;
    printf("Enter number of rows\n");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            if((i%2)==(j%2))
            {
                printf("1 ");
            }
            else
            {
                printf("0 ");
            }
        }
         printf("\n")
    }
   return 0;
}

I have just some modification in the above program. What is the difference between these two programs? In the first program, we have not used any decision making statements. We will have to print the value of a variable count and then increment.

Also Read: Strong Number in C Programming

But, in the second program, we have to decide when to print 1 and when to print 0. Now, I am going to explain the logic of this program.

Look at the following figure.

floyd triangle with 0 and 1

From the above figure, you can see, I have mentioned the values of i and j. Because, in the above program, I have used two loops. In the first loop, I have used variable i for rows and in the second loop, I have used j for columns.

For printing the Floyd triangle with 0 and 1, just look at the above figure and observe when 1 is printed and 0 is printed? That means while printing the value 1 and 0, what is the value of i and j? You will get some pattern.

Also Read: C Program to Find the Sum of Cubes of Elements in an Array

For printing 1, the values of i and j must be even or odd, but not opposite. Are you getting my point? From the above figure, when 1 is printed? When i=0 and j=0, when i=1 and j=1, when i=2 and j=0 and so on. If the value of i is even and j is odd or vice-versa, then 0 will be printed.

For this purpose, I have written the following code in the above program.

            if((i%2)==(j%2))
            {
                printf("1 ");
            }
            else
            {
                printf("0 ");
            }

We know, % operator is used to give remainder after performing division operation. So, every time we have to check whether the value of i and j is even or odd using the modulo operator. Because, we have to make the decisions, therefore, we have written an if-else statement here.

Also Read: C Program to Print Multiples of 5 using do-while loop

I hope, you have understood both the c programs Floyd triangle. If you know the Hindi language, then watch the following video.

Floyd Triangle with 0 and 1 in C Ex...
Floyd Triangle with 0 and 1 in C Explanation in HIndi

Thanks for watching and reading this post.

Leave a Comment