Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C Exercises: Check a parallelogram is a rectangle or a rhombus

C Basic Declarations and Expressions: Exercise-142 with Solution

Write a C program which reads the two adjoined sides and the diagonal of a parallelogram and check whether the parallelogram is a rectangle or a rhombus.

According to Wikipedia-
parallelograms: In Euclidean geometry, a parallelogram is a simple (non-self-intersecting) quadrilateral with two pairs of parallel sides. The opposite or facing sides of a parallelogram are of equal length and the opposite angles of a parallelogram are of equal measure.
rectangles: In Euclidean plane geometry, a rectangle is a quadrilateral with four right angles. It can also be defined as an equiangular quadrilateral, since equiangular means that all of its angles are equal (360°/4 = 90°). It can also be defined as a parallelogram containing a right angle.
rhombus: In plane Euclidean geometry, a rhombus (plural rhombi or rhombuses) is a simple (non-self-intersecting) quadrilateral whose four sides all have the same length. Another name is equilateral quadrilateral, since equilateral means that all of its sides are equal in length. The rhombus is often called a diamond, after the diamonds suit in playing cards which resembles the projection of an octahedral diamond, or a lozenge, though the former sometimes refers specifically to a rhombus with a 60° angle (see Polyiamond), and the latter sometimes refers specifically to a rhombus with a 45° angle.
Input:
Two adjoined sides and the diagonal.
1 <= ai, bi, ci <= 1000, ai + bi > ci

Sample Solution:

C Code:

#include <stdio.h>
main() {
  int h1, h2, t;
  int rect = 0;
  int hisi = 0;

  rect = 0;
  hisi = 0;
  printf("Input two adjoined sides of the parallelogram:\n");
  scanf("%d", & h1);
  scanf("%d", & h2);
  printf("\nInput the diagonal of the parallelogram:\n");
  scanf("%d", & t);
  if (t * t == h1 * h1 + h2 * h2)
    rect++;
  if (h1 == h2)
    hisi++;

  if (rect > 0)
    printf("\nThis is a rectangle.");
  if (hisi > 0)
    printf("\nThis is a rhombus.");

  return (0);
}

Sample Output:

Input two adjoined sides of the parallelogram:
3
4

Input the diagonal of the parallelogram:
5

This is a rectangle.

Sample Output:

Input two adjoined sides of the parallelogram:
5
5

Input the diagonal of the parallelogram:
7

This is a rhombus.

Flowchart:

C Programming Flowchart: Check a parallelogram is a rectangle or a rhombus.

C programming Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C program that reads n digits (given) chosen from 0 to 9 and prints the number of combinations where the sum of the digits equals to another given number (s). Do not use the same digits in a combination.
Next: Write a C program to find the difference between the largest integer and the smallest integer, which are created by 8 numbers from 0 to 9. The number that can be rearranged shall start with 0 as in 00135668.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP