Question

Use C++ to implement the following program about Prime Factorization of a Number. Do BOTH parts...

Use C++ to implement the following program about Prime Factorization of a Number. Do BOTH parts of the problem or you will lose points. Provide comments to explain each step.

a. Write a function that takes as a parameter a positive integer and returns a list (array) of the prime factors of the given integer. For example, if the parameter is 20, you should return 2 2 5.

b. Write a function that tests the above function by asking the user for input and displaying the output to screen. Your function should display the following menu:

1) Find the prime factorization of a number.

2) Quit.

Homework Answers

Answer #1

PROGRAM:

# include <iostream>

# include <math.h>

//method for finding the prime factor

void primeFactorsOfANumber(int num)

{

//no prime factor exists for 0 and 1

if(num==0||num==1)

{

std::cout<<"\n No primefactor\n";

}

else{

//if the number is even,print 2 and divide the number by 2

while (num%2 == 0)

{

std::cout<< 2 <<" ";

num = num/2;

}

  

   //initialize i=3 and i less than squareroot of the number

for (int i = 3; i <= sqrt(num); i = i+2)

{

//remainder of the number divided by i equal to 0

while (num%i == 0)

{

std::cout<< i<<" ";

num = num/i;

}

}

  

//number is greater than 2,print the number

if (num > 2)

{

std::cout<< num<<" ";

}

}

}

// Driver program

int main()

{

int num;

int choice;

do{

//menu

std::cout<<"\n1.Find The primeFactorial of a number\n";

std::cout<<"2. Quit\n";

std::cout<<"Enter your choice: ";

std::cin>>choice;

switch(choice)

{

case 1:

std::cout<<"\nEnter the NUmber : \n";

std::cin>>num;

//calling the function primeFactorsOfANumber

primeFactorsOfANumber(num);

break;

case 2:

//exit

return 0;

break;

}

}

while(1);

return 0;

}


OUTPUT:

1.Find The primeFactorial of a number

2. Quit

Enter your choice: 1

Enter the NUmber :

11

11

1.Find The primeFactorial of a number

2. Quit

Enter your choice: 1

Enter the NUmber :

24

2 2 2 3

1.Find The primeFactorial of a number

2. Quit

Enter your choice: 2

Program ended with exit code: 0

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a C program Design a program that uses an array to store 10 randomly generated...
Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
1) Write a function in C that takes in a single integer parameter and returns an...
1) Write a function in C that takes in a single integer parameter and returns an array of factors. The function signature should be : int* get_factors(int number) To make things a little easier, assume that the input (number) will never have more than 100 factors. The returned array should be null-terminated. 2) Write a main() function that asks a user for a number, calls the function above, then prints the results.
Write some C++ program segments that solves the indicated tasks (you do not have to write...
Write some C++ program segments that solves the indicated tasks (you do not have to write a complete program, nor be concerned about "good" output; a small code segment will be enough). a) A program that gets a double number from the user, decides whether that number is positive, negative, or zero and display its decision on the screen. a) A function isPositive that takes as input a double number and returns the integer 1 if the number is positive,...
Programing lanugaue is C++ Plan and code a menu-driven modular program utilizing an array An input...
Programing lanugaue is C++ Plan and code a menu-driven modular program utilizing an array An input file has an unknown number of numeric values(could be empty too).Read the numbers from the input fileand store even numbers in one arrayand odd numbers in another array.Create menu options to Display count of even numbers, count of odd numbersand the sum values in each array Determine the average of each array Determine the median of each array Sort each array in ascending order(use...
Collapse Write a program that prompts the user to input a positive integer. It should then...
Collapse Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd integer is prime if it is not divisible by any odd integer less than or equal to the square root of the number.) Turn in: Your source code for with The name of your program as a comment at the top...
Write the following in C: 2. An integer is said to be prime if it is...
Write the following in C: 2. An integer is said to be prime if it is divisible by only 1 and itself. Write a function called prime() that takes in one integer number, and returns 1 (true) if the number is prime and 0 (false) otherwise. Write a program to generate six random numbers between 1 to 100 and calls function prime() on each one to determine if it is prime or not.
Write a C program to check whether given number is prime or not by performing following...
Write a C program to check whether given number is prime or not by performing following steps: Define an integer variable as given_number.   Use the C function scanf to read the integer variable given_number. Use the conditional statement to find out given_number is prime or not prime. Print the final output as The number is prime or The number is not prime. Compile and test the program on CS Linux systems
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output for proof. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr....