Header Ads Widget

C++ Program to take the value from the user as input all sides of a triangle and check whether the triangle is valid or not. Using switch statement

 

 C++  Program to take the value from the user as input all sides of a triangle and check whether the triangle is valid or not. Using switch statement 

Given sides of the triangle , write a program to check whether it will form a valid triangle or not . For triangle to be valid the sum of two sides must be greater than third . 

Note:- 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  

Sample input 

1 10 12

Sample output

Invalid triangle

C++  Program to take the value from the user as input all sides of a triangle and check whether the triangle is valid or not. Using switch statement 

The objective of the code is to check that sides of the triangle will form a valid triangle or not . 

Code(C++):-

#include <iostream>
using namespace std;
int main() {
       int a,b,c,s;
cout<<"Enter the sides ";
cin>>a>>b>>c;
s=a+b+c;
int ch=((a+b)>c && (b+c)>a && (c+a)>b);
switch(ch)
{
case 1:cout<<"Valid Triangle";
break;
case 0:cout<<"Invalid triangle";
break;
}
}

Output:-

Enter the sides
1 10 12
Invalid triangle

Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-