Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
C

write c program to Find the Area of a Triangle

Reviewed & accurate
AI Summary
C Program to Calculate Area of Triangle | Easy Guide

C Program to Calculate the Area of a Triangle

Want to learn how to calculate the area of a triangle using C? In this beginner-friendly tutorial, we will create a simple C program to calculate the area of a triangle using its base and height.

You do not need advanced programming knowledge to understand this example. We will start with the triangle formula and then explain the C program step by step.

What Is the Area of a Triangle?

The area of a triangle tells us how much space is inside the triangle. To calculate it, we need two values:

  • The base of the triangle
  • The height of the triangle

The standard formula for calculating the area of a triangle is:

Area = ½ × Base × Height

In a C program, we can write one-half as 0.5.

Example:

Suppose the base is 10 and the height is 5.

Area = ½ × 10 × 5

Area = 25

Therefore, the area of the triangle is 25 square units.

C Program to Calculate the Area of a Triangle

Here is the complete C program:

area_triangle.c
#include <stdio.h>

int main()
{
    float base, height, area;

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

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

    // Calculate the area of the triangle
    area = 0.5 * base * height;

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

    return 0;
}

How Does This C Program Work?

The program follows a simple process:

Input → Calculate → Display

First, the program asks the user for the base. Then it asks for the height. After receiving both values, it calculates the area and displays the answer.

Step 1: Include the Standard Input and Output Library

area_triangle.c
#include <stdio.h>

The stdio.h header file provides standard input and output functions in C.

We need it because our program uses:

  • printf() to display messages.
  • scanf() to read values entered by the user.

Step 2: Create the main() Function

area_triangle.c
int main()
{
}

The main() function is the starting point of a C program. When we run the program, execution begins inside this function.

Step 3: Declare the Variables

area_triangle.c
float base, height, area;

We create three variables:

  • base stores the base of the triangle.
  • height stores the height of the triangle.
  • area stores the calculated area.

We use the float data type because the values can contain decimal numbers.

For example, the user could enter a base of 10.5 and a height of 6.25.

Step 4: Ask the User for the Base

area_triangle.c
printf("Enter the base of the triangle: ");

The printf() function displays a message on the screen.

The user will see something like:

Enter the base of the triangle:

Step 5: Read the Base Using scanf()

area_triangle.c
scanf("%f", &base);

The scanf() function reads the value entered by the user and stores it in the base variable.

The %f format specifier tells C that we are reading a floating-point number.

The &base tells scanf() where the entered value should be stored.

Step 6: Ask for the Height

area_triangle.c
printf("Enter the height of the triangle: ");

The program now asks the user to enter the height of the triangle.

Step 7: Read the Height

area_triangle.c
scanf("%f", &height);

The value entered by the user is stored in the height variable.

Step 8: Calculate the Area

This is the most important calculation in the program:

area_triangle.c
area = 0.5 * base * height;

This statement applies the triangle area formula:

Area = ½ × Base × Height

In C, 0.5 represents one-half.

For example, if the base is 10 and the height is 5:

area = 0.5 × 10 × 5

area = 25

Step 9: Display the Result

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

This line displays the calculated area.

The %.2f format specifier tells C to display the floating-point value with two digits after the decimal point.

For example:

25 becomes 25.00

15.756 becomes approximately 15.76

Step 10: End the Program

area_triangle.c
return 0;

The return 0; statement tells the operating system that the program finished successfully.

Example Output

Suppose the user enters 12 as the base and 8 as the height.

Enter the base of the triangle: 12 Enter the height of the triangle: 8 The area of the triangle is: 48.00

Another Example with Decimal Values

The program can also work with decimal values.

Base: 7.5

Height: 4.2

Area = 0.5 × 7.5 × 4.2

Area = 15.75

Why Do We Use float in This Program?

We use the float data type because triangle measurements are not always whole numbers.

A triangle might have measurements such as:

  • Base = 10.5
  • Height = 7.25
  • Base = 12.75

Using float allows our program to work with these decimal values.

Common Mistakes Beginners Make

1. Forgetting the & Symbol

Incorrect:

wrong.c
scanf("%f", base);

Correct:

correct.c
scanf("%f", &base);

2. Using the Wrong Format Specifier

Because base and height are declared as float, we use %f with scanf().

correct.c
float base;

scanf("%f", &base);

3. Forgetting the Semicolon

Most C statements end with a semicolon.

area_triangle.c
float base, height, area;

area = 0.5 * base * height;

What You Learn From This Program

Although this is a small program, it teaches several important C programming concepts.

  • Variables – store information.
  • float – stores decimal numbers.
  • printf() – displays information.
  • scanf() – accepts user input.
  • Arithmetic operators – perform calculations.
  • main() – starting point of the program.

Simple Program Flow

Start → Enter Base → Enter Height → Calculate Area → Display Result → End

Conclusion

Writing a C program to calculate the area of a triangle is a simple and useful exercise for beginners.

The program takes the base and height from the user, applies the formula Area = ½ × Base × Height, and displays the final result.

By understanding this small example, you are also learning some of the most important C programming basics, including variables, data types, user input, output, and arithmetic operations.

Once you understand this program, try creating similar programs to calculate the area of a rectangle, circle, or other geometric shapes.

Frequently Asked Questions

How do you calculate the area of a triangle in C?

Use the formula Area = ½ × Base × Height. In C, this can be written as:

area_triangle.c
area = 0.5 * base * height;

Which data type is used to calculate the area of a triangle in C?

The float data type is useful when the base, height, or area can contain decimal values.

What does %.2f mean in C?

%.2f tells the printf() function to display a floating-point number with two digits after the decimal point.

Can this C program accept decimal values?

Yes. The program uses float variables, so it can accept decimal values such as 10.5, 7.25, and 4.75.

What formula is used to find the area of a triangle?

The formula is Area = ½ × Base × Height.

Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments