Determining the smallest factor when a specific number is given. (other than 1)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SmallestFactor
{
class Program
{
static void Main(string[] args)
{
/*When a number is given, find the smallest factor other
than 1.
Try reading the number and see if
its less than 1. If its 1, then print 1
as the factor. If its less than 1
then exit. if its greater or equals to 2
find the factor. Using a counter
whenever the remainder becomes zero, as soon
as the counter becomes 2, break
the loop.*/
Console.WriteLine("Enter number for which Smallest Factor needs to be found : ");
int number = Convert.ToInt32(Console.ReadLine());
if (number < 1)
{
Console.WriteLine("Enter number greater than or equal to 1.
Exiting app.");
Console.ReadKey();
return;
}
int counter = 0;
int i = 0;
for (i = 1; i
<= number; i++)
{
if (number == 1)
break;
if (number % i == 0)
{
{
counter = counter + 1;
if (counter == 2)
{
break;
}
}
}
}
Console.WriteLine("The
smallest factor for {0} is {1}", number, i);
Console.ReadKey();
}
}
}
Determining the smallest factor for a set of numbers (between min and max numbers)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SmallestFactor
{
class Program
{
static void Main(string[] args)
{
/* Select the smallest and biggest
numbers. If smallest or biggest is less than 1 or if max is less than min, then
abort. Whenever the modulo is equal to 0 , increment the counter. If counter =
2, then exit and type in the value of i*/
Console.WriteLine("Enter
the minimum number : ");
int minnumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter
the maximum number : ");
int maxnumber = Convert.ToInt32(Console.ReadLine());
if
((minnumber < 1) || (maxnumber < 1) || (maxnumber <= minnumber))
{
Console.WriteLine(@"Either Minimum is less than 1 Or Maximum is less
than 1 Or Maximum is less than Minimum. Exiting Application.");
Console.ReadKey();
return;
}
int i = 0;
int numseq = 0;
for (numseq = minnumber; numseq <= maxnumber;
numseq++)
{
int counter = 0;
for (i = 1; i <= numseq; i++)
{
if (numseq == 1)
break;
if (numseq % i == 0)
{
{
counter = counter +
1;
if (counter == 2)
{
break;
}
}
}
}
Console.WriteLine("The smallest factor for {0} is {1}",
numseq, i);
}
Console.ReadKey();
}
}
}
No comments:
Post a Comment