Solved

C-sharp How to Find the biggest integer?

Posted on 2009-04-07
13
1,672 Views
Last Modified: 2012-05-06
I just started coding and iam doing c-sharp first. So far all i know is If and else statements. I am trying to figure out a way to find the biggest integer out of 5 integers the user inputs. My problem is that i cant get it to work here is my code:

Whenever the first integer is bigger then the second integer that i have inputted the code messes up. It assigns biggest to the first integer and it does not check the rest.

How do i fix that?

i tried adding more if statements that checked 1 vs 3 and 1 vs 4 1vs5 but i either did not do it correctly or it did not work that way...
using System;

using System.Collections.Generic;

using System.Text;

 

namespace Ch3

{

    class ch3practice

    {

        static void Main(string[] args)

        {

            int number1, number2, number3, number4, number5;

            int biggest = 0;

           

            // initialize the integers

 

            Console.Write("Integer please:  ");

            //promt for integer

            number1 = Convert.ToInt32(Console.ReadLine());

            //take integer 

            Console.Write("Integer please:  ");

            //promt for integer

            number2 = Convert.ToInt32(Console.ReadLine());

            //take integer Console.Write("Integer please");

            Console.Write("Integer please:  ");

            //promt for integer

            number3 = Convert.ToInt32(Console.ReadLine());

            //take integer 

            Console.Write("Integer please:  ");

            //promt for integer

            number4 = Convert.ToInt32(Console.ReadLine());

            //take integer Console.Write("Integer please");

            Console.Write("Integer please:  ");

            //promt for integer

            number5 = Convert.ToInt32(Console.ReadLine());

            //take integer Console.Write("Integer please");           

          

            //check 1v2

            if (number1 >= number2)

              

                biggest = number1;

            else 

 

            if (number2 >= number3)

                                    biggest = number2;

                                else

                                    if (number3 >= number4)

                                        biggest = number3;

                                    else

                                        if (number4 >= number5)

                                            biggest = number4;

                                        else

                                            if (number5 >= number1)

                                                biggest = number5;

                           

            

                

   

 

         Console.WriteLine("{0}  Is the bigest #", biggest );                

            Console.ReadLine();

 

        }

    }

}

Open in new window

0
Comment
Question by:Bulg
13 Comments
 
LVL 6

Expert Comment

by:Cebik
ID: 24091592
try this :)

using System;

using System.Collections.Generic;

using System.Text;
 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            List<int> values = new List<int>();
 

            while (true)

            {

                Console.Write("\nEnter value " + (values.Count + 1).ToString() + " :");

                string inputValue = Console.ReadLine();
 

                if (inputValue == "")

                    break;
 

                int value;

                if (int.TryParse(inputValue, out value) == false)

                    continue;
 

                values.Add(value);

            }
 

            int bigest = int.MinValue;

            foreach (int i in values)

                if (i > bigest)

                    bigest = i;
 

            Console.WriteLine("\n\nThe bigest number is: " + bigest.ToString());

            Console.ReadLine();

        }

    }

}

Open in new window

0
 
LVL 6

Expert Comment

by:Cebik
ID: 24091606
in yout example will be

if (number1 > bigest) bigest = number1;

if (number2 > bigest) bigest = number2;

if (number3 > bigest) bigest = number3;

(...)

if (numberN> bigest) bigest = numberN;

Open in new window

0
 
LVL 6

Expert Comment

by:Cebik
ID: 24091618
but... at the begining add

bigest = int.MinValue;

Open in new window

0
 

Author Comment

by:Bulg
ID: 24091814
show me an example please i can only use if and else statements so far ;/ thats why the first code is of no use to me.

added a few more lines but it doesn't work ;/
using System;

using System.Collections.Generic;

using System.Text;
 

namespace Ch3

{

    class ch3practice

    {

        static void Main(string[] args)

        {

            int number1, number2, number3, number4, number5;

            int biggest = 0;
 
 

           
 

            // initialize the integers
 

            Console.Write("Integer please:  ");

            //promt for integer

            number1 = Convert.ToInt32(Console.ReadLine());

            //take integer 

            Console.Write("Integer please:  ");

            //promt for integer

            number2 = Convert.ToInt32(Console.ReadLine());

            //take integer Console.Write("Integer please");

            Console.Write("Integer please:  ");

            //promt for integer

            number3 = Convert.ToInt32(Console.ReadLine());

            //take integer 

            Console.Write("Integer please:  ");

            //promt for integer

            number4 = Convert.ToInt32(Console.ReadLine());

            //take integer Console.Write("Integer please");

            Console.Write("Integer please:  ");

            //promt for integer

            number5 = Convert.ToInt32(Console.ReadLine());

            //take integer Console.Write("Integer please");           

          

            //check 1v2

            if (number1 >= number2)

              

                biggest = number1;

            

            else 
 

            if (number2 >= number3)

                                    biggest = number2;

                                else

                                    if (number3 >= number4)

                                        biggest = number3;

                                    else

                                        if (number4 >= number5)

                                            biggest = number4;

                                        else

                                            if (number5 >= number1)

                                                biggest = number5;

                            if (biggest <=number3 )

                                biggest = number3;

                            if (number4 >= biggest)

                                biggest = number4;

                            if (number4 >= biggest)

                                biggest = number5;

            

                

   
 

         Console.WriteLine("{0}  Is the bigest #", biggest );                

            Console.ReadLine();
 

        }

    }

}

Open in new window

0
 
LVL 1

Expert Comment

by:DaRakNa
ID: 24092007
Try to use this:
using System;

using System.Collections.Generic;

using System.Text;

 

namespace Ch3

{

    class ch3practice

    {

        static void Main(string[] args)

        {

            int number[6];

            int biggest = 0;

           

            // initialize the integers

 

            Console.Write("Integer please:  ");

            //promt for integer

            number[1] = Convert.ToInt32(Console.ReadLine());

            //take integer 

            Console.Write("Integer please:  ");

            //promt for integer

            number[2] = Convert.ToInt32(Console.ReadLine());

            //take integer Console.Write("Integer please");

            Console.Write("Integer please:  ");

            //promt for integer

            number[3] = Convert.ToInt32(Console.ReadLine());

            //take integer 

            Console.Write("Integer please:  ");

            //promt for integer

            number[4] = Convert.ToInt32(Console.ReadLine());

            //take integer Console.Write("Integer please");

            Console.Write("Integer please:  ");

            //promt for integer

            number[5] = Convert.ToInt32(Console.ReadLine());

            //take integer Console.Write("Integer please");           

          

            //check 

            for(int i=1;i<=5;i++)

               {

                  if(number[i]>biggest)

                     {

                         biggest=number[i];i=1;

                     }

               }

         Console.WriteLine("{0}  Is the bigest #", biggest );                

            Console.ReadLine();

 

        }

    }

}

Open in new window

0
 
LVL 6

Expert Comment

by:Cebik
ID: 24092076
fixed your mistakes

static void Main(string[] args)

        {

            int[] number = new int[6];

            int biggest = 0;

           

            // initialize the integers

 

            Console.Write("Integer please:  ");

            //promt for integer

            number[0] = Convert.ToInt32(Console.ReadLine());

            //take integer 

            Console.Write("Integer please:  ");

            //promt for integer

            number[1] = Convert.ToInt32(Console.ReadLine());

            //take integer Console.Write("Integer please");

            Console.Write("Integer please:  ");

            //promt for integer

            number[2] = Convert.ToInt32(Console.ReadLine());

            //take integer 

            Console.Write("Integer please:  ");

            //promt for integer

            number[3] = Convert.ToInt32(Console.ReadLine());

            //take integer Console.Write("Integer please");

            Console.Write("Integer please:  ");

            //promt for integer

            number[4] = Convert.ToInt32(Console.ReadLine());

            //take integer Console.Write("Integer please");           

          

            //check 

            for(int i=0;i<number.Length;i++)

            {

              if(number[i]>biggest)

                     biggest=number[i];i=1;

            }
 

           Console.WriteLine("{0}  Is the bigest #", biggest );                

           Console.ReadLine();

 

        }

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:Cebik
ID: 24092097
Bulg:


static void Main(string[] args)

{

    int number1, number2, number3, number4, number5;

   

    // initialize the integers
 

    Console.Write("Integer please:  ");

    //promt for integer

    number1 = Convert.ToInt32(Console.ReadLine());

    //take integer 

    Console.Write("Integer please:  ");

    //promt for integer

    number2 = Convert.ToInt32(Console.ReadLine());

    //take integer Console.Write("Integer please");

    Console.Write("Integer please:  ");

    //promt for integer

    number3 = Convert.ToInt32(Console.ReadLine());

    //take integer 

    Console.Write("Integer please:  ");

    //promt for integer

    number4 = Convert.ToInt32(Console.ReadLine());

    //take integer Console.Write("Integer please");

    Console.Write("Integer please:  ");

    //promt for integer

    number5 = Convert.ToInt32(Console.ReadLine());

    //take integer Console.Write("Integer please");           

  

    int biggest = int.MinValue;

    if (number1 > biggest)

        biggest = number1;

    if (number2 > biggest)

        biggest = number2;

    if (number3 > biggest)

        biggest = number3;

    if (number4 > biggest)

        biggest = number4;

    if (number5 > biggest)

        biggest = number5;

       

 Console.WriteLine("{0}  Is the bigest #", biggest );                

    Console.ReadLine();
 

}

Open in new window

0
 
LVL 6

Expert Comment

by:Cebik
ID: 24092119
but in my post id: 24091592 is if statement in line 30...
this is much more better solution..
0
 
LVL 2

Expert Comment

by:zulti
ID: 24092157
I would change line 28 in Cebik's answer to be
28:     int biggest = number1 ;

and you can remove lines 29 and 30
other than that this is the answer you asked for.....
0
 
LVL 6

Expert Comment

by:Cebik
ID: 24092178
shure... can be in this way also..
0
 
LVL 6

Accepted Solution

by:
Cebik earned 125 total points
ID: 24092198

static void Main(string[] args)

{

    int number1, number2, number3, number4, number5;

   

    // initialize the integers
 

    Console.Write("Integer please:  ");

    //promt for integer

    number1 = Convert.ToInt32(Console.ReadLine());

    //take integer 

    Console.Write("Integer please:  ");

    //promt for integer

    number2 = Convert.ToInt32(Console.ReadLine());

    //take integer Console.Write("Integer please");

    Console.Write("Integer please:  ");

    //promt for integer

    number3 = Convert.ToInt32(Console.ReadLine());

    //take integer 

    Console.Write("Integer please:  ");

    //promt for integer

    number4 = Convert.ToInt32(Console.ReadLine());

    //take integer Console.Write("Integer please");

    Console.Write("Integer please:  ");

    //promt for integer

    number5 = Convert.ToInt32(Console.ReadLine());

    //take integer Console.Write("Integer please");           

  

    biggest = number1;

    if (number2 > biggest)

        biggest = number2;

    if (number3 > biggest)

        biggest = number3;

    if (number4 > biggest)

        biggest = number4;

    if (number5 > biggest)

        biggest = number5;

       

    Console.WriteLine("{0}  Is the bigest #", biggest);                

    Console.ReadLine();

}

Open in new window

0
 
LVL 6

Expert Comment

by:Cebik
ID: 24092277
sorry...
line 28

int biggest = number1;

Open in new window

0
 

Author Closing Comment

by:Bulg
ID: 31567733
Thank you. It makes perfect seance. Sometimes the simplest things get me.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now