×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to calculate the area of the rhombus

Here, we are going to learn how to calculate the area of the rhombus using C program?
Submitted by Nidhi, on August 05, 2021

Problem statement

Read both diagonals of the rhombus from the user, and then calculate the area of the rhombus.

Area of the rhombus formula = ½ × d1 × d2

Where d1 is the length of diagonal 1 and d2 is the length of diagonal 2.

C program to calculate the area of the rhombus

The source code to calculate the area of the rhombus is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to calculate the area of rhombus

#include <stdio.h>

float calcuateAreaOfRhombus(float diagonal1, float diagonal2)
{
    float result = 0.0F;

    result = 0.5 * diagonal1 * diagonal2;
    return result;
}

int main()
{
    float diagonal1 = 0;
    float diagonal2 = 0;
    float area = 0;

    printf("Enter the value of first diagonal: ");
    scanf("%f", &diagonal1);

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

    area = calcuateAreaOfRhombus(diagonal1, diagonal2);

    printf("Area of rhombus is: %f\n", area);

    return 0;
}

Output

RUN 1:
Enter the value of first diagonal: 12.3
Enter the value of second diagonal: 25.5
Area of rhombus is: 156.824997

RUN 2:
Enter the value of first diagonal: 1.2
Enter the value of second diagonal: 2.3
Area of rhombus is: 1.380000

RUN 3:
Enter the value of first diagonal: 123.45
Enter the value of second diagonal: 0.1
Area of rhombus is: 6.172500

Explanation

In the above program, we created two functions calcuateAreaOfRhombus() and main(). The calcuateAreaOfRhombus() function is used to calculate the area of the rhombus based on given diagonals.

In the main() function, read the values of diagonals of the rhombus from the user. Then we called the calcuateAreaOfRhombus() function to calculate the area of the rhombus. After that, we printed the result on the console screen.

C Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.