settingsAccountsettings
Menusettings

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

+4 votes

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 c sharp

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

asked in C# category by user Jolie Ann
edited by user golearnweb

2 Answers

+2 votes
 
Best answer

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);
            }
    }
}
answered by user andrew
edited by user golearnweb
Niceeeee :-)
+1 vote

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

buffer default height in console application in C Sharp

answered by user nikole
edited by user golearnweb
...