I wrote this blog keeping in mind C# beginners. In most of the interviews beginners or freshers may face this question.
- void Main()
- {
- bool notPrime = false;
- int j;
- Console.WriteLine("Enter your Target?");
- int target = Int32.Parse(Console.ReadLine());
- for (int i = 2; i <= target; i++)
- {
- for (j = 2; j < i; j++)
- {
- if (i % j == 0)
- {
- notPrime = true;
- break;
- }
- }
- if (!notPrime)
- Console.Write("{0} ", j);
- else
- notPrime = false;
- }
- Console.ReadLine();
- }
- void Main()
- {
- int i, j;
- Console.WriteLine("Enter a number to check is it prime or not :");
- i = Int32.Parse(Console.ReadLine());
- for (j = 2; j <i; j++)
- {
- if (i % j == 0)
- {
- Console.WriteLine("{0} is not prime", i);
- break;
- }
- }
- if (j == i)
- {
- Console.WriteLine("{0} is prime", i);
- }
- Console.ReadLine();
- }
