using System;
namespace st
{
class HighestLowestFactors
{
public static void Main()
{
int highfact, lowfact;
Console.WriteLine("Enter a number : ");
int num = Int32.Parse(Console.ReadLine());
GetHighestLowestFactors( num, out highfact, out lowfact);
Console.WriteLine(
"For number {0}, Highest Factor is {1} and Lowest Factor is : {2}",
num, highfact,lowfact );
}
public static void GetHighestLowestFactors(int num, out int hf, out int lf)
{
// get lowest factor other than 1. In case of prime number 1
lf = 1;
for (int i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
lf = i;
break;
}
}
// get highest factor other than the number. In case of prime number it is number itself
hf = num;
for (int i = num/2 ; i > 1; i--)
{
if (num % i == 0)
{
hf = i;
break;
}
} // for
} // GetHighestLowestFactors
} // class
} // namespace
Comments
Post a Comment