Question

In: Computer Science

c# code working but output not right, I need to output all numbers like : Prime...

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();
}

}
  
}

Solutions

Expert Solution

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


Related Solutions

I need the output of the code like this in java First we create a new...
I need the output of the code like this in java First we create a new building and display the result: This building has no apartments. Press enter to continue......................... Now we add some apartments to the building and display the result: This building has the following apartments: Unit 1 3 Bedroom Rent $450 per month Currently unavailable Unit 2 2 Bedroom Rent $400 per month Currently available Unit 3 4 Bedroom Rent $1000 per month Currently unavailable Unit 4...
C Code! I need all of the \*TODO*\ sections of this code completed. For this dictionary.c...
C Code! I need all of the \*TODO*\ sections of this code completed. For this dictionary.c program, you should end up with a simple English-French and French-English dictionary with a couple of about 350 words(I've provided 5 of each don't worry about this part). I just need a way to look up a word in a sorted array. You simply need to find a way to identify at which index i a certain word appears in an array of words...
I need the code for following in C++ working for Visual studio please. Thanks Use a...
I need the code for following in C++ working for Visual studio please. Thanks Use a Struct to create a structure for a Player. The Player will have the following data that it needs maintain: Struct Player int health int level string playerName double gameComplete bool isGodMode Create the 2 functions that will do the following: 1) initialize(string aPlayerName) which takes in a playername string and creates a Player struct health= 100 level= 1 playerName = aPlayerName gameComplete = 0...
Using HTML and JavaScript, receive a positive number, n, and output all prime numbers that are...
Using HTML and JavaScript, receive a positive number, n, and output all prime numbers that are smaller than n and have a digit 7. For example, if n is 100, the program should output 7, 17, 37, 47, 67, 71, 73, 79, and 97.
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.
C# I need working code please Write a console application that accepts the following JSON as...
C# I need working code please Write a console application that accepts the following JSON as input: {"menu": { "header": "SVG Viewer", "items": [ {"id": "Open"}, {"id": "OpenNew", "label": "Open New"}, null, {"id": "ZoomIn", "label": "Zoom In"}, {"id": "ZoomOut", "label": "Zoom Out"}, {"id": "OriginalView", "label": "Original View"}, null, {"id": "Quality"}, {"id": "Pause"}, {"id": "Mute"}, null, {"id": "Find", "label": "Find..."}, {"id": "FindAgain", "label": "Find Again"}, {"id": "Copy"}, {"id": "CopyAgain", "label": "Copy Again"}, {"id": "CopySVG", "label": "Copy SVG"}, {"id": "ViewSVG", "label": "View...
this is a python code that i need to covert to C++ code...is this possible? if...
this is a python code that i need to covert to C++ code...is this possible? if so, can you please convert this pythin code to C++? def main(): endProgram = 'no' print while endProgram == 'no': print # declare variables notGreenCost = [0] * 12 goneGreenCost = [0] * 12 savings = [0] * 12 months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] getNotGreen(notGreenCost, months) getGoneGreen(goneGreenCost, months) energySaved(notGreenCost, goneGreenCost, savings) displayInfo(notGreenCost, goneGreenCost, savings, months)...
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:...
hi i need a code that will give me this output, For the multiply_list, the user...
hi i need a code that will give me this output, For the multiply_list, the user will be asked to input the length of the list, then to input each element of the list. For the repeat_tuple, the user is only asked to enter the repetition factor, but not the tuple. Your program should take the list created before and convert it to a tuple. output expected: (**user input**) ******Create your List ****** Enter length of your list: 3 ******...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT