Reply to thread

Hello, I am trying to translate my python code, which was a homework assignment in my computer science course, to C#. But I tried to the best of my ability, and when it ran I entered the number, then it just froze. Here is the C# code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace PrimeFactorization

{

    class Program

    {

        public static bool isPrime(int n)

        {

            bool prime = true;

            int div = 2;


            for (int i = div; i < n; i++)

        {

                if (n % i == 0)

                    prime = false;

            

        }

            return prime;

        } 

     

        public static int primeFactorPower(int n, int p)

        {

            int count = 1;


            while (n % (Math.Pow(p, count)) == 0)

            {

                count += 1;

            }


            return count - 1;

        }


        public static void displayFactorization(int num)

        {

            for (int p = 0; p < num + 1; p++)

            {

                if (isPrime(p))

                {

                    int e = primeFactorPower(num, p);

                   

                    if (e != 0)

                    {

                        Console.Write(p + "^" + e);

                        Console.ReadLine();

                    }


                }

            }


        }


        static void Main(string[] args)

        {

 

            Console.Write("Enter a positive integer (0 or negative to quit): ");

            int number = int.Parse(Console.ReadLine());


            while (number > 0)

            {

                displayFactorization(number);

                Console.Write("\nEnter a positive integer (0 or negative to quit): ");

                number = int.Parse(Console.ReadLine());


               

            }


        }

    }

}

As you can probably tell, this is a simple program with one class and is just an exercise in syntax for me and is really just for fun. However, I am curious as to why it just stops working after I enter the number variable.


Continue reading...


Back
Top