Triangle Valid or Not based On Sides: C Program

If the three sides 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 two sides is greater than the largest of the three sides.

Related Read:
Basic Arithmetic Operations In C
Find Area of a Triangle Using Its Sides: C Program
Triangle Valid or Not based On Angles: C Program

Logic To Find Valid Triangle or Not

First we find out biggest side in the 3 sides of the triangle. Next we add the other 2 sides. Now the addition of the other 2 sides must be greater than the biggest side of the Triangle, for a Triangle to be valid. If not, its not a Triangle.

Example:
If a, b and c are 3 sides of the Triangle. If c is the largest side. Then for the Triangle to be valid, (a+b) must be greater than c.
(a+b) > c
If this is true, then Triangle is valid, if not, its invalid Triangle.

Triangle Valid or Not based On Sides: C Program


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

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


Triangle Valid or Not based On Sides: C Program

#include < stdio.h >

int main()
{
    float a, b, c, flag = 0;

    printf("Enter values for a, b and c\n");
    scanf("%f%f%f", &a, &b, &c);

    if( a>b && a>c )
    {
        flag = ((b+c) > a);
    }
    else if( b>c )
    {
        flag = ((a+c) > b);
    }
    else
    {
        flag = ((a+b) > c);
    }

    if(flag)
    {
        printf("Valid Triangle\n");
    }
    else
    {
        printf("Invalid Triangle\n");
    }

    return 0;
}

Output 1:
Enter values for a, b and c
10
4
3
Invalid Triangle

Output 2:
Enter values for a, b and c
10
15
6
Valid Triangle

In above source code, variable flag stores true(1) or false(0) value.

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 Sides: C Program

If lengths of three sides of a Triangle are input through the keyboard, write a program to find the area of the Triangle.

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

Note:A Triangle is valid if the sum of 2 sides of the Triangle is greater than the largest of the 3 sides.

Important: In this program we assume that the user has entered valid lengths of the Triangle.

Formula To Calculate Area and Semi-perimeter of a Triangle

sp = (a + b + c) / 2.0;
area = sqrt( sp * (sp-a) * (sp-b) * (sp-c) );

where a, b and c are lengths of sides of the Triangle.
sp – Semi-perimeter.

Note: In geometry, above formula to calculate area of Triangle is called Heron’s formula (sometimes called Hero’s formula).

Find Area of a Triangle Using Its Sides: C Program


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

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


Find Area of a Triangle Using Its Sides: C Program

#include < stdio.h >
#include < math.h >

int main()
{
    float a, b, c, sp, area;

    printf("Enter values of a, b and c\n");
    scanf("%f%f%f", &a, &b, &c);

    sp = (a+b+c)/2.0;

    area = sqrt(sp*(sp-a)*(sp-b)*(sp-c));

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

    return 0;
}

Output:
Enter values of a, b and c
4
5
6
Area of triangle is 9.921567

Note: While calculating Semi-perimeter, make sure to divide by 2.0 and not by 2. Because division by integer number returns only the integer part of the result. So for certain inputs the result might be wrong. So always use the floating/double value while dividing in C Programs.

Note: Since we are using sqrt() method, we need to include math.h library file (header file) without fail.

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 Armstrong Numbers Between Two Integers

An Armstrong number or Narcissistic number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself.

Example 1:
If number = 153
It has 3 digits: 1, 5 and 3. So n = 3.
result = 13 + 53 + 33 = 1 + 125 + 27 = 153.
So the original number 153 is equal to the result. So it’s an Armstrong Number.

Example 2:
If number = 1634
It has 4 digits: 1, 6, 3 and 4. So n = 4.
result = 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1634.
So the original number 1634 is equal to the result. So it’s an Armstrong Number.

Related Read:
C Program To Reverse a Number
Basic Arithmetic Operations In C
while loop in C programming
if else statement in C
Calculate Sum of Digits: C Program
C Program to Check Armstrong Number
C Program to print Armstrong Numbers between 1 and 500

C Program to print Armstrong Numbers Between Two User Entered Integers


[youtube https://www.youtube.com/watch?v=lDc4Lx5-Ltc]

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


To count the digits in a number

        num = temp = count;
        n   = sum  = 0;

        while(temp)
        {
            temp = temp / 10;
            n++;
        }

To Iterate through the digits in the number

        while(num)
        {
            rem = num % 10;
            sum = sum + pow(rem, n);
            num = num / 10;
        }

To know above code logic, please visit and watch the video tutorial present at C Program to Check Armstrong Number.

Full Source Code: C Program to print Armstrong Numbers Between Two User Entered Integers

#include < stdio.h >
#include < math.h >

int main()
{
    int count, limit, rem, temp, num, n;
    float sum;

    printf("Enter 2 integer numbers\n");
    scanf("%d%d", &count, &limit);

    printf("\nArmstrong numbers between %d and %d\n", count, limit);

    while(count <= limit)
    {
        num = temp = count;
        n   = sum  = 0;

        while(temp)
        {
            temp = temp / 10;
            n++;
        }

        while(num)
        {
            rem = num % 10;
            sum = sum + pow(rem, n);
            num = num / 10;
        }

        if(count == sum)
        {
            printf("%d is Armstrong number\n", count);
        }

        count++;
    }

    return 0;
}

Output:
Enter 2 integer numbers
1
99999

Armstrong numbers between 1 and 99999
1 is Armstrong number
2 is Armstrong number
3 is Armstrong number
4 is Armstrong number
5 is Armstrong number
6 is Armstrong number
7 is Armstrong number
8 is Armstrong number
9 is Armstrong number
153 is Armstrong number
370 is Armstrong number
371 is Armstrong number
407 is Armstrong number
1634 is Armstrong number
8208 is Armstrong number
9474 is Armstrong number
54748 is Armstrong number
92727 is Armstrong number
93084 is Armstrong 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

Calculate Power of a Number using pow(): C Program

Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another by using pow() method present in math.h library.

Related Read:
while loop in C programming
Calculate Power of a Number: C Program

One Number Raised To Another using pow(): C Program


[youtube https://www.youtube.com/watch?v=i-aop38U0Hg]

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


Source Code:One Number Raised To Another using pow(): C Program

#include < stdio.h >
#include < math.h >

int main()
{
    int base, exponent;

    printf("Enter base value\n");
    scanf("%d", &base);

    printf("Enter exponent value\n");
    scanf("%d", &exponent);

    printf("%d to the power of %d is %2f\n",
           base, exponent, pow(base, exponent));

    return 0;
}

Output:
Enter base value
25
Enter exponent value
3
25 to the power of 3 is 15625.000000

Note:
pow() method is present in math.h library, so we include it at the top of our c source code.

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

Calculate Power of a Number: C Program

Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another.

Related Read:
while loop in C programming

In this program, we ask the user to input values for base and exponent. We use while loop in this program. The while loop is executed “exponent” times. And inside while loop we multiply base value and output the result.

One Number Raised To Another: C Program


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

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


Source Code:One Number Raised To Another: C Program

#include < stdio.h >

int main()
{
    int base, exponent, res = 1, temp;

    printf("Enter the base\n");
    scanf("%d", &base);

    printf("Enter the exponent\n");
    scanf("%d", &exponent);

    temp = exponent;

    while(exponent)
    {
        res = res * base;
        exponent--;
    }

    printf("%d to the power of %d is %d\n", base, temp, res);

    return 0;
}

Output:
Enter the base
5
Enter the exponent
4
5 to the power of 4 is 625

Note:
1. We are initializing value of variable res to 1 in order to make sure it doesn’t have garbage value in it. If it was an addition operation we could have assigned it a initial value of 0. But in above program we are doing multiplication. So multiplying any number with 0 will give 0 as result. So we initialize the value of variable res to 1.

2. We modify the value of variable exponent in while loop. So to preserve the user entered value for exponent, we take another variable temp and store the original number(exponent value) entered by the user.

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