Find prime number between two range.
Write a function named prime count with a signature in int PrimeCount(int start, int end); the function print the prime number from the given range.
Recall the prime is an integer greater than 1whose only integer factor is one or itself.
if start is | And end is | reason |
10 |
30 | The prime between 10 to 30 are 11 ,13 ,17 ,19, 23 ,29. |
11 | 29 | The prime between 10 to 30 are 11 ,13 ,17 ,19, 23 ,29. |
20 | 22 | 20,21,22 are all non-prime |
1 | 1 | by definition 1 is not prime |
5 | 5 | 5 is prime |
6 | 2 | starting number must less than or equal to the end |
-10 | 6 | Prime is greater than 1,2,3,5 |
using System;
public class PrimeCount
{
public int Result(int start, int end)
{
int a = start, b = end;
int i = 1, j;
if (a > 0 && b > 0)
{
if (a < b)
{
if (a >1 && b > 1)
{
for (j = a; j <= b; j++)
{
int c = 0;
for (i = 1; i <= b; i++)
{
if (j % i == 0)
{
c++;
}
}
if (c == 2)
{
Console.WriteLine(j + " "+ "It a prime");
}
else
{
Console.WriteLine(j + " " + "It is not prime");
}
}
}
else
{
Console.WriteLine("by Defination it is not prime" + a, b);
}
}
else
{
Console.WriteLine("Starting number must be smaller than ending number" + a, b);
}
}
else
{
Console.WriteLine("please enter greater than 0");
}
return 1;
}
}
namespace AssignmentFirst
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Enter the starting number");
int start = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter the Ending number");
int end = Convert.ToInt16(Console.ReadLine());
PrimeCount objCount = new PrimeCount();
objCount.Result(start, end);
Console.ReadLine();
}
}
}