Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Write a program that generates random factors. The user should enter a three

ID: 3729046 • Letter: 2

Question

2. Write a program that generates random factors. The user should enter a three digit number. Then the program should generate 4 numbers that are factors of that number (excluding 1). If the number is a prime number the program should state that. (you should develop a function that check if the number is a prime, function that generates a number within an appropriate range and a function to generate a factor) Hint: A three digit number can have a maximum of 32 factors Sample run: Please enter a number (-1 to stop): 300 The random factors are 3,300,150, 2 Please enter a number (-1 to stop): 101 This number is prime Please enter a number (-1 to stop): -1 Thank you

Explanation / Answer

#include <iostream>

#include <stdlib>

using namespace std;

// Function prototype (declaration)

int prime(int);

int main(void)

{

int num,i,j,v,count=0;

cout<<"Please enter the number ( -1 to stop): ";

cin >> num;

if(num<=0)

cout<<"Thank you!";

else

{

// Function call

j = prime(num);

if(j==1) \ Number of factor of any number lesss than that number one means 1 a,prime number.  

cout<<"This number is prime";

else

{

for(int k=1;k<=num;k++)

{

cout<<"The random factor are ";

v = rand() % 999 + 2; \this function produce random number from 2 to 999

if(num%v==0 && count<=4)

{

cout<<v<<",";

count++;

}

}

}

}

}

// Function definition

int prime(int a)

{

int i,count=0;

for(i=1;i<a;i++)

{

if(a%i==0)

count++;

}

// Return statement

return count;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote