Hello friends
Can some post here Prime Number programme in C#. i tried that but it giving me error.
Thanks
Printable View
Hello friends
Can some post here Prime Number programme in C#. i tried that but it giving me error.
Thanks
Here is C# code to display given no is Prime Number or not
Code:using System;
using System.Collections.Generic;
using System.Text;
namespace Exercise_1
{
class Prime
{
public void Prime_Number(int number)
{
int i;
for (i = 2; i < number; i++)
{
if (number % i == 0)
{
Console.WriteLine("{0} is not a prime number", number);
break;
}
}
if(i == number)
{
Console.WriteLine("{0} is a prime number", number);
}
}
static void Main(string[] args)
{
int num;
Console.WriteLine("Enter the number: ");
num = Convert.ToInt32(Console.ReadLine());
Prime p = new Prime();
p.Prime_Number(num);
}
}
}
Code:using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
bool current = false;
int j;
Console.WriteLine("Enter any integer");
int num = Int32.Parse(Console.ReadLine());
for (int i = 2; i <= num; i++)//2 is the first <strong class="highlight">prime</strong> <strong class="highlight">number</strong>.
//I set i to 2 beacuse it has to print 2 firstly...
{
for (j = 2; j < i; j++)
{
if (i % j == 0)//Controls i is <strong class="highlight">prime</strong> <strong class="highlight">number</strong> or not...
{
current = true;
break;//breaks for not controlling anymore...
}
}
if (current == false)
Console.Write("{0} ", j);//if i is <strong class="highlight">prime</strong> <strong class="highlight">number</strong>, print it...
else
current = false;
}
Console.ReadLine();
}
}
}