# How do I solve this exercise? [solved]

## Recommended Posts

Looks like I posted on the wrong forum but i was quite frustrated, sorry. I woke up the next day and solved it myself. I just wanted to say that i didn't want the code, just some advice. I knew what I had to do just didn't know how to put it into code.

Q: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.
A:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

If there is another way to do it, please post it. I'd like to know if there is an "easier" or just a better way to do. Cheers!

##### Share on other sites

Normally I don't reply to such threads, but you have solved the issue (even with some minor faults) and you're looking for alternative methods... so I'm more than happy to help.

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

Keep in mind I did this fairly quickly so it's a bit rushed, but I added in comments.

The goal here is to accept input until 0 has been entered. You need to do the following though... verify if the input is actually an integer. Unless I missed it, your code doesn't verify if someone types in "a" or "45.02a" or "1.25". Once the input has been verified you would track the prior input the current input to see which number is higher. Naturally if you enter in all negative numbers then 0, 0 would be the highest.

Code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Variable to track the highest input.
int highestNumber = 0;

// Create a holding variable for the input
string enteredNumber = "";

// Create a boolean to decide if the program should continue checking, ect...
bool cycleComplete = false;

// ==================================================================

// Loop

while (cycleComplete == false)
{
Console.WriteLine("Please enter an interger below: ");

// Get input

// Verify Input
int number = 0;
if (Int32.TryParse(enteredNumber, out number) == true)
{
// Input is actually an interger - compare and set the new highest
if (number > highestNumber)
{
highestNumber = number;
}

// Check if the number is 0
if (number == 0)
{
cycleComplete = true;
}

}
else
{
// Input was not an interger
Console.WriteLine("Not an interger! - Try again!");
}
}

// Final
Console.WriteLine("==========================");
Console.WriteLine("You have entered 0, and your largest interger was: " + highestNumber);
Console.WriteLine("==========================");
Console.WriteLine("Press any key to exit.");

// End
}
}
}

I hope this helps understand things better, and if you have questions let me know. Happy coding!

## Create an account

Register a new account

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 26
• 10
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
633717
• Total Posts
3013510
×