In this program, you’ll learn to print all prime numbers within an interval using for loops and display it.
A positive integer greater than 1 which has no other factors except 1 and the number itself is called a prime number.
2, 3, 5, 7 etc. are prime numbers as they do not have any other factors. But 6 is not prime (it is composite) since, 2 x 3 = 6
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
static void Main(string[] args) { int num1, num2,sayac=0; Console.Write("Enter lower range: "); num1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter upper range: "); num2 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Prime numbers between {0} and {1} are: ",num1,num2); Console.WriteLine("=============================================="); for(int i=num1;i<num2;i++) { sayac = 0; if(i>1) { for(int j=2;j<i;j++) { if(i%j==0) { sayac = 1; break; } } if(sayac==0) { Console.WriteLine(i); } } } Console.ReadKey(); } |