Check if it's a square or rectangle
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I want to check in my if and else if statement if the inputs form a square or a rectangle, now my if and else if statements seems to be unreadable, now I'm thinking of ways to improve it.
/* input are angles in degrees, 1st side is parallel to 3rd side, 2nd side is parallel to 4th side*/
static void Main(string args)
Console.Write("Input first: ");
int first = int.Parse(Console.ReadLine());
Console.Write("Input second: ");
int second = int.Parse(Console.ReadLine());
Console.Write("Input third: ");
int third = int.Parse(Console.ReadLine());
Console.Write("Input fourth: ");
int fourth = int.Parse(Console.ReadLine());
// all sides are equal
// logic for if and else-if, needs codereview for improvement
if (first == second && first == third && first == fourth && second == third && second == fourth && third == fourth)
Console.WriteLine("Square");
else if (first != second && first == third && first != fourth && second != third && second == fourth )
Console.WriteLine("Rectangle");
else
Console.WriteLine("Not a square nor a rectangle");
Console.ReadKey();
c#
New contributor
add a comment |Â
up vote
1
down vote
favorite
I want to check in my if and else if statement if the inputs form a square or a rectangle, now my if and else if statements seems to be unreadable, now I'm thinking of ways to improve it.
/* input are angles in degrees, 1st side is parallel to 3rd side, 2nd side is parallel to 4th side*/
static void Main(string args)
Console.Write("Input first: ");
int first = int.Parse(Console.ReadLine());
Console.Write("Input second: ");
int second = int.Parse(Console.ReadLine());
Console.Write("Input third: ");
int third = int.Parse(Console.ReadLine());
Console.Write("Input fourth: ");
int fourth = int.Parse(Console.ReadLine());
// all sides are equal
// logic for if and else-if, needs codereview for improvement
if (first == second && first == third && first == fourth && second == third && second == fourth && third == fourth)
Console.WriteLine("Square");
else if (first != second && first == third && first != fourth && second != third && second == fourth )
Console.WriteLine("Rectangle");
else
Console.WriteLine("Not a square nor a rectangle");
Console.ReadKey();
c#
New contributor
2
Why have tags of linq and functional-programming? You aren't using either of them. As a side note you can simplify your square if statement no need to check the if 2nd = 3rd & 4th and 3rd = 4th since you checked the 1st equal to all sides the others ones are redundant
â CharlesNRice
2 hours ago
maybe there's an easier way to do this via linq? perhaps...
â Randel Ramirez
2 hours ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to check in my if and else if statement if the inputs form a square or a rectangle, now my if and else if statements seems to be unreadable, now I'm thinking of ways to improve it.
/* input are angles in degrees, 1st side is parallel to 3rd side, 2nd side is parallel to 4th side*/
static void Main(string args)
Console.Write("Input first: ");
int first = int.Parse(Console.ReadLine());
Console.Write("Input second: ");
int second = int.Parse(Console.ReadLine());
Console.Write("Input third: ");
int third = int.Parse(Console.ReadLine());
Console.Write("Input fourth: ");
int fourth = int.Parse(Console.ReadLine());
// all sides are equal
// logic for if and else-if, needs codereview for improvement
if (first == second && first == third && first == fourth && second == third && second == fourth && third == fourth)
Console.WriteLine("Square");
else if (first != second && first == third && first != fourth && second != third && second == fourth )
Console.WriteLine("Rectangle");
else
Console.WriteLine("Not a square nor a rectangle");
Console.ReadKey();
c#
New contributor
I want to check in my if and else if statement if the inputs form a square or a rectangle, now my if and else if statements seems to be unreadable, now I'm thinking of ways to improve it.
/* input are angles in degrees, 1st side is parallel to 3rd side, 2nd side is parallel to 4th side*/
static void Main(string args)
Console.Write("Input first: ");
int first = int.Parse(Console.ReadLine());
Console.Write("Input second: ");
int second = int.Parse(Console.ReadLine());
Console.Write("Input third: ");
int third = int.Parse(Console.ReadLine());
Console.Write("Input fourth: ");
int fourth = int.Parse(Console.ReadLine());
// all sides are equal
// logic for if and else-if, needs codereview for improvement
if (first == second && first == third && first == fourth && second == third && second == fourth && third == fourth)
Console.WriteLine("Square");
else if (first != second && first == third && first != fourth && second != third && second == fourth )
Console.WriteLine("Rectangle");
else
Console.WriteLine("Not a square nor a rectangle");
Console.ReadKey();
c#
c#
New contributor
New contributor
edited 45 mins ago
Henrik Hansen
5,2481620
5,2481620
New contributor
asked 2 hours ago
Randel Ramirez
1084
1084
New contributor
New contributor
2
Why have tags of linq and functional-programming? You aren't using either of them. As a side note you can simplify your square if statement no need to check the if 2nd = 3rd & 4th and 3rd = 4th since you checked the 1st equal to all sides the others ones are redundant
â CharlesNRice
2 hours ago
maybe there's an easier way to do this via linq? perhaps...
â Randel Ramirez
2 hours ago
add a comment |Â
2
Why have tags of linq and functional-programming? You aren't using either of them. As a side note you can simplify your square if statement no need to check the if 2nd = 3rd & 4th and 3rd = 4th since you checked the 1st equal to all sides the others ones are redundant
â CharlesNRice
2 hours ago
maybe there's an easier way to do this via linq? perhaps...
â Randel Ramirez
2 hours ago
2
2
Why have tags of linq and functional-programming? You aren't using either of them. As a side note you can simplify your square if statement no need to check the if 2nd = 3rd & 4th and 3rd = 4th since you checked the 1st equal to all sides the others ones are redundant
â CharlesNRice
2 hours ago
Why have tags of linq and functional-programming? You aren't using either of them. As a side note you can simplify your square if statement no need to check the if 2nd = 3rd & 4th and 3rd = 4th since you checked the 1st equal to all sides the others ones are redundant
â CharlesNRice
2 hours ago
maybe there's an easier way to do this via linq? perhaps...
â Randel Ramirez
2 hours ago
maybe there's an easier way to do this via linq? perhaps...
â Randel Ramirez
2 hours ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
Simplify the if by just doing
if (first == second && second == third && third == fourth)
....
You said the inputs are degrees, now itâÂÂs been awhile since geometry but squares and rectangles are determined by length of side, not angle degree. Both are 4 sided objects so every angle is the same (unless youâÂÂre talking about something like a trapezoid, rhombus).
New contributor
For completeness, this optimizes theSquare
check. Not theRectangle
.
â Mast
28 mins ago
add a comment |Â
up vote
1
down vote
I assume, that all inputs are length of sides and that the sides are entered in consecutive clock- or counterclockwise order.
Console.Write("Input first: ");
int first = int.Parse(Console.ReadLine());
Console.Write("Input second: ");
int second = int.Parse(Console.ReadLine());
Console.Write("Input third: ");
int third = int.Parse(Console.ReadLine());
Console.Write("Input fourth: ");
int fourth = int.Parse(Console.ReadLine());
Repetitive code should be a no-go in programming.
The if-statements are checking too much. A rectangle has pairwise equal sides (1 == 3 and 2 == 4)
and a square is a rectangle where the two pair of sides are also equal in length (rectangle == true && (1 == 2))
All in all that could be:
void CheckForRectangleAndSqare()
const int numSides = 4;
int sides = new int[numSides];
for (int i = 0; i < numSides; i++)
do
Console.Write($"Enter side no i + 1: ");
while (!int.TryParse(Console.ReadLine(), out sides[i]));
// Check for Rectangle
if (sides[0] == sides[2] && sides[1] == sides[3])
// Check for a square
if (sides[0] == sides[1])
Console.WriteLine("It's a Square");
else
Console.WriteLine("It's a Recangle");
else
Console.WriteLine("It's neither a Square nor a Rectangle");
If the sides are entered arbitrarily and you can combine them as you want, you can do:
void CheckForRectangleAndSqare()
const int numSides = 4;
int sides = new int[numSides];
for (int i = 0; i < numSides; i++)
do
Console.Write($"Enter side no i + 1: ");
while (!int.TryParse(Console.ReadLine(), out sides[i]));
var sideGroups = sides.GroupBy(s => s).Count();
if (sideGroups == 1)
Console.WriteLine("It's a Square");
else if (sideGroups == 2)
Console.WriteLine("It's a Recangle");
else
Console.WriteLine("It's neither a Square nor a Rectangle");
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Simplify the if by just doing
if (first == second && second == third && third == fourth)
....
You said the inputs are degrees, now itâÂÂs been awhile since geometry but squares and rectangles are determined by length of side, not angle degree. Both are 4 sided objects so every angle is the same (unless youâÂÂre talking about something like a trapezoid, rhombus).
New contributor
For completeness, this optimizes theSquare
check. Not theRectangle
.
â Mast
28 mins ago
add a comment |Â
up vote
4
down vote
accepted
Simplify the if by just doing
if (first == second && second == third && third == fourth)
....
You said the inputs are degrees, now itâÂÂs been awhile since geometry but squares and rectangles are determined by length of side, not angle degree. Both are 4 sided objects so every angle is the same (unless youâÂÂre talking about something like a trapezoid, rhombus).
New contributor
For completeness, this optimizes theSquare
check. Not theRectangle
.
â Mast
28 mins ago
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Simplify the if by just doing
if (first == second && second == third && third == fourth)
....
You said the inputs are degrees, now itâÂÂs been awhile since geometry but squares and rectangles are determined by length of side, not angle degree. Both are 4 sided objects so every angle is the same (unless youâÂÂre talking about something like a trapezoid, rhombus).
New contributor
Simplify the if by just doing
if (first == second && second == third && third == fourth)
....
You said the inputs are degrees, now itâÂÂs been awhile since geometry but squares and rectangles are determined by length of side, not angle degree. Both are 4 sided objects so every angle is the same (unless youâÂÂre talking about something like a trapezoid, rhombus).
New contributor
New contributor
answered 1 hour ago
Sean
561
561
New contributor
New contributor
For completeness, this optimizes theSquare
check. Not theRectangle
.
â Mast
28 mins ago
add a comment |Â
For completeness, this optimizes theSquare
check. Not theRectangle
.
â Mast
28 mins ago
For completeness, this optimizes the
Square
check. Not the Rectangle
.â Mast
28 mins ago
For completeness, this optimizes the
Square
check. Not the Rectangle
.â Mast
28 mins ago
add a comment |Â
up vote
1
down vote
I assume, that all inputs are length of sides and that the sides are entered in consecutive clock- or counterclockwise order.
Console.Write("Input first: ");
int first = int.Parse(Console.ReadLine());
Console.Write("Input second: ");
int second = int.Parse(Console.ReadLine());
Console.Write("Input third: ");
int third = int.Parse(Console.ReadLine());
Console.Write("Input fourth: ");
int fourth = int.Parse(Console.ReadLine());
Repetitive code should be a no-go in programming.
The if-statements are checking too much. A rectangle has pairwise equal sides (1 == 3 and 2 == 4)
and a square is a rectangle where the two pair of sides are also equal in length (rectangle == true && (1 == 2))
All in all that could be:
void CheckForRectangleAndSqare()
const int numSides = 4;
int sides = new int[numSides];
for (int i = 0; i < numSides; i++)
do
Console.Write($"Enter side no i + 1: ");
while (!int.TryParse(Console.ReadLine(), out sides[i]));
// Check for Rectangle
if (sides[0] == sides[2] && sides[1] == sides[3])
// Check for a square
if (sides[0] == sides[1])
Console.WriteLine("It's a Square");
else
Console.WriteLine("It's a Recangle");
else
Console.WriteLine("It's neither a Square nor a Rectangle");
If the sides are entered arbitrarily and you can combine them as you want, you can do:
void CheckForRectangleAndSqare()
const int numSides = 4;
int sides = new int[numSides];
for (int i = 0; i < numSides; i++)
do
Console.Write($"Enter side no i + 1: ");
while (!int.TryParse(Console.ReadLine(), out sides[i]));
var sideGroups = sides.GroupBy(s => s).Count();
if (sideGroups == 1)
Console.WriteLine("It's a Square");
else if (sideGroups == 2)
Console.WriteLine("It's a Recangle");
else
Console.WriteLine("It's neither a Square nor a Rectangle");
add a comment |Â
up vote
1
down vote
I assume, that all inputs are length of sides and that the sides are entered in consecutive clock- or counterclockwise order.
Console.Write("Input first: ");
int first = int.Parse(Console.ReadLine());
Console.Write("Input second: ");
int second = int.Parse(Console.ReadLine());
Console.Write("Input third: ");
int third = int.Parse(Console.ReadLine());
Console.Write("Input fourth: ");
int fourth = int.Parse(Console.ReadLine());
Repetitive code should be a no-go in programming.
The if-statements are checking too much. A rectangle has pairwise equal sides (1 == 3 and 2 == 4)
and a square is a rectangle where the two pair of sides are also equal in length (rectangle == true && (1 == 2))
All in all that could be:
void CheckForRectangleAndSqare()
const int numSides = 4;
int sides = new int[numSides];
for (int i = 0; i < numSides; i++)
do
Console.Write($"Enter side no i + 1: ");
while (!int.TryParse(Console.ReadLine(), out sides[i]));
// Check for Rectangle
if (sides[0] == sides[2] && sides[1] == sides[3])
// Check for a square
if (sides[0] == sides[1])
Console.WriteLine("It's a Square");
else
Console.WriteLine("It's a Recangle");
else
Console.WriteLine("It's neither a Square nor a Rectangle");
If the sides are entered arbitrarily and you can combine them as you want, you can do:
void CheckForRectangleAndSqare()
const int numSides = 4;
int sides = new int[numSides];
for (int i = 0; i < numSides; i++)
do
Console.Write($"Enter side no i + 1: ");
while (!int.TryParse(Console.ReadLine(), out sides[i]));
var sideGroups = sides.GroupBy(s => s).Count();
if (sideGroups == 1)
Console.WriteLine("It's a Square");
else if (sideGroups == 2)
Console.WriteLine("It's a Recangle");
else
Console.WriteLine("It's neither a Square nor a Rectangle");
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I assume, that all inputs are length of sides and that the sides are entered in consecutive clock- or counterclockwise order.
Console.Write("Input first: ");
int first = int.Parse(Console.ReadLine());
Console.Write("Input second: ");
int second = int.Parse(Console.ReadLine());
Console.Write("Input third: ");
int third = int.Parse(Console.ReadLine());
Console.Write("Input fourth: ");
int fourth = int.Parse(Console.ReadLine());
Repetitive code should be a no-go in programming.
The if-statements are checking too much. A rectangle has pairwise equal sides (1 == 3 and 2 == 4)
and a square is a rectangle where the two pair of sides are also equal in length (rectangle == true && (1 == 2))
All in all that could be:
void CheckForRectangleAndSqare()
const int numSides = 4;
int sides = new int[numSides];
for (int i = 0; i < numSides; i++)
do
Console.Write($"Enter side no i + 1: ");
while (!int.TryParse(Console.ReadLine(), out sides[i]));
// Check for Rectangle
if (sides[0] == sides[2] && sides[1] == sides[3])
// Check for a square
if (sides[0] == sides[1])
Console.WriteLine("It's a Square");
else
Console.WriteLine("It's a Recangle");
else
Console.WriteLine("It's neither a Square nor a Rectangle");
If the sides are entered arbitrarily and you can combine them as you want, you can do:
void CheckForRectangleAndSqare()
const int numSides = 4;
int sides = new int[numSides];
for (int i = 0; i < numSides; i++)
do
Console.Write($"Enter side no i + 1: ");
while (!int.TryParse(Console.ReadLine(), out sides[i]));
var sideGroups = sides.GroupBy(s => s).Count();
if (sideGroups == 1)
Console.WriteLine("It's a Square");
else if (sideGroups == 2)
Console.WriteLine("It's a Recangle");
else
Console.WriteLine("It's neither a Square nor a Rectangle");
I assume, that all inputs are length of sides and that the sides are entered in consecutive clock- or counterclockwise order.
Console.Write("Input first: ");
int first = int.Parse(Console.ReadLine());
Console.Write("Input second: ");
int second = int.Parse(Console.ReadLine());
Console.Write("Input third: ");
int third = int.Parse(Console.ReadLine());
Console.Write("Input fourth: ");
int fourth = int.Parse(Console.ReadLine());
Repetitive code should be a no-go in programming.
The if-statements are checking too much. A rectangle has pairwise equal sides (1 == 3 and 2 == 4)
and a square is a rectangle where the two pair of sides are also equal in length (rectangle == true && (1 == 2))
All in all that could be:
void CheckForRectangleAndSqare()
const int numSides = 4;
int sides = new int[numSides];
for (int i = 0; i < numSides; i++)
do
Console.Write($"Enter side no i + 1: ");
while (!int.TryParse(Console.ReadLine(), out sides[i]));
// Check for Rectangle
if (sides[0] == sides[2] && sides[1] == sides[3])
// Check for a square
if (sides[0] == sides[1])
Console.WriteLine("It's a Square");
else
Console.WriteLine("It's a Recangle");
else
Console.WriteLine("It's neither a Square nor a Rectangle");
If the sides are entered arbitrarily and you can combine them as you want, you can do:
void CheckForRectangleAndSqare()
const int numSides = 4;
int sides = new int[numSides];
for (int i = 0; i < numSides; i++)
do
Console.Write($"Enter side no i + 1: ");
while (!int.TryParse(Console.ReadLine(), out sides[i]));
var sideGroups = sides.GroupBy(s => s).Count();
if (sideGroups == 1)
Console.WriteLine("It's a Square");
else if (sideGroups == 2)
Console.WriteLine("It's a Recangle");
else
Console.WriteLine("It's neither a Square nor a Rectangle");
edited 8 mins ago
answered 19 mins ago
Henrik Hansen
5,2481620
5,2481620
add a comment |Â
add a comment |Â
Randel Ramirez is a new contributor. Be nice, and check out our Code of Conduct.
Randel Ramirez is a new contributor. Be nice, and check out our Code of Conduct.
Randel Ramirez is a new contributor. Be nice, and check out our Code of Conduct.
Randel Ramirez is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f206012%2fcheck-if-its-a-square-or-rectangle%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
Why have tags of linq and functional-programming? You aren't using either of them. As a side note you can simplify your square if statement no need to check the if 2nd = 3rd & 4th and 3rd = 4th since you checked the 1st equal to all sides the others ones are redundant
â CharlesNRice
2 hours ago
maybe there's an easier way to do this via linq? perhaps...
â Randel Ramirez
2 hours ago