How to find Prime number within Range from 1 to nth in c#


In this Program, you can see how to find Prime number in c#  using for loop

using System;

using System.Collections.Generic;

using System.Text;



namespace testconsole

{

 class Program

 {

  static void Main(string[] args)

  {

   bool isprimenumber = true;

   Console.WriteLine("Enter nth number");

   int totalnumber = Convert.ToInt32(Console.ReadLine());

   for (int i = 2; i <= totalnumber; i++)

   {

    for (int j = 2; j < totalnumber; j++)

    {

     if (i != j && i % j == 0)

     {

      isprimenumber = false;

      break;

     }

    }

    if (isprimenumber)

    {

     Console.WriteLine(+i);

    }

    isprimenumber = true;

   }



   Console.ReadLine();





  }

 }

}




Comments

Popular posts from this blog

Bootstrap Modal Popup keep open on PostBack in ASP.Net

Resolved Issue in Asp core 3.0 serializersettings does not exist in AddJsonOptions

.Net most asked interview questions for experienced professionals (C#,Asp WEBFORM,MVC,ASP CORE,WEB API,SQL Server,Java Script,Jquery)