In this tutorial, we will discuss the concept of the Program for printing first n prime numbers using for loop in C#
In this post, we will learn how to print first n prime numbers using a for loop in C# programming language with Example program.
Prime number
The number that can be divided by 1 and itself. it is called a prime number
for Example 2,3,5,7,11,13…
In this program, the user declares and initializes variables then it will print first n prime numbers, using the for loop in the C# language.
Program 1
using System; namespace primeNumber { class first_N_Prime { static void Main(string[]args) { int num=12, count , i=3, x; if(num>=1) { Console.WriteLine("First "+num+" prime numbers are:\n "); Console.Write("2 "); } for(count=2; count<=num;){ for(x=2; x<=i-1; x++) { if(i%x==0) break; } if(x==i) { Console.Write(i+" "); count++; } i++; } } } }
When the above code is executed, it produces the following result
First 12 prime numbers are: 2 3 5 7 11 13 17 19 23 29 31 37
In this program, the user is asked to enter the value for n., then it will print first n prime numbers, using the for loop in the C# language.
Program 2
using System; namespace primeNumber { class first_N_Prime { static void Main(string[]args) { int num, count , i=3, x; Console.WriteLine("Enter the number of prime you want: "); num=Convert.ToInt32(Console.ReadLine()); if(num>=1) { Console.WriteLine("First "+num+" prime numbers are:\n "); Console.Write("2 "); } for(count=2; count<=num;){ for(x=2; x<=i-1; x++) { if(i%x==0) break; } if(x==i) { Console.Write(i+" "); count++; } i++; } } } }
When the above code is executed, it produces the following result
Enter the number of prime you want:
10
First 10 prime numbers are:
2 3 5 7 11 13 17 19 23 29
Suggested for you
Program to find whether a Number is Prime or Not in C++
Program to find whether a Number is Prime or Not in C
Program to find whether a Number is Prime or Not in Java
Program to find whether a Number is Prime or Not in Python
Program to find whether a Number is Prime or Not in PHP
Program to find whether a Number is Prime or Not using the PHP function
C++ program to find first n prime numbers
Java program to find first n prime numbers
C program to find first n prime numbers
C code to 5 ways to check whether the given integer is Even or Odd
Java code to 5 ways to check whether the given integer is Even or Odd
C++ code to 5 ways to check whether the given integer is Even or Odd
Python code to 5 ways to check whether the given integer is Even or Odd
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…