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

Write a program that accepts a single integer input from the user. The program w

ID: 3669040 • Letter: W

Question

Write a program that accepts a single integer input from the user. The program will then determine whether or not the number is prime. If the number is prime, a message indicating this is displayed. If the number is composite, a message indicating that the number is composite is displayed. In addition to this, the prime factors for the number are displayed. The prime factors do not have to be in order, but they must all be shown. For the sake of simplicity, we will only consider integers that are greater than or equal to 2 (x is on [2, ) Z). If the user enters a number that is not within this range, display a message indicating that the number entered is not being considered. C++ THANK YOU

Sample outputs:

Explanation / Answer

#include <iostream>
using namespace std;

bool isPrime(int n)
{
int i;
for(i=2;i<=n/2;++i)
{
    if(n%i==0)
      return false;
}
return true;
}

int main()
{
int n, i;
int composite[1000];
int count = 0;

cout << "Enter a positive integer: ";
cin >> n;
if(n<2)
    {
      cout<<"Number not within range [2, inf)"<<endl;
      return 1;
    }


for(i=2;i<=n/2;++i)
{
      if(n%i==0 && isPrime(i) )
        composite[count++] = i;

}


if (count==0)
      cout << "This is a prime number ";
else{
    cout << "This is a composite ";
    cout<<"Prime factors are ";
    for(i=0;i<count;i++)
      cout<<composite[i]<<" ";
}
cout<<endl;
    
return 0;
}

//Images are not opening

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