.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

Check the number is Prime or not

Posted By: Aditya Varma 999     Posted Date: March 14, 2014    Points:5   Category :C#
Please Check the following code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Prime2
{
class CheckPrime
{
//Return true If x is Prime
#region IsPrimebooleanLogic
public bool IsPrime(int x)
{
for (int i = 2; i <= x / i; i++)
{
if ((x % i) == 0)
{
return false;
}
else
return true;
}
}
#endregion
}

class CheckPrimeDemo
{
static void Main(string[] args)
{
CheckPrime opt = new CheckPrime();

int end;
Console.WriteLine("Enter the Ending Number for Prime number search");
end = int.Parse(Console.ReadLine());
for (int i = 1; i < 10; i++)
{
if (opt.IsPrime(i))
Console.WriteLine(i+ " is Prime Number");
else
Console.WriteLine(i + " is Not Prime Number");
}
}

}

}


Responses
Author: karthik             
Posted Date: May 09, 2014     Points: 20   

nt num1;

Console.WriteLine("Accept number:");
num1 = Convert.ToInt32(Console.ReadLine());
if(isPrime(num1))
{
Console.WriteLine("It is prime");
}
else
{
Console.WriteLine("It is not prime");
}

public static boolean isPrime(int number)
{
int boundary = Math.Floor(Math.Sqrt(number));

if (number == 1) return false;
if (number == 2) return true;

for (int i = 2; i <= boundary; ++i) {
if (number % i == 0) return false;
}

return true;
}



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend