C++ Online Compiler
Example: Triangle Angle Validator in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Triangle Angle Validator #include <iostream> int main() { // Step 1: Declare three variables to store the angles double angle1, angle2, angle3; // Step 2: Prompt the user to enter the three angles std::cout << "Enter the first angle: "; std::cin >> angle1; std::cout << "Enter the second angle: "; std::cin >> angle2; std::cout << "Enter the third angle: "; std::cin >> angle3; // Step 3: Calculate the sum of the three angles double sumOfAngles = angle1 + angle2 + angle3; // Step 4: Check if the sum is approximately 180 and all angles are positive // Using a small epsilon for floating-point comparison is good practice, // but for exact integer angles, direct comparison might suffice. // For general floating-point input, comparison with a tolerance is better. // Here we'll assume precise input for simplicity. if (sumOfAngles == 180.0 && angle1 > 0 && angle2 > 0 && angle3 > 0) { std::cout << "The entered angles (" << angle1 << ", " << angle2 << ", " << angle3 << ") form a valid triangle." << std::endl; } else { std::cout << "The entered angles (" << angle1 << ", " << angle2 << ", " << angle3 << ") do not form a valid triangle." << std::endl; if (sumOfAngles != 180.0) { std::cout << "Reason: Sum of angles is " << sumOfAngles << ", which is not 180." << std::endl; } if (angle1 <= 0 || angle2 <= 0 || angle3 <= 0) { std::cout << "Reason: One or more angles are not positive." << std::endl; } } return 0; }
Output
Clear
ADVERTISEMENTS