1
Gives me error saying that the variable remainder
was not designated. There are several parts of the code that I do not understand and would like explanation.
- Write a program that continually reads in integer values until a four Digit number in the range 111 to 9999 is entered. Display the number vertically i.e. each Digit on a Separate line.
Example:
Input: 1234
Output:
1
2
3
4
static void Main(string[] args)
{
int number, remainder;
string s = "";
//Reading a 4-digit number between 1111 and 9999
do
{
Console.WriteLine("Enter a number between 1111 and 9999: ");
number = int.Parse(Console.ReadLine());
} while (number < 1111 || number > 9999);/*Por que tenho que pôr esta condição*/
//Breaking number and formatting it vertically in a string
while (number > 0)
remainder = number % 10;
s = remainder + "\n" + s;
number = number / 10;
// I didn’t get anything from the 4 lines above, what they do?
//Displaying number vertically
Console.WriteLine(s); // porquê visualizar a variável s?
Console.ReadLine();
This code is very confusing and missing a part. Which line gives the error? The rest seems to be just a matter of text interpretation.
– Maniero
It is mandatory to use this
while (number > 0)
? It would be better to use theforeach
.– Francisco
@Francis how to use a
foreach
if not even a collection exists?– Maniero
If I understand the question the right way, convert the
number
for string and makesforeach (char in string)
. It would work @bigown?– Francisco
It is possible, but it seems to me not what he wants, he wants to do mathematically.
– Maniero
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).
– Maniero