Check if it's a square or rectangle

The name of the pictureThe name of the pictureThe name of the pictureClash 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();










share|improve this question









New contributor




Randel Ramirez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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















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();










share|improve this question









New contributor




Randel Ramirez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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













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();










share|improve this question









New contributor




Randel Ramirez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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#






share|improve this question









New contributor




Randel Ramirez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Randel Ramirez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 45 mins ago









Henrik Hansen

5,2481620




5,2481620






New contributor




Randel Ramirez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









Randel Ramirez

1084




1084




New contributor




Randel Ramirez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Randel Ramirez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Randel Ramirez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 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




    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











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).






share|improve this answer








New contributor




Sean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • For completeness, this optimizes the Square check. Not the Rectangle.
    – Mast
    28 mins ago

















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");






share|improve this answer






















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Randel Ramirez is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    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






























    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).






    share|improve this answer








    New contributor




    Sean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

















    • For completeness, this optimizes the Square check. Not the Rectangle.
      – Mast
      28 mins ago














    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).






    share|improve this answer








    New contributor




    Sean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

















    • For completeness, this optimizes the Square check. Not the Rectangle.
      – Mast
      28 mins ago












    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).






    share|improve this answer








    New contributor




    Sean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    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).







    share|improve this answer








    New contributor




    Sean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    share|improve this answer



    share|improve this answer






    New contributor




    Sean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    answered 1 hour ago









    Sean

    561




    561




    New contributor




    Sean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





    New contributor





    Sean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    Sean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.











    • 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















    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












    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");






    share|improve this answer


























      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");






      share|improve this answer
























        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");






        share|improve this answer














        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");







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 8 mins ago

























        answered 19 mins ago









        Henrik Hansen

        5,2481620




        5,2481620




















            Randel Ramirez is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            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.













             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            Installing NextGIS Connect into QGIS 3?

            One-line joke