Wednesday, 24 April 2013

Write a program to accept a number from the user and display all the prime numbers from one up to the number entered by user. (Duration: 40 min)


using System;
class primeNumber
{
public void PrimeNum(int Num)
{
Boolean isPrime=true;
Console.WriteLine("\nThe prime numbers between 1-{0} is:",Num);
for (int i = 0; i <=Num; i++)
{
for (int j = 2; j<=Num; j++)
{
if (i !=j && i == 0)
{
isPrime = false;
break;
}
}
if (isPrime == true)
{Console.WriteLine("{0}",i);}
isPrime = true;
}
}
}
class EntryPoint
{
static void Main (string[]args)
{
int Num;
PrimeNumber FindPrimeNo = new PrimeNumber();
string str;
Console.WriteLine("Enter the number till which you want to show the prime Numbers");
str = Convert.ReadLint();
Num = Convert.PrimeNum(Num);
Console.ReadLint();
}
}

No comments:

Post a Comment