Periyasamy Vellingirisamy
Write a program for first N number of prime number using c#

Program for finding first 10 or N number of Prime numbers.
Here assume “number” value as 6.
Result would be: 1,2,3,5

By Periyasamy Vellingirisamy in C# on Jun 01 2020
  • yoesh
    Jan, 2022 20

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

    • 0
  • Vara Prasad
    Nov, 2021 8

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

    • 0
  • Wendy Tran
    Jul, 2021 23

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

    1. foreach (var item in result)
    2. {
    3. Console.WriteLine(item);
    4. }
    5. return result;
    6. }
    7. }

    • 0
  • shiva charan
    Mar, 2021 16

    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++;
    }
    }
    }
    }

    • 0
  • Periyasamy Vellingirisamy
    Jul, 2020 3

    Hi
    Refer following sample program.

    1. for (int i = 2; i <= number; i++)
    2. {
    3. int count = 0;
    4. for (int j = 2; j <= i; j++)
    5. {
    6. if (i % j == 0)
    7. {
    8. count++;
    9. }
    10. if (count >= 2)
    11. break;
    12. }
    13. if (count < 2)
    14. {
    15. Console.WriteLine("Prime : " + i);
    16. }
    17. }
    18. Console.Read();

    • 0
  • Periyasamy Vellingirisamy
    Jul, 2020 3

    Hi
    Refer following sample program.

    1. for (int i = 2; i <= number; i++)
    2. {
    3. int count = 0;
    4. for (int j = 2; j <= i; j++)
    5. {
    6. if (i % j == 0)
    7. {
    8. count++;
    9. }
    10. if (count >= 2)
    11. break;
    12. }
    13. if (count < 2)
    14. {
    15. Console.WriteLine("Prime : " + i);
    16. }
    17. }
    18. Console.Read();

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS