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

C Program To Print Floyd’s Triangle

Lets write C program to print Floyd’s Triangle, 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

Logic To Print Floyd’s Triangle

We ask the user to input the number of rows of Floyd’s Triangle, we store it inside variable num. We assign 1 to variables count and count1. Variable count is used to print the natural numbers for Floyd’s Triangle. Variable count1 is used to keep track of outer while loop. Variable count2 is used to keep track of inner while loop.

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.

Outer While loop
In our C program, row number and the total numbers to be printed for that row is present inside variable count1.

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

Source Code: C Program To Print Floyd’s Triangle

#include<stdio.h>

int main()
{
    int num, count = 1, count1 = 1, count2;

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

    printf("\n");

    while(count1 <= num)
    {
        count2 = 1;
        while(count2 <= count1)
        {
            printf("%d  ", count);
            count++;
            count2++;
        }
        count1++;
        printf("\n");
    }

    return 0;
}

Output 1:
Enter no 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 no 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

Video Tutorial: C Program To Print Floyd’s Triangle


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

YouTube Link: https://www.youtube.com/watch?v=5olpgYeAUKU [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

C program To Check whether a Number is Strong Number or Not

Lets write a C program to check whether user entered number is strong number or not, using nested while loop.

Strong Number: Sum of factorial of a number’s individual digits should be equal to the number itself. Such a number is called Strong Number.

For Example: If user entered number is 145. We find factorial of individual digits of 145 and add it.
i.e., !1 + !4 + !5

(1) + (24) + (120) = 145

So the original number and the sum of factorials of individual digits are both 145. So number 145 is considered as a Strong Number.

Related Read:
Basic Arithmetic Operations In C
while loop in C programming
Nested While Loop: C Program

Important Topics
Calculate Sum of Digits: C Program
C Program To Find Factorial of a Number

Source Code: C program To Check whether a Number is Strong Number or Not

#include < stdio.h >

int main()
{
    int num, temp, rem, count, fact, sum = 0;

    printf("Enter a number\n");
    scanf("%d", &num);

    temp = num;

    while(num)
    {
        rem = num % 10;

        count = 1;
        fact  = 1;
        while(count <= rem)
        {
            fact = fact * count;
            count++;
        }

        printf("Factorial of %d is %d\n", rem, fact);

        sum = sum + fact;

        num = num / 10;
    }

    if(temp == sum)
    {
        printf("%d is a strong number\n", temp);
    }
    else
    {
        printf("%d is not a strong number\n", temp);
    }

    return 0;
}

Output 1
Enter a number
145
Factorial of 5 is 120
Factorial of 4 is 24
Factorial of 1 is 1
145 is a strong number

Output 2
Enter a number
140
Factorial of 0 is 1
Factorial of 4 is 24
Factorial of 1 is 1
140 is not a strong number

Video Tutorial: C Program To Find Strong Number


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

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

Logic To Check whether a Number is Strong Number or Not

If user entered number is 145. i.e., num = 145. Using outer while loop we keep fetching digits one by one by using below code.

rem = num % 10;
num = num / 10;

For each iteration the outer loop fetches individual digits of the number. The inner while loop calculates the factorial for that fetched individual digit. Next we keep adding the factorial of each individual digit.

Once the control exits outer while loop we check if the user entered number is equal to the value present in variable sum. If true, then the user entered number is a strong number, if not, its not a strong number.

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

Nested While Loop: C Program

In this video tutorial we’ll demonstrate the use of nested while loop in C programming.

Related Read:
C Program to print Armstrong Numbers between 1 and 500

Number of Iterations In Nested Loops
Number of iterations will be equal to the number of iterations in the outer loop multiplied by the number of iterations in the inner loop.

Nested While Loop: C Program


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

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


Source Code: Single While Loop: C Program

#include < stdio.h >

int main()
{
    int count1 = 1;

    while(count1 <= 5)
    {
        printf("%d\n", count1);
        count1++;
    }

    return 0;
}

Output:
1
2
3
4
5

Source Code: Nested While Loop: C Program

#include < stdio.h >

int main()
{
    int count1 = 1, count2;

    while(count1 <= 5)
    {
        printf("%d\n", count1);
        count2 = 2;

        while(count2)
        {
            printf("   %d\n", count2);
            count2--;
        }

        count1++;
    }

    return 0;
}

Output:

1
   2
   1
2
   2
   1
3
   2
   1
4
   2
   1
5
   2
   1

For every single iteration of the outer while loop, the inner while loop completes its iterations.

A loop inside another loop is called a nested loop. Consider a nested loop where the outer loop runs x times and consists of another loop inside it. The inner loop runs y times. Then, the total number of times the inner loop runs during the program execution is x*y times.

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