Printing the numbers from 1 to 1000 in Console Application in C#

  

1
Rozpoczęcie tematu

Write a program that prints at the console the numbers from 1 to 1000, each at a separate line. But whenever I am doing the CTRL+F5, the console only prints the numbers from 702 to 1000...

using System;
 
class NumberPrinting
{
    static void Main()
    {
            for (int i = 0; i <= 1000; i++)
            {
                Console.WriteLine(i);
            }
    }
}

console application

Here is the code I am using - please help me understand where I am wrong!

2 Odpowiedzi
1

The problem is that by default the Console has a LIMIT of the numbers (symbols) it can display - it is 300 rows

https://msdn.microsoft.com/en-us/library/system.console.bufferheight.aspx

That is why you are seeing the numbers from 702 to 1000...

The solution is simple - just add: Console.BufferHeight = 2000; -> before the for loop. 

You can also learn more about Console.BufferHeight  by selecting it and pressing F1 (Help).

Here is your code which will display all the numbers required:

using System;
 
class NumberPrinting
{
    static void Main()
    {
            Console.BufferHeight = 2000;    
            for (int i = 0; i <= 1000; i++)
            {
                Console.WriteLine(i);
            }
    }
}
1

Change the default settings of your console application - in the Properties - like in the picture below:

buffer default size console application

Udostępnij: