Lets write a C program to calculate area of a right angled Triangle, by asking the user to enter its width and height.
Related Read:
Find Area of a Triangle Using Its Sides: C Program
Find Area of a Triangle Using Its Base and Height: C Program
Page Contents
Formula To Find Area of Right Angled Triangle
Area = (width * Height) / 2.0;
OR
Area = (width * Height * 0.5);
Note: If we divide an expression or number by 2, it’ll return only the integer part and the decimal part will be discarded. So we are dividing the expression by 2.0 (which is of type double).
C program To Find Area of Right Angled Triangle
[youtube https://www.youtube.com/watch?v=Vjf7zgwiXU8]
Source Code: C program To Find Area of Right Angled Triangle
#include < stdio.h > int main() { float h, w, area; printf("Enter height and width of a right angled triangle\n"); scanf("%f%f", &h, &w); area = (h * w) / 2.0; printf("Area of a Right Angled Triangle is %f\n", area); return 0; }
Output:
Enter height and width of a right angled triangle
10
5
Area of a Right Angled Triangle is 25.000000
Validate the Input and Find Area of Triangle: C Program
#include < stdio.h > int main() { float h, w, area; printf("Enter height and width of a right angled triangle\n"); scanf("%f%f", &h, &w); if(w == 0 || h == 0) { printf("Invalid Input\n"); } else { area = (h * w) / 2.0; printf("Area of a Right Angled Triangle is %f\n", area); } return 0; }
Output 1:
Enter height and width of a right angled triangle
10
5
Area of a Right Angled Triangle is 25.000000
Output 2:
Enter height and width of a right angled triangle
0
5
Invalid Input
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert