Description:
In this example we will learn how to get list of prime numbers upto given numbers. Here this example is done under console application. |
C# Code:
using System;
namespace ConsoleApplication1
{
class Class1
{
public static void Main()
{
Class1 p = new Class1();
Console.WriteLine("Enter number");
int n = int.Parse(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("Prime numbers upto {0}", n);
for (int i = 2; i <= n; i++)
{
p.DisplayPrime(i);
}
Console.ReadLine();
}
public void DisplayPrime(int n)
{
Class1 p = new Class1();
if (p.isprime(n))
{
Console.WriteLine(n);
}
}
public bool isprime(int n)
{
for (int i = 2; i <= n / 2; i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
}
}
|
Result:
|
Subscribe to:
Posts (Atom)
No comments:
Post a Comment