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 Find Area of Right Angled Triangle

Lets write a C program to calculate area of a right angled Triangle, by asking the user to enter its width and height.

Related Read:
Find Area of a Triangle Using Its Sides: C Program
Find Area of a Triangle Using Its Base and Height: C Program

Formula To Find Area of Right Angled Triangle

Area = (width * Height) / 2.0;

OR

Area = (width * Height * 0.5);

Note: If we divide an expression or number by 2, it’ll return only the integer part and the decimal part will be discarded. So we are dividing the expression by 2.0 (which is of type double).

C program To Find Area of Right Angled Triangle


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

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


Source Code: C program To Find Area of Right Angled Triangle

#include < stdio.h >

int main()
{
    float h, w, area;

    printf("Enter height and width of a right angled triangle\n");
    scanf("%f%f", &h, &w);

    area = (h * w) / 2.0;

    printf("Area of a Right Angled Triangle is %f\n", area);

    return 0;
}

Output:
Enter height and width of a right angled triangle
10
5
Area of a Right Angled Triangle is 25.000000

Validate the Input and Find Area of Triangle: C Program

#include < stdio.h >

int main()
{
    float h, w, area;

    printf("Enter height and width of a right angled triangle\n");
    scanf("%f%f", &h, &w);

    if(w == 0 || h == 0)
    {
        printf("Invalid Input\n");
    }
    else
    {
        area = (h * w) / 2.0;
        printf("Area of a Right Angled Triangle is %f\n", area);
    }

    return 0;
}

Output 1:
Enter height and width of a right angled triangle
10
5
Area of a Right Angled Triangle is 25.000000

Output 2:
Enter height and width of a right angled triangle
0
5
Invalid Input

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

Find Area of a Triangle Using Its Base and Height: C Program

Find the area of the Triangle when it’s base and height are input by the user.

Related Read:
Find Area of a Triangle Using Its Sides: C Program

Formula To Calculate Area of Triangle when its Base and Height are given

Area = (Base * Height) / 2.0;

Note: If we divide an expression or number by 2, it’ll return only the integer part and the decimal part will be discarded. So we are dividing the expression by 2.0 (which is of type double).

Find Area of a Triangle Using Its Base and Height: C Program


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

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


Find Area of a Triangle Using Its Base and Height: C Program

#include < stdio.h >

int main()
{
    float base, height, area;

    printf("Enter length of base of Triangle\n");
    scanf("%f", &base);

    printf("Enter length of height of Triangle\n");
    scanf("%f", &height);

    area = (base * height) / 2.0;

    printf("Area of Triangle is %0.2f\n", area);

    return 0;
}

Output:
Enter length of base of Triangle
15
Enter length of height of Triangle
25
Area of Triangle is 187.50

Validate the Input and Find Area of Triangle: C Program

#include < stdio.h >

int main()
{
    float base, height, area;

    printf("Enter length of base of Triangle\n");
    scanf("%f", &base);

    printf("Enter length of height of Triangle\n");
    scanf("%f", &height);

    if(base == 0 || height == 0)
    {
        printf("Invalid Input\n");
    }
    else
    {
        area = (base * height) / 2.0;
        printf("Area of Triangle is %0.2f\n", area);
    }

    return 0;
}

Output 1:
Enter length of base of Triangle
0
Enter length of height of Triangle
25
Invalid Input

Output 2:
Enter length of base of Triangle
15
Enter length of height of Triangle
30
Area of Triangle is 225.00

Note: Note that the area of Triangle has only 2 digits after the decimal point. That is because we have %0.2f as format specifier in the printf method where we are printing out the area of Triangle.

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

Find Missing Angle in a Triangle if two angles are given: C Program

We ask the user to input 2 angles of the Triangle. And using C Program we calculate and output the 3rd angle.

Related Read:
Basic Arithmetic Operations In C
Triangle Valid or Not based On Sides: C Program.

Formula To Calculate Valid Triangle

a + b + c = 180;

Formula To Find Missing Angle of Triangle

c = 180 – (a + b);

where a, b and c are angles of Triangle.

Note: Also not that if any of the angle is 0, then those 3 angles can’t form a Triangle, so it’s a invalid Triangle.

Find Missing Angle in a Triangle if two angles are given: C Program


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

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


Find Missing Angle in a Triangle if two angles are given: C Program

#include < stdio.h >

int main()
{
    int a1, a2, a3;

    printf("Enter 2 angles of Triangle\n");
    scanf("%d%d", &a1, &a2);

    a3 = 180 - ( a1 + a2 );

    if(a1 != 0 && a2 != 0 && a3 != 0)
    {
        printf("The 3rd angle is %d\n", a3);
    }
    else
    {
        printf("Invalid angles of Triangle\n");
    }

    return 0;
}

Output 1:
Enter 2 angles of Triangle
0
90
Invalid angles of Triangle

Output 2:
Enter 2 angles of Triangle
90
90
Invalid angles of Triangle

Output 3:
Enter 2 angles of Triangle
90
80
The 3rd angle is 10

Output 3 outputs valid Triangle as addition of all 3 angles gives 180.
90 + 80 + 10 = 180.

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

Triangle Valid or Not based On Angles: C Program

Three angles of a Triangle are entered through the keyboard, write a C program to check whether the Triangle is valid or not. The Triangle is valid if the sum of all the angles is exactly equal to 180.

Related Read:
Basic Arithmetic Operations In C
Triangle Valid or Not based On Sides: C Program.

Logic To Find Valid Triangle or Not

We ask the user to enter all 3 angles of a Triangle. Then we add all these angles and if the result is 180 then its a valid Triangle, if not, its not a valid Triangle.

Formula To Calculate Valid Triangle
a + b + c = 180;

where a, b and c are 3 angles of a Triangle.

Note: Also not that if any of the angle is 0, then those 3 angles can’t form a Triangle, so it’s a invalid Triangle.

Triangle Valid or Not based On Angles: C Program


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

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


Triangle Valid or Not based On Angles: C Program

#include < stdio.h >

int main()
{
    int a1, a2, a3, sum;

    printf("Enter 3 angles of a Triangle\n");
    scanf("%d%d%d", &a1, &a2, &a3);

    sum = a1 + a2 + a3;

    if(sum == 180 && a1 != 0 && a2 != 0 && a3 != 0)
    {
        printf("It's a valid Triangle\n");
    }
    else
    {
        printf("It's not a valid Triangle\n");
    }

    return 0;
}

Output 1:
Enter 3 angles of a Triangle
90
80
0
It’s not a valid Triangle

Output 2:
Enter 3 angles of a Triangle
90
90
90
It’s not a valid Triangle

Output 3:
Enter 3 angles of a Triangle
90
80
10
It’s a valid Triangle

Output 3 outputs valid Triangle as addition of all 3 angles gives 180.
90 + 80 + 10 = 180.

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