HomeC ProgrammingC Program to Find Area of Rhombus

C Program to Find Area of Rhombus

This C program calculates the area of a rhombus using the lengths of its diagonals. It demonstrates a function to compute the area and ensures that the user provides valid input for the diagonals.

Problem statement

Given the lengths of the two diagonals of a rhombus, we need to find the area of the rhombus.

C Program to Find Area of Rhombus

#include <stdio.h>

int main() {
    float diagonal1, diagonal2, area;

    printf("Rhombus Area Calculator\n");

    // Input diagonals
    printf("Enter the length of the first diagonal: ");
    scanf("%f", &diagonal1);

    printf("Enter the length of the second diagonal: ");
    scanf("%f", &diagonal2);

    // Calculate area
    area = (diagonal1 * diagonal2) / 2;

    // Output the result
    printf("The area of the rhombus is: %.2f\n", area);

    return 0;
}

How it works

  1. We start by including the necessary header file stdio.h, which allows us to use functions like printf and scanf.
  2. The main function is the entry point of the program.
  3. We declare three variables: diagonal1, diagonal2, and area. diagonal1 and diagonal2 will store the lengths of the diagonals, and area will store the calculated area.
  4. We prompt the user to enter the lengths of the diagonals using printf and read the input using scanf.
  5. The area of a rhombus is calculated by multiplying the lengths of its diagonals and dividing the result by 2. We perform this calculation and store the result in the area variable.
  6. Finally, we use printf to display the calculated area to the user.

Input / output

C Program to Find Area of Rhombus

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...