Skip to main content
deleted 230 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Largest Prime Factor - My first console application Finding largest prime factor from inputted number

This is my first C# Console Applicationconsole application. This program asks for an input of a number, and finds the highest prime factor of that number. Please review my code.

  • Have I used things right?

    Have I used things right?
  • Is my code efficient?

    Is my code efficient?
  • Is there are a better way to achieve this?

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using System.Threading.Tasks;
    
     namespace Problems
     {
         class Program
         {
             static int FindFactor(Int64 UserInput)
             {
                 int LargestFactor = 0;
                 Int64 Number = UserInput;
    
                 for (int i = 2; i < Number; i++)
                 {
                     if (Number % i == 0)
                     { 
                         if (PrimeFactorCheck(i) == true)
                         {
                             LargestFactor = i;
                         }
                     }
                 }
                 return LargestFactor;
             }
             static bool PrimeFactorCheck(int Number)
             {
                 bool PrimeNumberValidity = true;
    
                 for(int i = 2; i < Number; i++)
                 {
                     if (PrimeNumberValidity == true)
                     {
                         if (Number % i == 0)
                         {
                             PrimeNumberValidity = false;
                         }
                     }
                     else
                     {
                         i = Number;
                     }
                 }
    
                 return PrimeNumberValidity;
             }
             static void Main(string[] args)
             {
                 Console.Write("Hello, please enter a number to find the biggest prime factor of it: ");
                  Int64 UserInput = Convert.ToInt64(Console.ReadLine());
    
                 int Answer = FindFactor(UserInput);
                 Console.WriteLine(Answer);
                 Console.ReadKey();
             }
         }
     }
    
    Is there a better way to achieve this?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Problems
{
    class Program
    {
        static int FindFactor(Int64 UserInput)
        {
            int LargestFactor = 0;
            Int64 Number = UserInput;

            for (int i = 2; i < Number; i++)
            {
                if (Number % i == 0)
                { 
                    if (PrimeFactorCheck(i) == true)
                    {
                        LargestFactor = i;
                    }
                }
            }
            return LargestFactor;
        }
        static bool PrimeFactorCheck(int Number)
        {
            bool PrimeNumberValidity = true;

            for(int i = 2; i < Number; i++)
            {
                if (PrimeNumberValidity == true)
                {
                    if (Number % i == 0)
                    {
                        PrimeNumberValidity = false;
                    }
                }
                else
                {
                    i = Number;
                }
            }

            return PrimeNumberValidity;
        }
        static void Main(string[] args)
        {
            Console.Write("Hello, please enter a number to find the biggest prime factor of it: ");
             Int64 UserInput = Convert.ToInt64(Console.ReadLine());

            int Answer = FindFactor(UserInput);
            Console.WriteLine(Answer);
            Console.ReadKey();
        }
    }
}

Largest Prime Factor - My first console application

This is my first C# Console Application. This program asks for an input of a number, and finds the highest prime factor of that number. Please review my code.

  • Have I used things right?

  • Is my code efficient?

  • Is there are a better way to achieve this?

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using System.Threading.Tasks;
    
     namespace Problems
     {
         class Program
         {
             static int FindFactor(Int64 UserInput)
             {
                 int LargestFactor = 0;
                 Int64 Number = UserInput;
    
                 for (int i = 2; i < Number; i++)
                 {
                     if (Number % i == 0)
                     { 
                         if (PrimeFactorCheck(i) == true)
                         {
                             LargestFactor = i;
                         }
                     }
                 }
                 return LargestFactor;
             }
             static bool PrimeFactorCheck(int Number)
             {
                 bool PrimeNumberValidity = true;
    
                 for(int i = 2; i < Number; i++)
                 {
                     if (PrimeNumberValidity == true)
                     {
                         if (Number % i == 0)
                         {
                             PrimeNumberValidity = false;
                         }
                     }
                     else
                     {
                         i = Number;
                     }
                 }
    
                 return PrimeNumberValidity;
             }
             static void Main(string[] args)
             {
                 Console.Write("Hello, please enter a number to find the biggest prime factor of it: ");
                  Int64 UserInput = Convert.ToInt64(Console.ReadLine());
    
                 int Answer = FindFactor(UserInput);
                 Console.WriteLine(Answer);
                 Console.ReadKey();
             }
         }
     }
    

Finding largest prime factor from inputted number

This is my first C# console application. This program asks for an input of a number, and finds the highest prime factor of that number. Please review my code.

  • Have I used things right?
  • Is my code efficient?
  • Is there a better way to achieve this?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Problems
{
    class Program
    {
        static int FindFactor(Int64 UserInput)
        {
            int LargestFactor = 0;
            Int64 Number = UserInput;

            for (int i = 2; i < Number; i++)
            {
                if (Number % i == 0)
                { 
                    if (PrimeFactorCheck(i) == true)
                    {
                        LargestFactor = i;
                    }
                }
            }
            return LargestFactor;
        }
        static bool PrimeFactorCheck(int Number)
        {
            bool PrimeNumberValidity = true;

            for(int i = 2; i < Number; i++)
            {
                if (PrimeNumberValidity == true)
                {
                    if (Number % i == 0)
                    {
                        PrimeNumberValidity = false;
                    }
                }
                else
                {
                    i = Number;
                }
            }

            return PrimeNumberValidity;
        }
        static void Main(string[] args)
        {
            Console.Write("Hello, please enter a number to find the biggest prime factor of it: ");
             Int64 UserInput = Convert.ToInt64(Console.ReadLine());

            int Answer = FindFactor(UserInput);
            Console.WriteLine(Answer);
            Console.ReadKey();
        }
    }
}
edited tags
Link
200_success
  • 145.4k
  • 22
  • 190
  • 478
Tweeted twitter.com/#!/StackCodeReview/status/491924878841622528
Moved intro before code; improved title; added tagz
Source Link
RubberDuck
  • 31.1k
  • 6
  • 73
  • 176

Largest Prime Factor - My first console application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Problems
{
    class Program
    {
        static int FindFactor(Int64 UserInput)
        {
            int LargestFactor = 0;
            Int64 Number = UserInput;

            for (int i = 2; i < Number; i++)
            {
                if (Number % i == 0)
                {
                    if (PrimeFactorCheck(i) == true)
                    {
                        LargestFactor = i;
                    }
                }
            }
            return LargestFactor;
        }
        static bool PrimeFactorCheck(int Number)
        {
            bool PrimeNumberValidity = true;

            for(int i = 2; i < Number; i++)
            {
                if (PrimeNumberValidity == true)
                {
                    if (Number % i == 0)
                    {
                        PrimeNumberValidity = false;
                    }
                }
                else
                {
                    i = Number;
                }
            }

            return PrimeNumberValidity;
        }
        static void Main(string[] args)
        {
            Console.Write("Hello, please enter a number to find the biggest prime factor of it: ");
            Int64 UserInput = Convert.ToInt64(Console.ReadLine());

            int Answer = FindFactor(UserInput);
            Console.WriteLine(Answer);
            Console.ReadKey();
        }
    }
}

Hello everyone, thisThis is my first C# Console Application. This program asks for an input of a number, and finds the highest prime factor of that number. Please review my use of code, have I used things right? Is my code efficient? Is there are a better way to achieve this? Also, suggestions for the next application to code are also accepted!.

  • Have I used things right?

  • Is my code efficient?

  • Is there are a better way to achieve this?

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using System.Threading.Tasks;
    
     namespace Problems
     {
         class Program
         {
             static int FindFactor(Int64 UserInput)
             {
                 int LargestFactor = 0;
                 Int64 Number = UserInput;
    
                 for (int i = 2; i < Number; i++)
                 {
                     if (Number % i == 0)
                     { 
                         if (PrimeFactorCheck(i) == true)
                         {
                             LargestFactor = i;
                         }
                     }
                 }
                 return LargestFactor;
             }
             static bool PrimeFactorCheck(int Number)
             {
                 bool PrimeNumberValidity = true;
    
                 for(int i = 2; i < Number; i++)
                 {
                     if (PrimeNumberValidity == true)
                     {
                         if (Number % i == 0)
                         {
                             PrimeNumberValidity = false;
                         }
                     }
                     else
                     {
                         i = Number;
                     }
                 }
    
                 return PrimeNumberValidity;
             }
             static void Main(string[] args)
             {
                 Console.Write("Hello, please enter a number to find the biggest prime factor of it: ");
                  Int64 UserInput = Convert.ToInt64(Console.ReadLine());
    
                 int Answer = FindFactor(UserInput);
                 Console.WriteLine(Answer);
                 Console.ReadKey();
             }
         }
     }
    

My first console application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Problems
{
    class Program
    {
        static int FindFactor(Int64 UserInput)
        {
            int LargestFactor = 0;
            Int64 Number = UserInput;

            for (int i = 2; i < Number; i++)
            {
                if (Number % i == 0)
                {
                    if (PrimeFactorCheck(i) == true)
                    {
                        LargestFactor = i;
                    }
                }
            }
            return LargestFactor;
        }
        static bool PrimeFactorCheck(int Number)
        {
            bool PrimeNumberValidity = true;

            for(int i = 2; i < Number; i++)
            {
                if (PrimeNumberValidity == true)
                {
                    if (Number % i == 0)
                    {
                        PrimeNumberValidity = false;
                    }
                }
                else
                {
                    i = Number;
                }
            }

            return PrimeNumberValidity;
        }
        static void Main(string[] args)
        {
            Console.Write("Hello, please enter a number to find the biggest prime factor of it: ");
            Int64 UserInput = Convert.ToInt64(Console.ReadLine());

            int Answer = FindFactor(UserInput);
            Console.WriteLine(Answer);
            Console.ReadKey();
        }
    }
}

Hello everyone, this is my first C# Console Application. This program asks for an input of a number, and finds the highest prime factor of that number. Please review my use of code, have I used things right? Is my code efficient? Is there are a better way to achieve this? Also, suggestions for the next application to code are also accepted!

Largest Prime Factor - My first console application

This is my first C# Console Application. This program asks for an input of a number, and finds the highest prime factor of that number. Please review my code.

  • Have I used things right?

  • Is my code efficient?

  • Is there are a better way to achieve this?

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using System.Threading.Tasks;
    
     namespace Problems
     {
         class Program
         {
             static int FindFactor(Int64 UserInput)
             {
                 int LargestFactor = 0;
                 Int64 Number = UserInput;
    
                 for (int i = 2; i < Number; i++)
                 {
                     if (Number % i == 0)
                     { 
                         if (PrimeFactorCheck(i) == true)
                         {
                             LargestFactor = i;
                         }
                     }
                 }
                 return LargestFactor;
             }
             static bool PrimeFactorCheck(int Number)
             {
                 bool PrimeNumberValidity = true;
    
                 for(int i = 2; i < Number; i++)
                 {
                     if (PrimeNumberValidity == true)
                     {
                         if (Number % i == 0)
                         {
                             PrimeNumberValidity = false;
                         }
                     }
                     else
                     {
                         i = Number;
                     }
                 }
    
                 return PrimeNumberValidity;
             }
             static void Main(string[] args)
             {
                 Console.Write("Hello, please enter a number to find the biggest prime factor of it: ");
                  Int64 UserInput = Convert.ToInt64(Console.ReadLine());
    
                 int Answer = FindFactor(UserInput);
                 Console.WriteLine(Answer);
                 Console.ReadKey();
             }
         }
     }
    
Source Link
Abandoned Account
  • 1.2k
  • 3
  • 14
  • 30
Loading