In: Computer Science
c#
Please write unit tests for this code, at least one or two that I can understand how to write it.
using System;
namespace PrimeNumberFactors
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a number you want to check: ");
if (int.TryParse(Console.ReadLine(), out int num))
{
PrimeFactors(num);
}
else
{
Console.WriteLine("Invalid input!!");
}
Console.ReadKey();
}
public static void PrimeFactors(int num)
{
Console.Write($"Prime Factors of {num} are: ");
while (num % 2 == 0)
{
Console.Write("2 ");
num /= 2;
}
for (int i = 3; i <= Math.Sqrt(num); i += 2)
{
while (num % i == 0)
{
Console.Write($"{i} ");
num /= i;
}
}
if (num > 2)
{
Console.Write($"{num} ");
}
}
}
}
Short Summary:
Solution:
Unit Test 1: Enter string characters - passed
Unit Test 2: Enter 0 ( 0 is neither prime or composite, it does not have any factors) – failed, it is in endless loop.
Unit Test 3: Enter 1 (it does not have any factors)
Unit Test 4: Enter 2
Unit Test 5: Enter 3
Unit Test 5: Enter 50
**************************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
****************************************************************************************