- #1
rollcast
- 408
- 0
I'm trying to write a program that will find and then display the prime factor of a number.
I have written this in C# but it works for some numbers but doesn't work for others?
I have written this in C# but it works for some numbers but doesn't work for others?
Code:
using System;
class primefactors
{
public static void Main()
{
int number;
bool isprime = true;
Console.WriteLine("Please enter the number you wish to find the prime factors of:");
number = int.Parse(Console.ReadLine());
Console.WriteLine("The prime factors of {0} are;", number);
for (int i = 2; i < number; i++)
{
if (number % i == 0)
{
for (int j = 2; j < i; j++)
{
if (i % j == 0 && i != j)
{
isprime = false;
}
}
if (isprime == true)
{
Console.WriteLine(i);
}
}
}
Console.WriteLine("Press ANY Key to continue");
Console.ReadKey();
}
}