Question

In: Computer Science

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 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

Program:

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!!");
    }
  }
}

Output:


Related Solutions

ANSWER ALL 11 QUESTIONS PLEASE DON'T !!! ANSWER IF YOU DON'T KNOW ALL THE ANSWERS THANK...
ANSWER ALL 11 QUESTIONS PLEASE DON'T !!! ANSWER IF YOU DON'T KNOW ALL THE ANSWERS THANK YOU. 1. What things affect airflow and which one is the most important? 2. Explain how an asthma attack could create a life-threatening condition? 3. Explain how emphysema is associated with expiratory flow limitation and its consequence on the person’s health 4. What are the muscles of inspiration? 5. What role do these muscles perform? 6. What are the primary sources of resistance for...
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,...
IMPORTANT: I know the answer is "C". However, I don't know why. Could you please explain...
IMPORTANT: I know the answer is "C". However, I don't know why. Could you please explain why? Thank you A linear total cost curve that passes through the origin implies that a.         average cost is constant and marginal cost is variable. b.         average cost is variable and marginal cost is constant.             c.         average and marginal costs are constant and equal.             d.         you need more information to answer question.
Database Normalization Please if you don't know the answer don't comment as "Need More Information." Please...
Database Normalization Please if you don't know the answer don't comment as "Need More Information." Please answer it in a table form with the data included as per table A and table B. Introduction: This lab is designed to help you with practicing normalization concepts implementation. Submission: After finishing the task below, convert the word file to a PDF document and submit it to Brightspace. Task: Using this file, normalize the following tables to be in the third normal form....
please i need unique answer , don't copy and paste ,, don't use handwriting.. can you...
please i need unique answer , don't copy and paste ,, don't use handwriting.. can you complete my answer , i need you answer b only Question: 3- Al Yamamah Steel Industries Co. uses the step method for allocating the costs of its service departments to operating departments. The company has two support departments (Human Resource and Information Technology) and two operating departments (Hot Rolled Hollow Steel and Cold Rolled Hollow Steel). Al Yamamah Steel Industries Co. decided to allocate...
PLEASE DO NOT COPY OTHERS ANSWER, THANK YOU! Let x1, x2, · · · , xn...
PLEASE DO NOT COPY OTHERS ANSWER, THANK YOU! Let x1, x2, · · · , xn ∈ {0, 1}. (a) (10 points) Consider the equation x1 + x2 + · · · + xn = 0 mod 2. How many solutions does this equation have? Express your answer in terms of n. For example, if n = 2, x1 + x2 = 0 has 2 solutions: (x1, x2) = (0, 0) and (x1, x2) = (1, 1). (b) (5 points)...
****** please don't copy and paste and don't use handwriting ****** I need a unique answer...
****** please don't copy and paste and don't use handwriting ****** I need a unique answer Q1: Write HTML code to get the following output that include the following elements. The title is: Favorite Books Large heading with text “Your Favorite Books” Form that has 3 input values (two text boxes and one drop down list) Drop-down list contains the following options, Pearson Wiley Mc-Graw Hill Submit information to “books.php” using GET request. Q2:Display a simple message "Welcome" on your...
Need new and unique answers. Please don't copy and paste. Thank you. Q1.Discuss the process of...
Need new and unique answers. Please don't copy and paste. Thank you. Q1.Discuss the process of normalization and its purpose in the context of relational databases. Provide at least one example (preferably a table) of how normalization helps solve certain problems in relations.
Please answer to the point and accurate and please do not copy someone else.. Please don't...
Please answer to the point and accurate and please do not copy someone else.. Please don't go for length, 200-300 words will be fine but please very accurate and very careful. I will be very much thankful. I am preperation for exam and it is tough for me. 1. Explain why companies do not consolidate all subsidiaries
Database Normalization Please if you don't know the answer don't comment as "Need More Information." Introduction:...
Database Normalization Please if you don't know the answer don't comment as "Need More Information." Introduction: This lab is designed to help you with practicing normalization concepts implementation. Submission: After finishing the task below, convert the word file to a PDF document and submit it to Brightspace. Task: Using this file, normalize the following tables to be in the third normal form. Remember to consider having the data when you do the normalization. Course_Title Course_Credit_Hours Professor_ID Professor_Name First_day_work Professor Specialization_ID...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT