Q:

C program to calculate the area of the rhombus

0

C program to calculate the area of the rhombus

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

Program:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C programming Basic Input, Output, if else, Ternary Operator based Programs

Similar questions


need a help?


find thousands of online teachers now
C program to calculate the area of Parallelogram... >>
<< C program to calculate the area of Trapezium...