A program which checks a given number is Prime number or not and repeats until user presses something else than ‘Y’.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleFile_Read
{
class Program
{
static void Main(string[] args)
{
int num;
char ch;
do
{
int factors = 0;
Console.Write("Enter a number ");
num= int.Parse(Console.ReadLine());
for (int i = 1; i <= num / 2; i++)
{
if (num % i == 0)
{
factors++;
}
}
if (factors > 1)
Console.WriteLine( "{0} is not a prime number ",num);
else
Console.WriteLine("{0} is a prime number ", num);
Console.Write("Do you want to do it again press Y for yes ");
ch =char.Parse(Console.ReadLine());
}
while (ch == 'y' || ch == 'Y');
}
}
}
Comments
Post a Comment