ADVERTISEMENT
ADVERTISEMENT

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 Solution:

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.

C Basic Programs »


ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.