C Program To Print Floyd’s Triangle using For Loop

Lets write C program to print Floyd’s Triangle, using nested for loop.

Floyd’s Triangle: is a right angled Triangle formed with natural numbers.

Related Read:
For Loop In C Programming Language
Nested For Loop In C Programming Language
C Program To Print Floyd’s Triangle

Video Tutorial: C Program To Print Floyd’s Triangle using For Loop


[youtube https://www.youtube.com/watch?v=cfN_uSXJW_w]

YouTube Link: https://www.youtube.com/watch?v=cfN_uSXJW_w [Watch the Video In Full Screen.]

Logic To Print Floyd’s Triangle using For Loop

We ask the user to input the number of rows for Floyd’s Triangle, we store it inside variable num.

Outer For loop
In our C program, row number and the total numbers to be printed for that row is present inside variable row. So the outer for loop selects the row number and the number of elements to be printed in that row.

We initialize the row to 1 and iterate through the outer for loop until row is less than or equal to user entered number. For each iteration of the outer for loop we increment the value of row by 1. That way selecting the next row for each iteration.

Inner For loop
Inner for loop prints natural numbers in each selected row. Variable col is assigned to 1 for each iteration of outer for loop, so that the numbers gets printed from the first position in any selected row.

Inner for loop prints the natural number from the first position till the selected row number of times.

For Example, if user enters num = 5, the following Triangle will be printed:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Note that the Triangle printed is a right angled Triangle and has 5 rows of natural numbers.

Important Note:
Also note that first row has 1 number. Second row has 2 numbers. Third row has 3 numbers and so on. So row number and total numbers in that particular row are always equal in any Floyd’s Triangle.

Source Code: C Program To Print Floyd’s Triangle using For Loop

#include<stdio.h>

int main()
{
    int num, row, col, count = 1;

    printf("Enter number of rows for Floyd's Triangle\n");
    scanf("%d", &num);

    printf("\n");
    for(row = 1; row <= num; row++)
    {
        for(col = 1; col <= row; col++)
        {
            printf("%d  ", count++);
        }
        printf("\n");
    }

    return 0;
}

Output 1:
Enter number of rows for Floyd’s Triangle
5

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Output 2:
Enter number of rows for Floyd’s Triangle
14

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Draw Pyramid of Alphabets, using While Loop

Lets write a C program to draw / print / display a pyramid / triangle formed from uppercase / capital letter English Alphabets, using while loop.

Related Read:
Nested While Loop: C Program
C Program To Draw Pyramid of Stars, using While Loop

Expected Output for the Input

User Input:
Enter the number of rows for Pyramid
5

Output:

    
     A
    BCD
   EFGHI
  JKLMNOP
 QRSTUVWXY

Pyramid With 20 Rows
pyramid of alphabets

Video Tutorial: C Program To Draw Pyramid of Alphabets, using While Loop


[youtube https://www.youtube.com/watch?v=eDPYA_c73I4]

YouTube Link: https://www.youtube.com/watch?v=eDPYA_c73I4 [Watch the Video In Full Screen.]

Logic To Draw Pyramid of Alphabets, using While Loop

We ask the user to enter the maximum number of rows for the pyramid and store it inside the address of variable num. We declare and initialize a variable count to 1(indicating first row of the pyramid).

In the outer while loop we check if count is less than or equal to num. For each iteration of the while loop we increment the value of count by 1. So the count value will have the row number i.e., for each iteration of the outer while loop we select row one by one (to print the alphabets).

Inside first inner while loop we print the adequate number of space for each row. Inside the second inner while loop we actually print the alphabets needed for each row.

At the end the result will be a pyramid with the number of rows as input by the user.

ASCII Value of Capital Letter English Alphabets

ASCII value of A is 65

ASCII value of B is 66

ASCII value of C is 67

ASCII value of D is 68

ASCII value of E is 69

ASCII value of F is 70

ASCII value of G is 71

ASCII value of H is 72

ASCII value of I is 73

ASCII value of J is 74

ASCII value of K is 75

ASCII value of L is 76

ASCII value of M is 77

ASCII value of N is 78

ASCII value of O is 79

ASCII value of P is 80

ASCII value of Q is 81

ASCII value of R is 82

ASCII value of S is 83

ASCII value of T is 84

ASCII value of U is 85

ASCII value of V is 86

ASCII value of W is 87

ASCII value of X is 88

ASCII value of Y is 89

ASCII value of Z is 90

Reference: C Program To Print All ASCII Characters and Code

Source Code: C Program To Draw Pyramid of Alphabets, using While Loop

#include < stdio.h >

int main()
{
    int num, count = 1, i, alphabet = 65;

    printf("Enter the number of rows for Pyramid\n");
    scanf("%d", &num);

    while(count <= num)
    {
        i = 0;
        while(i <= (num-count))
        {
            printf(" ");
            i++;
        }

        i = 0;
        while(i < (2*count-1))
        {
            printf("%c", alphabet);

            if(alphabet == 90)
            {
                alphabet = 64;
            }

            alphabet++;
            i++;
        }
        printf("\n");
        count++;
    }

    return 0;
}

Output
Enter the number of rows for Pyramid
14

              A
             BCD
            EFGHI
           JKLMNOP
          QRSTUVWXY
         ZABCDEFGHIJ
        KLMNOPQRSTUVW
       XYZABCDEFGHIJKL
      MNOPQRSTUVWXYZABC
     DEFGHIJKLMNOPQRSTUV
    WXYZABCDEFGHIJKLMNOPQ
   RSTUVWXYZABCDEFGHIJKLMN
  OPQRSTUVWXYZABCDEFGHIJKLM
 NOPQRSTUVWXYZABCDEFGHIJKLMN

Note: Inside second nested while loop we are checking for the condition – if alphabet is equal to 90(ASCII value of Z), and then reinitializing it to 64(but ASCII value of A is 65), that is because after the if condition is executed we have alphabet++; statement which increments the value of variable alphabet from 64 to 65(which is the ASCII value of A).

Alphabets and ASCII Value
We can even write the code in if else block as shown below:

        i = 0;
        while(i < (2*count-1))
        {
            printf("%c", alphabet);

            if(alphabet == 90)
            {
                alphabet = 65;
            }
            else
            {
                alphabet++;
            }
            i++;
        }

In this case we assign the value of variable alphabet to 65(ASCII value of A) once it has reached a value of 90(ASCII value of Z).

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Draw Pyramid of Stars, using While Loop

Lets write a C program to draw / print / display a pyramid / triangle formed from stars, using while loop.

Related Read:
while loop in C programming
Nested While Loop: C Program

Expected Output for the Input

User Input:
Enter no of rows of Pyramid
5

Output:

    
     *
    ***
   *****
  *******
 *********

Pyramid With 20 Rows
pyramid of stars

Video Tutorial: C Program To Draw Pyramid of Stars, using While Loop


[youtube https://www.youtube.com/watch?v=pwLx0cELxY4]

YouTube Link: https://www.youtube.com/watch?v=pwLx0cELxY4 [Watch the Video In Full Screen.]

Logic To Draw Pyramid of Stars, using While Loop

We ask the user to enter the maximum number of rows for the pyramid and store it inside the address of variable num. We declare and initialize a variable count to 1(indicating first row of the pyramid).

In the outer while loop we check if count is less than or equal to num. For each iteration of the while loop we increment the value of count by 1. So the count value will have the row number i.e., for each iteration of the outer while loop we select row one by one (to print the stars).

Inside first inner while loop we print the adequate number of space for each row. Inside the second inner while loop we actually print the stars needed for each row.

At the end the result will be a pyramid with the number of rows as input by the user.

Source Code: C Program To Draw Pyramid of Stars, using While Loop

#include<stdio.h>

int main()
{
    int num, count = 1, i;

    printf("Enter no of rows of Pyramid\n");
    scanf("%d", &num);

    while(count <= num)
    {
        i = 0;
        while( i <= (num - count) )
        {
            printf(" ");
            i++;
        }

        i = 0;
        while(i < (2*count-1))
        {
            printf("*");
            i++;
        }

        printf("\n");
        count++;
    }

    return 0;
}

Output 1:
Enter no of rows of Pyramid
5

     *
    ***
   *****
  *******
 *********

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Display Right Angled Triangle With Alphabets, using While Loop

Lets write a C program to display/print/output a right angled triangle pattern formed with English Alphabets, using nested while loop.

Related Read:
Nested While Loop: C Program
C Program To Print Floyd’s Triangle

Expected Output for the Input

If user enters number of rows as 5. Then our C program prints a Right angled Triangle with 5 rows of English Alphabets in it.

User Input:
num = 5;

Output:

A
B B
C C C
D D D D
E E E E E

Video Tutorial: C Program To Print Right Angled Triangle With Alphabets, using While Loop


[youtube https://www.youtube.com/watch?v=_6oaPQKb5Ks]

YouTube Link: https://www.youtube.com/watch?v=_6oaPQKb5Ks [Watch the Video In Full Screen.]

Logic To Display Right Angled Triangle With Alphabets, using While Loop

If we take a closer look at the right angled triangle, we can see that the number of rows and the number of elements in that row are same. Example, row number 5 has 5 elements. Row number 6 has 6 elements etc. So we store this number in a variable called row. Since row starts from 1, we initialize the variable row to 1.

We ask the user to enter the maximum number of rows for the right angled triangle, and store it in address of variable num. In outer while loop we check if value of row is less than or equal to num. For each iteration of the outer while loop we increment the value of row by 1.

Inside the outer while loop we initialze the variable start to 1 – to print the alphabets from position 1 for that particular row number present in variable row. We increment the value of start by 1 for each iteration of inner while loop. Inside inner while loop we print the alphabets using ASCII value of upper case English Alphabets.

Related Read:
C Program To Print All ASCII Characters and Code

ASCII Values of uppercase English Alphabets

ASCII value of A is 65

ASCII value of B is 66

ASCII value of C is 67

ASCII value of D is 68

ASCII value of E is 69

ASCII value of F is 70

ASCII value of G is 71

ASCII value of H is 72

ASCII value of I is 73

ASCII value of J is 74

ASCII value of K is 75

ASCII value of L is 76

ASCII value of M is 77

ASCII value of N is 78

ASCII value of O is 79

ASCII value of P is 80

ASCII value of Q is 81

ASCII value of R is 82

ASCII value of S is 83

ASCII value of T is 84

ASCII value of U is 85

ASCII value of V is 86

ASCII value of W is 87

ASCII value of X is 88

ASCII value of Y is 89

ASCII value of Z is 90

Source Code: C Program To Display Right Angled Triangle With Alphabets, using While Loop

#include<stdio.h>

int main()
{
    int num, row = 1, start;

    printf("Enter no of rows\n");
    scanf("%d", &num);

    printf("\n");
    while(row <= num)
    {
        start = 1;
        while(start <= row)
        {
            printf("%c  ", (row+64));
            start++;
        }
        printf("\n");
        row++;
    }

    return 0;
}

Output 1:
Enter no of rows
5

A
B B
C C C
D D D D
E E E E E

Output 2:
Enter no of rows
10

A
B  B
C  C  C
D  D  D  D
E  E  E  E  E
F  F  F  F  F  F
G  G  G  G  G  G  G
H  H  H  H  H  H  H  H
I  I  I  I  I  I  I  I  I
J  J  J  J  J  J  J  J  J  J

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Print Floyd’s Triangle In Reverse

Lets write C program to print Floyd’s Triangle in reverse, using nested while loop.

Floyd’s Triangle: is a right angled Triangle formed with natural numbers.

Related Read:
while loop in C programming
Nested While Loop: C Program
C Program To Print Floyd’s Triangle

Logic To Print Floyd’s Triangle In Reverse

We ask the user to input the number of rows of Floyd’s Triangle, we store it inside variable num. We assign 1 to variable nn(natural number). Variable num has the number of natural numbers to be printed in particular row. The inner while loop prints the natural numbers upto num.

For Example, if user enters num = 5, the following Triangle will be printed:

1 2 3 4 5
6 7 8 9
10 11 12
13 14
15

Note that the Triangle printed is a right angled Triangle and has 5 rows of natural numbers.

Source Code: C Program To Print Floyd’s Triangle In Reverse

#include < stdio.h >

int main()
{
    int num, nn = 1, count;

    printf("Enter no of rows of Floyd's Triangle\n");
    scanf("%d", &num);
    
    printf("\n");
    while(num)
    {
        count = 1;
        while(count <= num)
        {
            printf("%d  ", nn);
            nn++;
            count++;
        }
        printf("\n");
        num--;
    }

    return 0;
}

Output 1:
Enter no of rows of Floyd’s Triangle
5

1 2 3 4 5
6 7 8 9
10 11 12
13 14
15

Output 2:
Enter no of rows of Floyd’s Triangle
14

1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77
78 79 80 81 82 83 84
85 86 87 88 89 90
91 92 93 94 95
96 97 98 99
100 101 102
103 104
105

Video Tutorial: C Program To Print Floyd’s Triangle In Reverse


[youtube https://www.youtube.com/watch?v=7CTZGsGm6yE]

YouTube Link: https://www.youtube.com/watch?v=7CTZGsGm6yE [Watch the Video In Full Screen.]

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert