Since you don't know if/else statements clearly here is a quick on-the-spot guide/tutorial:
if and else statements are used when the expected input/output may be different each time and you want your program to act differently depending on the value. The ordering of if/else statements are as the following:
use if for the first expected value or the value to check for first, followed by else if for the second expected value or the value to check for if the first one is false and else at the last when all if/else if fails.
And here is a sample:
Code:
#include <stdio.h>
int main()
{
int userAge;
printf("Enter your age: ");
scanf("%d", &userAge);
if(userAge < 13)
{
printf("You are a kid!\n");
}
else if(userAge >= 13 && userAge < 18)
{
printf("You are a teen but not an adult.\n");
}
else if(userAge >= 18 && userAge < 20)
{
printf("You are a teen as well as an adult.\n");
}
else
{
printf("You are an adult and not a teen.");
}
getch();
return 0;
}
And now coming to your code.
Why are you putting the if/else if/else construct inside curly braces? Its not necessary.
Code:
{//No need this
if (SideA*SideA + SideB*SideB == SideC*SideC){
printf("This is a right-angled triangle.\n");
}
else if (SideC*SideC < SideA*SideA + SideB*SideB){
printf("This is an acute-angled triangle.\n");
}
else if(SideC*SideC > SideA*SideA + SideB*SideB){
printf("This is an obtuse-angled triangle.\n");
}
else {
printf("This is not a triangle\n");
}//No need this
Also, use 4 spaces inside the body of the if/else if/else statements. And you might want to use double/float instead of int so you can enter decimal lengths.
Try this code:
Code:
#include <stdio.h>
void flush_buf(void); //Function that stops crashing if input is different from variable type
int main()
{
float A, B, C;
printf("Enter side A: ");
scanf("%f", &A);
flush_buf();
printf("Enter side B: ");
scanf("%f", &B);
flush_buf();
printf("Enter side C: ");
scanf("%f", &C);
flush_buf();
printf("\nA: %.2f, B: %.2f, C: %.2f\n", A, B, C);
if((int)A == 0 || (int)B == 0 || (int)C == 0) //Check whether user entered 0/letters
{
printf("\nNot a valid triangle.\n");
}
else if(A*A + B*B == C*C)
{
printf("\nThe triangle is a right-angled triangle.\n");
}
else if(A*A + B*B > C*C)
{
printf("\nThe triangle is an acute-angled triangle.\n");
}
else if(A*A + B*B < C*C)
{
printf("\nThe triangle is an obtuse-angled triangle.\n");
}
getch();
return 0;
}
void flush_buf(void)
{
while(getchar() != '\n');
}