Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Write a program that asks the user to enter a series of non-zero integers, one at a time.
The program should keep asking the user for a number until they enter a value of 0.
The program should then display the largest of the numbers that have been entered.
Be aware that the user could enter all negative integers and your program has to be
able to handle this.

What I have tried:

Ok, so this is what i ended up doing and it's completly fine. I just needed to get some sleep, when I tried again it went pretty smoothly. I knew how to do it just didn't know how to put it into code. Thanks for the solution posted but I haven't seen the code you posted so I'm sure I shouldn't be using it. Cheers!






C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace bigestNumber
{
    class ProgramNumber
    {
        static void Main(string[] args)
        {
            int num2 ;  //declaring the required variables.
            int numberEntered;
            string inputNumber, inputNumber1;
            
            
            Console.Write("Please enter a series of non-zero integer numbers, once at a time. Enter 0 to stop: ");

            inputNumber = Console.ReadLine();  //waiting for the user imput
            numberEntered = int.Parse(inputNumber);
            do
            {

                Console.Write("Please enter another number. Enter 0 to stop: ");
                inputNumber1 = Console.ReadLine();
                num2 = int.Parse(inputNumber1);
                if (numberEntered < num2 && num2 != 0) //if statment
                {
                    numberEntered = num2; //assigns the value of num2 to the numberEntered variable
                }

            }
            while ( num2 != 0);  //loop statment

            Console.Write("The largest of the numbers you have entered is: ");
            Console.WriteLine(numberEntered); //displays the value of the variable num

            Console.ReadKey();  //waits for the user to press a key
        }
    }
}
Posted
Updated 4-Nov-18 10:48am
v3
Comments
Eric Lynch 4-Nov-18 0:40am    
It sounds like you've made an honest effort to solve the problem. So, why not click "Improve question" and share the actual code you've tried?

That way, folks here can help point out where you've gone wrong, instead of writing it for you.

This is homework, so we'll give you no code.
But to be honest, this is pretty simple, honest!

Break it down into smaller bets and get those working first.

So start with the most important:
Ask the user for a number, and read it in.
You know how to do that, right? It's just a Console.WriteLine, followed by a Console.ReadLine and a conversion to an integer (And you know how to do that, I'm sure)

Test that: print the number you entered! When it reads numbers fine, (and complains about non-numeric inputs instead of crashing) move on to the next bit:

Continue to read in numbers until he enters zero
You know how to do that as well: a simple loop that checks the number he read and if it's zero exits.
Test it. Test it again.

Next bit:
Display the largest
Slightly more complicated, but not a lot. Add a "max so far" variable and before you enter the loop set it to the smallest possible value - I'll even give you the code for that:
C#
int maxSoFar = int.MinValue;
setting it to the smallest possible value means that whatever the user enters will be bigger!
Inside the loop, check what the user entered against that value. If the new value is bigger, set maxSoFar to the new value.
After the loop, print maxSoFar because it is the biggest entered value!

Test it. Test it with different data. And again, with yet different data.
When you are happy, all done!
 
Share this answer
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace bigestNumber
{
    class ProgramNumber
    {
        static void Main(string[] args)
        {
            int num2 ;  //declaring the required variables.
            int numberEntered;
            string inputNumber, inputNumber1;
            
            
            Console.Write("Please enter a series of non-zero integer numbers, once at a time. Enter 0 to stop: ");

            inputNumber = Console.ReadLine();  //waiting for the user imput
            numberEntered = int.Parse(inputNumber);
            do
            {

                Console.Write("Please enter another number. Enter 0 to stop: ");
                inputNumber1 = Console.ReadLine();
                num2 = int.Parse(inputNumber1);
                if (numberEntered < num2 && num2 != 0) //if statment
                {
                    numberEntered = num2; //assigns the value of num2 to the numberEntered variable
                }

            }
            while ( num2 != 0);  //loop statment

            Console.Write("The largest of the numbers you have entered is: ");
            Console.WriteLine(numberEntered); //displays the value of the variable num

            Console.ReadKey();  //waits for the user to press a key
        }
    }
}
 
Share this answer
 
Comments
Richard MacCutchan 4-Nov-18 13:00pm    
You do not help people to learn by doing their work for them.
Member 14042955 4-Nov-18 14:22pm    
dude I am the OP, this is my code, just posted it as a solution too
Richard MacCutchan 5-Nov-18 3:27am    
Sorry, I should have checked the ids more closely.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900