C LanguageProgramming

C Program for Right-Angle Triangle (Pythagoras Theorem)

Right-Angle Triangle
Spread the love

In this article, I am giving you a c program for the right-angle triangle in which we will use the Pythagoras theorem to check whether the triangle is right-angled or not. It is a dynamic program which means you can take any value. Before you go to the program firstly you have to know some basics of C language which will help you to understand the program.

part 1
part 2

Above are two basic videos on C (in Hindi) watch both of them then go through the program part. Also, watch the video where I told about all operators of c language from here.

Program for Right-Angle Triangle

#include<stdio.h>
main()
{
	int hyp, perp, base;  //thre integer variables
	int temp, temp2;  //two variables to store values
	printf("Enter hypotenuse");
	scanf("%d", &hyp);  //hypotenuse input
	printf("enter perpendicular");
	scanf("%d", &perp);  //perpendicular input
	printf("Enter base");
	scanf("%d", &base);  //base input
	//H sq = P sq + Bsq
	temp = (perp*perp)+(base*base);  //perp and base square sum store here
	temp2 = hyp*hyp;  //hyp square stored here
	if(temp==temp2) //pythagoras theorem
	{
		printf("Triangle is right angled triangle");
	}
	else
	{
		printf("Not");
	}
}

Explanation

  • We are starting with the header files and the main function. Inside the curly brackets of the main function, we have to write our whole program code.
#include<stdio.h>
main()
{ //program code }
  • In the fourth line, I have taken three integer variables to store the values of hypotenuse, perpendicular, and base.
int hyp, perp, base;
  • In the fifth line, I have taken two integer variables to store the left-hand side and right-hand side of the Pythagoras theorem.
int temp, temp2
  • ¬†From sixth to the eleventh line I have just passed messages in printf statements to enter numbers (perpendicular, base, and hypotenuse) and storing the input entered by the user in variable’s memory location.
printf("Enter hypotenuse");
scanf("%d", &hyp);  //hypotenuse input
printf("enter perpendicular");
scanf("%d", &perp);  //perpendicular input
printf("Enter base");
scanf("%d", &base);  //base input
  • In the 12th line, I have passed the comment which shows the Pythagoras theorem (Hypotenuse2  =  Perpendicular2 + Base2 )
//H sq = P sq + Bsq
  • In the 13th line, I have stored the sum of squares of perpendicular and base (Perpendicular2 + Base2) in the temp variable.
temp = (perp*perp)+(base*base);
  • In the 14th line, I have stored hypotenuse square in the temp2 variable.
temp2 = hyp*hyp;
  • In the 15th lin, I am comparing the values of temp and temp2 variable. If both of them are equal that means Pythagoras theorem is verified. So I have passed the message that the triangle is a right-angled triangle.
  • If both values are not equal then else part will be executed.
if(temp==temp2) //pythagoras theorem
{
	printf("Triangle is right angled triangle");
}
else
{
        printf("Not");
}

Program pdf: Download


Spread the love
Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.