Question

In: Computer Science

C# Prime factors are the combination of the smallest prime numbers, that, when multiplied together, will...

C#

Prime factors are the combination of the smallest prime numbers, that, when multiplied together, will produce the original number. Consider the following example:

Prime factors of 4 are: 2 x 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

Prime factors of 50 are: 2 x 5 x 5

Create a console application with a method named PrimeFactors that, when passed an int variable as a parameter, returns a string showing its prime factors.

Use the debugging tools and write unit tests to ensure that your function works correctly with multiple inputs and returns the correct output.

Solutions

Expert Solution

SOLUTION-
I have solve the problem in C# code with comments and screenshot for easy understanding :)

CODE-

//c# code
using System;
class HelloWorld {
  
public static void PrimeFactors(int num) {
Console.Write($"Prime Factors of {num} are: ");

// for even numbers
while (num % 2 == 0)
{
Console.Write("2 ");
num /= 2;
}
  
// if n is an odd number
for(int i = 3; i <= Math.Sqrt(num); i += 2)
{
while (num % i == 0)
{
Console.Write($"{i} ");
num /= i;
}
}

// If n is greater than 2 and Prime number
if (num > 2)
{
Console.Write($"{num} ");
}
}
static void Main() {
Console.Write("Enter a number greater than 1: ");
if (int.TryParse(Console.ReadLine(), out int num) && num > 1)
{
PrimeFactors(num);
}
else
{
Console.WriteLine("Invalid input!!");
}
}
}


SCREENSHOT-


IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

C# please answer if you know, don't copy others answers, Prime factors are the combination of...
C# please answer if you know, don't copy others answers, Prime factors are the combination of the smallest prime numbers, that, when multiplied together, will produce the original number. Consider the following example: Prime factors of 4 are: 2 x 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 Prime factors of 50 are: 2 x 5 x 5 Create...
Problem DescriptionFind all numbers that when multiplied by 365 produce an eightdigit product where...
Problem Description Find all numbers that when multiplied by 365 produce an eight digit product where the first four digits of that product are equal to the last four digits of that product. For example, 271123 time 365 produces an eight digit product (98959895) whose first four digits (9895) are equal to its last four digits (9895). Input/Output There is no input for this problem. The output will be a list of the first ten integers that when multiplied by...
Write a smallest_gap(start_num, end_num) function that finds smallest gap between successive primes, considering prime numbers in...
Write a smallest_gap(start_num, end_num) function that finds smallest gap between successive primes, considering prime numbers in the range from start_num to end_num (inclusive). For example, start_num = 5 and end_num = 12, the prime numbers in that range are: [5, 7, 11]. The smallest gap between any two prime numbers in this list is 2, so the function would return 2. You may want to modify your solution from Problem 1 on Assignment 4, or you can use this starter...
Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
Please use minimum cost rule to solve this problem. When the smallest numbers in a tie...
Please use minimum cost rule to solve this problem. When the smallest numbers in a tie in the same column?How to determine which one should be picked? And It they are in the same row and the demands are also the same, how to pick? Thank you Transportation Tableau Steel1 Steel 2 Steel 3 Dummy Plant 1 60 40 28 0 120 Plant 2 50 30 30 0 150 Plant 3 20 20 20 0 160 100 100 100 130...
(Prime Numbers) An integer is said to be prime if it is divisible by only 1...
(Prime Numbers) An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. Write pseudocode and function called isPrime that receives an integer and determines whether the integer is prime or not. Write a test program that uses isPrime to determine and print all the prime numbers between 1 and 1000. Display 10 numbers per line. Twin primes...
write a code using c++. Find the first 10 prime numbers and store them in an...
write a code using c++. Find the first 10 prime numbers and store them in an array.
Question : Write a C++ program to find all prime numbers between 10 to 100 by...
Question : Write a C++ program to find all prime numbers between 10 to 100 by using while loop. Hint: a prime number is a number that is divisible by 1 and itself. For example 3, 5, 7, 11, 13 are prime numbers because they are only divisible by 1 and themselves.
How is the sound different when both tuning forks C AND C prime as opposed to...
How is the sound different when both tuning forks C AND C prime as opposed to a signal tuning are ringing? Why the measured beat minimum amplitude is not zero try to create empirical formulas for A_beat_max=f (Ac, Ac’) and A_beat_min=f (Ac, Ac’): List the frequencies found for the tuning forks and how do these frequencies compare to the frequencies of the notes given in lecture?
Given the prime factors p and​ q, the encryption exponent​ e, and the ciphertext​ C, apply...
Given the prime factors p and​ q, the encryption exponent​ e, and the ciphertext​ C, apply the RSA algorithm to find ​(a) the decryption exponent d and ​(b) the plaintext message M. p q e C 17 5 19 65 I have to get d and M
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT