In: Computer Science
c#
code working but output not right, I need to output all numbers like :
Prime factors of 4 are: 2 x 2 here is just 2
Prime factors of 7 are: 7
Prime factors of 30 are: 2 x 3 x 5
Prime factors of 40 are: 2 x 2 x 2 x 5 here is just 2,5
Prime factors of 50 are: 2 x 5 x 5 here is just 2,5
1) How I can fix it
2)I need few unit tests for code
using System;
namespace PrimeFactor
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number greater than 1: ");
int entry =Convert.ToInt32(Console.ReadLine());
int num = entry;
int testFactor = 1;
string primeString = "";
while (++testFactor <= num)
{
primeString += (num % testFactor == 0) ? " " + testFactor :
"";
while (num % testFactor == 0) num /= testFactor;
}
Console.WriteLine("The prime factors of " + entry + " are:" +
primeString);
Console.ReadKey();
}
}
}
Program
using System;
namespace PrimeFactor
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number greater than 1: ");
int entry =Convert.ToInt32(Console.ReadLine());
int num = entry;
int testFactor = 1;
string primeString = "";
while (++testFactor <= num)
{
while (num % testFactor == 0)
{
primeString +=" " + testFactor ;
num/= testFactor ;
}
}
Console.WriteLine("The prime factors of " + entry + " are:" +
primeString);
Console.ReadKey();
}
}
}
Sample output
Enter a number greater than 1:
4
The prime factors of 4 are: 2 2
Enter a number greater than 1:
7
The prime factors of 7 are: 7
Enter a number greater than 1:
40
The prime factors of 40 are: 2 2 2 5