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?
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(); } } }
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();
}
}
}