[Solved] Console application that prints the previous 5 numbers

  

7
Topic starter

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.

2 Answers
7

You should use the reverse for loop;

Try this code for your task:

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();
    }
}

Thank you so much sir _/\_

7

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