Skip to content

Prime numbers from 1 to N in reverse order in C#

Prime numbers from 1 to N in reverse order in C# – Learn how to display the prime numbers in reverse order.
For example you want to display prime Numbers 1 to 20 in reverse order as 19,17,13,11,7,5,3,2

Prime numbers from 1 to N in reverse order in C#

Example Program

using System;
 class Program
    {
        static void Main(string[] args)
        {
            int n = 20;
            Boolean flag = true;

            Console.WriteLine("Prime Numbers 
             are in Reverse order");
            for(int i=n;i>=2;i--)
            { 
                for(int j=i-1;j>=2;j--)
                {
                    if(i%j==0)
                    {
                        flag = false;
                        break;
                    }
                }
                if(flag==true)
                {
                    Console.WriteLine(i);
                }
                flag = true;
            }
        }
    }

Output

Prime Numbers are in Reverse order
19
17
13
11
7
5
3
2


Published inC# Programming Examples