Client loves numbers so much that he tries to find some quirky math bricks whenever he can. One day, he decides to take up a number and tries to find out its largest prime divisor. Note: Largest prime divisor has to be greater that 1 and less than the number itself. If there is no such divisor then return -1.
using System;
namespace prime_divisor
{
class Program
{
public static int prime_divisor(int num)
{
int i, j, temp;
for (i = num-1; i > 1; i--)
{
if ((num % i) == 0)
{
temp = 0;
for (j = i-1; j > 1; j--)
if ((i % j) == 0)
{
temp = 1;
break;
}
if (temp == 0)
return i;
}
}
return -1;
}
public static void Main(string[] args)
{
int number;
Console.Write("Enter number: ");
number = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("largest prime divisor: {0}", prime_divisor(number));
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
Comments
Leave a comment