settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: How to make console application which will write 5 earlier number on console

+1 vote
how to make console application which will take input a number and it will write 5 earlier numbers on console if user type 7 then output should be 6,5,4,3,2 or if user type 2 then output should be 1,0,-1,-2,-3.---------- sorry i am totally new in programming.
asked in C# category by user dev3ns
edited by user golearnweb

1 Answer

0 votes
 
Best answer

You should use the reverse for loop;

Try this code for your task (see highlighted line #10):

using System;
 
class NumbersFromNToOne
{
    static void Main()
    {
        Console.WriteLine("Please write your number: ");
        int n = int.Parse(Console.ReadLine());
 
        for (int i = n-1; i >= n-5; i--)
        {
            Console.Write("{0} ",i);
        }
        Console.WriteLine();
    }
}

 

answered by user golearnweb
selected by user golearnweb
Thank you so much sir _/\_
The first i MUST BE n-1 and the whole reverse for loop will look like this:

for (int i = n-1; i >= n-5; i--) {
Console.Write("{0} ",i);
}
...