Skip to main content

Check Pythagorean triplet using Matlab

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.
Image of a right angle triangle - pythagorean triplet theorem - (c) Wikipedia
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

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
I have saved this file as pythagorean.m on my local computer.

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 if the user has entered pythagorean triplet, using Matlab - Matlab session
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

Popular posts from this blog

Project Euler Problem 67 Solution with Python

Maximum path sum II By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom in triangle.txt (right click and 'Save Link/Target As...'), a 15K text file containing a triangle with one-hundred rows.

Making a quiz web app with python and flask

Edit : When you are creating a web app with h tml templates, then y ou will have to sa ve the html file in templates folder in the Current Wor ki ng Directory( CWD). If you save the file in the C W D directl y you will get a TemplateNotFound error. Thank you Udhay for pointing it out.   In this post we will create a quiz website using python . I will be using the flask framework . After reading this tutorial you will learn form submission , flask templates , python code in flask templates , shuffling the questions and options with the random module and few others.  Please note that this tutorial is not big as it seems to be. Some of the code has been rewritten to maintain consistency and also font size is somewhat big so that your eyes won't get stressed reading this tutorial. Also the content has not occupied the full width of the page. In this tutorial I am assuming that you are having a very basic understanding of the flask framework . Please refer the documenta...

Project Euler Problem 66 Solution with python

Diophantine equation ¶ Consider quadratic Diophantine equations of the form: $$ x^{2} – Dy^{2} = 1 $$ For example, when $D = 13$, the minimal solution in $ x $ is $ 649^{2} – 13 \times 180^{2} = 1 $ It can be assumed that there are no solutions in positive integers when $ D $ is square. By finding minimal solutions in $ x $ for $ D = {2, 3, 5, 6, 7} $, we obtain the following: $$ 3^{2} – 2×2^{2} = 1 $$ $$ 2^{2} – 3×1^{2} = 1 $$ $$ 9^{2} – 5×4^{2} = 1 $$ $$ 5^{2} – 6×2^{2} = 1 $$ $$ 8^{2} – 7×3^{2} = 1 $$ Hence, by considering minimal solutions in $ x $ for $ D ≤ 7 $, the largest $ x $ is obtained when $ D = 5 $. Find the value of $ D ≤ 1000 $ in minimal solutions of $ x $ for which the largest value of $ x $ is obtained.