Answer to Question #325250 in C# for Anji

Question #325250

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.


1
Expert's answer
2022-04-07T07:30:35-0400
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);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS