Prime Factors In C#

后端 未结 6 1875
无人共我
无人共我 2020-12-09 04:41

I want to create a program in C# 2005 which calculates prime factors of a given input. i want to use the basic and simplest things, no need to create a method for it nor arr

6条回答
  •  情书的邮戳
    2020-12-09 05:21

    int a, b;
    Console.WriteLine("Please enter your integer: ");
    a = int.Parse(Console.ReadLine());
    
    for (b = 2; a > 1; b++)
        if (a % b == 0)
        {
            int x = 0;
            while (a % b == 0)
            {
                a /= b;
                x++;
            }
            Console.WriteLine($"{b} is a prime factor {x} times!");
        }
    Console.WriteLine("Th-Th-Th-Th-Th-... That's all, folks!");
    

    Works on my machine!

提交回复
热议问题