Program for finding first 10 or N number of Prime numbers.Here assume “number” value as 6.Result would be: 1,2,3,5
int n = int.Parse(Console.ReadLine()); int counter = 0; int i = 2; while(counter< n) { bool isPrime = true; for (int j = 2; j < i; j++) { if (i % j == 0) { isPrime = false; } } if (isPrime) { ++counter; Console.WriteLine(i); } ++i; }
int n = int.Parse(Console.ReadLine()); for (int i = 2; i <=n ; i++) { bool isPrime = true; for (int j = 2; j < i; j++) { if (i%j==0) { isPrime = false; } } if (isPrime) { Console.WriteLine(i); } } Console.ReadLine();
public static List firstNPrimeNumbers(int inputNumber) { List result = new List(); int i, j, flag; for (i = 2; i <= inputNumber; i++) { flag = 1; for (j = 2; j <= i/2; j++) { if (i % j == 0) { flag = 0; break; } } if (flag == 1) { result.Add(i); } }
foreach (var item in result) { Console.WriteLine(item); } return result; }}
foreach (var item in result)
{
Console.WriteLine(item);
}
return result;
using System;
namespace FirstNPrimes{ class Program { static void Main(string[] args) { int n; n = Convert.ToInt32(Console.ReadLine()); int noOfPrimeCount = 0,count,num=2; while(noOfPrimeCount < n) { count = 0; for(int i=2;i<= num; i++) { if(num % i==0) { count++; } if(count>=2) { break; } } if(count<2) { noOfPrimeCount++; Console.WriteLine(num); } num++; } } }}
HiRefer following sample program.
for (int i = 2; i <= number; i++) { int count = 0; for (int j = 2; j <= i; j++) { if (i % j == 0) { count++; } if (count >= 2) break; } if (count < 2) { Console.WriteLine("Prime : " + i); } } Console.Read();
for (int i = 2; i <= number; i++)
int count = 0;
for (int j = 2; j <= i; j++)
if (i % j == 0)
count++;
if (count >= 2)
break;
if (count < 2)
Console.WriteLine("Prime : " + i);
Console.Read();