HomeC ProgrammingC Program to Find the Area of a Triangle

C Program to Find the Area of a Triangle

This C program calculates the area of a triangle using the formula: area = 0.5 * base * height.

Problem statement

Write a C program to find the area of a triangle using the given base length and height.

C Program to Find the Area of a Triangle

#include <stdio.h>

int main() {
    printf("Program to calculate the area of a triangle\n");

    float base, height;
    printf("Enter the base length of the triangle: ");
    scanf("%f", &base);

    printf("Enter the height of the triangle: ");
    scanf("%f", &height);

    float area = 0.5 * base * height;

    printf("The area of the triangle is: %f\n", area);

    return 0;
}

How it works

  1. The program starts with an introduction message.
  2. It declares two variables: base and height to store the base length and height of the triangle, respectively.
  3. It prompts the user to enter the base length of the triangle.
  4. The user enters the base length, which is read using the scanf function and stored in the variable base.
  5. It prompts the user to enter the height of the triangle.
  6. The user enters the height, which is read using the scanf function and stored in the variable height.
  7. The program calculates the area of the triangle using the formula area = 0.5 * base * height and stores the result in the variable area.
  8. Finally, it displays the area of the triangle using the printf function.

Input / output

C Program to Find the Area of a Triangle

Share:

Leave A Reply

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

You May Also Like

This C program calculates the volume and surface area of a sphere using its radius. A sphere is a three-dimensional...
This C program converts a Roman numeral to a decimal number. Roman numerals are a system of numerical notation used...
This C program calculates the value of sin(x) using the Taylor series expansion. The Taylor series expansion is a mathematical...