Program to check if a rectangle is a square or not
Last Updated :
17 Jan, 2024
Given the dimensions of a rectangle (length and breadth), write a program to determine whether the given rectangle is a square or not.
Examples:
Input: Length = 5, Breadth = 5
Output: Square
Explanation: Since the length and breadth of the rectangle is same, therefore the given rectangle is a square.
Input: Length = 6, Breadth = 8
Output: Rectangle
Explanation: Since the length and breadth are different, therefore it is a rectangle.
Approach: To solve the problem, follow the below idea:
If the length and breadth of a rectangle are equal, it is a square. Otherwise, it is a rectangle.
Step-by-step algorithm:
- Take the input for length and breadth.
- Check if length equals breadth.
- If true, print “Square.” Otherwise, print “Not a Square.”
Below is the implementation of the algorithm:
C++
#include <iostream>
using namespace std;
int main() {
int length, breadth;
length = 5;
breadth = 5;
if (length == breadth)
cout << "Square" << endl;
else
cout << "Not a Square" << endl;
return 0;
}
|
C
#include <stdio.h>
int main() {
int length, breadth;
length = 5;
breadth = 5;
if (length == breadth)
printf ( "Square.\n" );
else
printf ( "Not a Square.\n" );
return 0;
}
|
Java
public class SquareChecker {
public static void main(String[] args) {
int length = 5 ;
int breadth = 5 ;
if (length == breadth)
System.out.println( "Square." );
else
System.out.println( "Not a Square." );
}
}
|
Python3
length, breadth = 5 , 5
if length = = breadth:
print ( "Square." )
else :
print ( "Not a Square." )
|
C#
using System;
class Program {
static void Main() {
int length = 5;
int breadth = 5;
if (length == breadth)
Console.WriteLine( "Square." );
else
Console.WriteLine( "Not a Square." );
}
}
|
Javascript
let length = 5;
let breadth = 5;
if (length === breadth)
console.log( "Square." );
else
console.log( "Not a Square." );
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...