Program
We know that the Pythagorean is an equation which satisfies the following condition:
a2 + b2 = c2
Write a program with Matlab, which takes the user input of the numbers and checks if the above condition is satisfied. If the condition is satisfied then display the success to the user.
Right angle triangle |
Solution
We take the user input and then use the if else statement to check if the user has entered three number which are in accordance to the Pythagorean theorem and display the success using the fprintf in Matlab.
Some of the concepts you need to know to solve the above problem are as follows:
1) User input using input function
2) if else statement in Matlab
3) fprintf in Matlab
1) User input using input function
2) if else statement in Matlab
3) fprintf in Matlab
Program
1 2 3 4 5 6 7 8 9 | a = input('Enter length of Opposite side(a): '); b = input('Enter the length of Adjacent side(b): '); c = input('Enter the length of Hypotenuse(c): '); if a^2 + b^2 == c^2 fprintf('Thats a pytagorean triplet\n'); else fprintf('Sorry not a pythagorean triplet\n'); end |
Explanation
I have asked the user to input their values for the length of the sides of the triangle and using the if else I have checked the Pythagorean condition and if the condition is satisfied then display the user that the triplet entered is Pythagorean triplet. Otherwise(else), display not a Pythagorean triplet.
Try it yourself
1) In the above program even if the user enters negative values it checks the values for the pythagorean triplet. But this is not in the real case. Write a program such that if the user enters negative values then display that they have entered invalid numbers. Comment your program in the comment box below.
Input/output
Check the Pythagorean triplet using Matlab |
Final Note
As always please do contact me if you have any doubt, suggestion, or did not understand anything. You can also comment in the comment box below and I will respond to you as soon as possible. You can contact me from here: Contact me
The above program was high lighted using hilite.me
Keywords: pythagorean. triplet