Step 1:- Method
public void PrintPrimeNumber1()
{
int count,
num, nd = 0;
Console.WriteLine("Enter
a positive integer...");
num
= Convert.ToInt32(Console.ReadLine());
if (num
== 1)
Console.WriteLine("The
number is not a prime number");
else
{
for (count
= 1; count <= num; count++)
{
if (num
% count == 0)
nd++;
}
if (nd
== 2)
Console.WriteLine("The
given number is prime number");
else
Console.WriteLine("The
given number is not a prime number");
}
}
public void PrintPrimeNumber2()
{
int count,
num = 1, nd;
Console.WriteLine("The
prime number 1-100 are as follows");
while (num
<= 100)
{
nd
= 0;
for (count
= 1; count <= num; count++)
if (num
% count == 0)
nd++;
if (nd
== 2)
Console.WriteLine(num);
num++;
}
}
Step 2:- Complete code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_Application_Demo
{
class Program
{
public void PrintPrimeNumber1()
{
int count,
num, nd = 0;
Console.WriteLine("Enter
a positive integer...");
num
= Convert.ToInt32(Console.ReadLine());
if (num
== 1)
Console.WriteLine("The
number is not a prime number");
else
{
for (count
= 1; count <= num; count++)
{
if (num
% count == 0)
nd++;
}
if (nd
== 2)
Console.WriteLine("The
given number is prime number");
else
Console.WriteLine("The
given number is not a prime number");
}
}
public void PrintPrimeNumber2()
{
int count,
num = 1, nd;
Console.WriteLine("The
prime number 1-100 are as follows");
while (num
<= 100)
{
nd
= 0;
for (count
= 1; count <= num; count++)
if (num
% count == 0)
nd++;
if (nd
== 2)
Console.WriteLine(num);
num++;
}
}
static void Main(string[]
args)
{
Program p
= new Program();
p.PrintPrimeNumber1();
p.PrintPrimeNumber2();
Console.ReadLine();
}
}
}
Example:-
No comments:
Post a Comment