Write a program to accept three sides of the triangle check whether a triangle is possible or not

Last update on May 21 2022 13:07:40 (UTC/GMT +8 hours)

Write a C program to check whether three given lengths (integers) of three sides of a triangle form a right triangle or not. Print "Yes" if the given sides form a right triangle otherwise print "No".

Input: Integers separated by a single space.

1 <=>

Sample Solution:

C Code:

#include <stdio.h> int main() { int x,y,z; printf("Input the three sides of a trainagel:\n"); scanf("%d %d %d",&x,&y,&z); if((x*x)+(y*y)==(z*z) || (x*x)+(z*z)==(y*y) || (y*y)+(z*z)==(x*x) ) { printf("It is a right angle triangle!\n"); } else { printf("It is not a right angle triangle!\n"); } return 0; }

Sample Output:

Input the three sides of a trainagel: 12 11 13 It is not a right angle triangle!

Flowchart:

Write a program to accept three sides of the triangle check whether a triangle is possible or not

C programming Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C program to calculate the sum of two given integers and count the number of digits of the sum value.
Next: Write a C program which reads an integer n and find the number of combinations of a, b, c and d (0 <=>

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Maximum value of int:

In C:

#include <limits.h> then use int imin = INT_MIN; // minimum value int imax = INT_MAX;

or

#include <float.h> float fmin = FLT_MIN; // minimum positive value double dmin = DBL_MIN; // minimum positive value float fmax = FLT_MAX; double dmax = DBL_MAX;

Ref : https://bit.ly/3fi8yk9

Last update on May 16 2022 12:12:31 (UTC/GMT +8 hours)

Write a Java program to check if three given side lengths (integers) can make a triangle or not.

Pictorial Presentation:

Write a program to accept three sides of the triangle check whether a triangle is possible or not

Sample Solution:

Java Code:

import java.util.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Input side1: "); int s1 = in .nextInt(); System.out.print("Input side2: "); int s2 = in .nextInt(); System.out.print("Input side3: "); int s3 = in .nextInt(); System.out.print("Is the said sides form a triangle: " + isValidTriangle(s1, s2, s3)); } public static boolean isValidTriangle(int a, int b, int c) { return (a + b > c && b + c > a && c + a > b); } }

Sample Output:

Input side1: 5 Input side2: 6 Input side3: 8 Is the said sides form a triangle: true

Flowchart:

Write a program to accept three sides of the triangle check whether a triangle is possible or not

Java Code Editor:

Company:  LinkedIn

Contribute your code and comments through Disqus.

Previous: Write a Java program to find the all positions of a given number in a given matrix. If the number not found print ("Number not found!").
Next: Write a Java program to create a spiral array of n * n sizes from a given integer n.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

flattenDepth:

Flattens an array up to the specified depth.

public static Object[] flattenDepth(Object[] elements, int depth) { if (depth == 0) { return elements; } return Arrays.stream(elements) .flatMap(el -> el instanceof Object[] ? Arrays.stream(flattenDepth((Object[]) el, depth - 1)) : Arrays.stream(new Object[]{el}) ).toArray(); }

Ref: https://bit.ly/38QYcDI

  • Write a C program to enter three sides of a triangle and check whether a triangle is valid or not.


A triangle is a valid triangle, If and only If, the sum of any two sides of a triangle is greater than the third side.
For Example, let A, B and C are three sides of a triangle. Then, A + B > C, B + C > A and C + A > B.

C program to check whether a triangle is valid, given sides of triangle

/* * Given three sides of a triangle, Write a c program to * check whether it is a valid triangle or not */ #include <stdio.h> int main() { int side1, side2, side3; /* * Take length of three sides of triangle as input * from user using scanf */ printf("Enter Length of Three Sides of a Triangle\n"); scanf("%d %d %d", &side1, &side2, &side3); if((side1 + side2 > side3)&&(side2 + side3 > side1) &&(side3 + side1 > side2)) { printf("It is a Valid Triangle\n"); } else { printf("It is an invalid Triangle"); } return 0; }
Output Enter Length of Three Sides of a Triangle 10 20 20 It is a Valid Triangle Enter Length of Three Sides of a Triangle 10 20 40 It is an invalid Triangle
Related Topics


Page 2

  • Write a C program to check whether input triangle is equilateral, isosceles or scalene triangle using if else statement.


  • A triangle is equilateral triangle, If it's sides are equal.
  • A triangle is isosceles triangle, If any two sides of a triangle are equal.
  • A triangle is scalene triangle, If none of the sides are equal.

C program to check whether input triangle is equilateral, isosceles or scalene triangle

/* * Write a C program to check whether a Triangle is Equilateral, * Scalene or Isosceles */ #include <stdio.h> int main() { int side1, side2, side3; /* * Take length of three sides of triangle as input * from user using scanf */ printf("Enter Length of Three Sides of a Triangle\n"); scanf("%d %d %d", &side1, &side2, &side3); if((side1 == side2)&&(side2 == side3)) { /* All Sides Equal, Hence Equilateral */ printf("It is an Equilateral Triangle\n"); } else if (side1!=side2 && side2!=side3 && side3!=side1) { /* All sides different, Hence Scelene */ printf("It is a Scalene Triangle\n"); } else { /* Two sides equal, Hence Isoscales */ printf("It is an Isoscales Triangle\n"); } return 0; }
Output Enter Length of Three Sides of a Triangle 10 10 10 It is an Equilateral Triangle Enter Length of Three Sides of a Triangle 10 10 5 It is an Isoscales Triangle Enter Length of Three Sides of a Triangle 7 10 5 It is an Isoscales Triangle
Related Topics

Write a program to input the three sides of a triangle and check whether it forms a triangle or not, if it forms a triangle, check whether it is an equilateral, isosceles or a scalene triangle.

(Hint: To form a triangle, each side should be less the sum of the other two sides.

To form an equilateral triangle every side should be equal.

To form an isosceles triangle any two sides should be equal.

To form a scalene triangle all three sides should be different from each other.)

Given three sides, check whether triangle is valid or not. Examples: 

Input : a = 7, b = 10, c = 5 Output : Valid Input : a = 1 b = 10 c = 12 Output : Invalid

Approach: A triangle is valid if sum of its two sides is greater than the third side. If three sides are a, b and c, then three conditions should be met. 
 

1.a + b > c 2.a + c > b 3.b + c > a

bool checkValidity(int a, int b, int c)

    if (a + b <= c || a + c <= b || b + c <= a)

    int a = 7, b = 10, c = 5;

    if (checkValidity(a, b, c))

bool checkValidity(int a, int b, int c)

    if (a + b <= c || a + c <= b || b + c <= a)

    int a = 7, b = 10, c = 5;

    if (checkValidity(a, b, c))

    public static int checkValidity(int a, int b, int c)

        if (a + b <= c || a + c <= b || b + c <= a)

    public static void main(String args[])

        int a = 7, b = 10, c = 5;

        if ((checkValidity(a, b, c)) == 1)

            System.out.print("Valid");

            System.out.print("Invalid");

def checkValidity(a, b, c):

    if (a + b <= c) or (a + c <= b) or (b + c <= a) :

if checkValidity(a, b, c):

    public static int checkValidity(int a, int b,

        if (a + b <= c || a + c <= b ||

    public static void Main()

        int a = 7, b = 10, c = 5;

        if ((checkValidity(a, b, c)) == 1)

          Console.Write("Invalid");

function checkValidity($a, $b, $c)

    if (checkValidity($a, $b, $c))

function checkValidity(a, b, c)

    if (a + b <= c || a + c <= b || b + c <= a)

    let a = 7, b = 10, c = 5;

    if (checkValidity(a, b, c))

        document.write("Invalid");    

Output : 
 

Valid

Article Tags :

Practice Tags :