Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Get the Largest among the four numbers.

Get the Largest among the four numbers.

Scheduled Pinned Locked Moved C#
csharphelp
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chitra4sat
    wrote on last edited by
    #1

    Hi all, i am new to programming in C#. i dont know to code it to get the output ie., the largest among the four numbers and the user will give the input as numbers. and the munbers are int type inta,b,c,d; i need the code for largest among the four numbers of int a,b,c,d. pleae help me in sending code. regards, chitra.

    A L R D 4 Replies Last reply
    0
    • C chitra4sat

      Hi all, i am new to programming in C#. i dont know to code it to get the output ie., the largest among the four numbers and the user will give the input as numbers. and the munbers are int type inta,b,c,d; i need the code for largest among the four numbers of int a,b,c,d. pleae help me in sending code. regards, chitra.

      A Offline
      A Offline
      abhinarulkar
      wrote on last edited by
      #2

      you can apply a simple algorithm, if (a > b) { if (a > C) { if (a > d) { return a; } else { return d; } } else { if (c > d) { return c; } else { return d; } } } else { if (b > C) { if (b > d) { return b; } else { return d; } } else { if (c > d) { return c; } else { return d; } } }

      Learning is a never ending process of Life.

      1 Reply Last reply
      0
      • C chitra4sat

        Hi all, i am new to programming in C#. i dont know to code it to get the output ie., the largest among the four numbers and the user will give the input as numbers. and the munbers are int type inta,b,c,d; i need the code for largest among the four numbers of int a,b,c,d. pleae help me in sending code. regards, chitra.

        L Offline
        L Offline
        Luis Alonso Ramos
        wrote on last edited by
        #3

        There's a simpler algorithm: int Max4(int a, int b, int c, int d) { return Math.Max(Math.Max(a, b), Math.Max(c, d)); } Is this for a school assignment? :-D

        Luis Alonso Ramos Intelectix Chihuahua, Mexico

        Not much here: My CP Blog!

        J 1 Reply Last reply
        0
        • C chitra4sat

          Hi all, i am new to programming in C#. i dont know to code it to get the output ie., the largest among the four numbers and the user will give the input as numbers. and the munbers are int type inta,b,c,d; i need the code for largest among the four numbers of int a,b,c,d. pleae help me in sending code. regards, chitra.

          R Offline
          R Offline
          Russell Jones
          wrote on last edited by
          #4

          i'd use: int[] numbers = {a,b,c,d}; int MaxNumber = GetHighestNumber(numbers); public int GetHighestNumber(int[] testNumbers) { int max = 0; foreach(int i in testNumbers) { //algorithm goes here } return max; }

          1 Reply Last reply
          0
          • C chitra4sat

            Hi all, i am new to programming in C#. i dont know to code it to get the output ie., the largest among the four numbers and the user will give the input as numbers. and the munbers are int type inta,b,c,d; i need the code for largest among the four numbers of int a,b,c,d. pleae help me in sending code. regards, chitra.

            D Offline
            D Offline
            DavidNohejl
            wrote on last edited by
            #5

            Using extension methods (C# 3.0):

            int[] nums = {a,b,c,d};
            int max = nums.GetLargest();
            

            :cool:


            "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

            1 Reply Last reply
            0
            • L Luis Alonso Ramos

              There's a simpler algorithm: int Max4(int a, int b, int c, int d) { return Math.Max(Math.Max(a, b), Math.Max(c, d)); } Is this for a school assignment? :-D

              Luis Alonso Ramos Intelectix Chihuahua, Mexico

              Not much here: My CP Blog!

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              Luis Alonso Ramos wrote:

              Is this for a school assignment?

              Seems pretty likely - and you just did him no favours by doing it for him.

              --- How to get answers to your questions[^]

              L 1 Reply Last reply
              0
              • J J4amieC

                Luis Alonso Ramos wrote:

                Is this for a school assignment?

                Seems pretty likely - and you just did him no favours by doing it for him.

                --- How to get answers to your questions[^]

                L Offline
                L Offline
                Luis Alonso Ramos
                wrote on last edited by
                #7

                Yeah, I didn't notice until I saw other posts from this same guy... :doh: Anyway, the solution was trivial if he knew of the existence of Math.Max.

                Luis Alonso Ramos Intelectix Chihuahua, Mexico

                Not much here: My CP Blog!

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups