Question

#include <iostream> using namespace std; int main() {    int i,n;    int counter=0;    cin...

#include <iostream>

using namespace std;

int main()
{
   int i,n;
   int counter=0;
   cin >> n;
   for (i = 1; i <= n; i++)
   {

       if (n % i == 0 )
       {
           counter++;
       }

       /*else
       {
           counter++;
           i++;
       }*/
      
   }
   if (counter == 2)
       cout << n << " is a prime number with only 2 factors ( " << n << " and " << 1 << " )." << endl;
   else
       cout << n << " is not prime. It has more than 2. (" << 1 << " & " <<i<<","<< counter <<")"<< endl;
   return 0;
}

How do you modify this code for non-prime number.

For example, if the input is 4, I want it to display "4 is not a prime because it has more than 2 factors. (1,2,4).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts please comment below. Please give upvote if you like this.

Cpp Code:

#include <bits/stdc++.h>

using namespace std;

int main()
{
int i,n;
int counter=0;
cin >> n;
for (i = 1; i <= n; i++)
{

if (n % i == 0 )
{
counter++;
}
  
}
if (counter == 2)
cout << n << " is a prime number with only 2 factors ( " << n << " and " << 1 << " )." << endl;
else
{
        cout << n << " is not prime. It has more than 2. (";
        // loop for printing factors of n if n is non-prime
        for(int i=1;i<n;i++)
        {
            if(n%i==0) // condition for factor
              cout << i<<","; //display the factors
       }
       cout<<n<<")"<< endl;
}

return 0;
}

Add a comment
Know the answer?
Add Answer to:
#include <iostream> using namespace std; int main() {    int i,n;    int counter=0;    cin...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0;...

    #include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0; while(true) { cout << "Please enter a number between 1 and 11: "; cin >> number; if (number >= 1 && number <= 11) { cout << number << endl; sum = sum + number; //only add the sum when number is in range: 1-11, so add wthin this if case } else { cout << number << endl; cout << "Out of range;...

  • How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0,...

    How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...

  • #include <iostream> using namespace std; int main() {    int sumOdd = 0; // For accumulating...

    #include <iostream> using namespace std; int main() {    int sumOdd = 0; // For accumulating odd numbers, init to 0    int sumEven = 0; // For accumulating even numbers, init to 0    int upperbound; // Sum from 1 to this upperbound    // Prompt user for an upperbound    cout << "Enter the upperbound: ";    cin >> upperbound;    // Use a loop to repeatedly add 1, 2, 3,..., up to upperbound    int number =...

  • Write following program using Switch statement. #include <iostream> using namespace std; int main() int number; cout...

    Write following program using Switch statement. #include <iostream> using namespace std; int main() int number; cout << "Enter an integer cin >> number; if (number > B) cout << You entered a positive integer: " << number << endl; else if (number (8) cout<<"You entered a negative integer: " << number << endl; cout << "You entered e." << endl; cout << "This line is always printed." return 0;

  • #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code...

    #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...

  • Consider the following C++ program: #include <iostream> #include <cstdlib> using namespace std; int main int n...

    Consider the following C++ program: #include <iostream> #include <cstdlib> using namespace std; int main int n =0; int i = 0; cout << "Please enter a strictly positive number:"; cin >> n if (n <= 0) exit(EXIT_FAILURE) while (n > 1) n-n/2; i << endl; cout"Output:" return 0; Answer the following questions: What is the output of the program for each of the following values of n: -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9? What does...

  • #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the...

    #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the array"<<endl;    cin>>SIZE;     int *numlist = new int[SIZE];     // Read SIZE integers from the keyboard     for (int i = 0; i<SIZE; i++ )    {        cout << "Enter value #" << i+1 << ": ";        cin >> numlist[i];     }     // Display the numbers in a reverse order     for (int i = SIZE; i > 0; i--...

  • #include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be...

    #include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be implemented by you } int main() { cout << "To calculate x^y ..." << endl; double x; int y; cout << "Please enter x: "; cin >> x; cout << "Please enter y: "; cin >> y; if(x == 0) { if (y > 0) cout << 0 << endl; else cout << "x^y is not defined" <<endl; } else { cout << improvedPow(x,y)...

  • My C++ code won't loop like it should. What am I doing wrong? #include <iostream> using...

    My C++ code won't loop like it should. What am I doing wrong? #include <iostream> using namespace std; int main() {    int n;    int flag= 1;    cout << "Prime/Not Prime" << endl;    cout << "Enter the Number to check Prime:" << endl;    cin >> n;    int m = n / 2;    int i;    for (i = 2; i <= m; i++)        if (n % i == 0)           ...

  • This program illustrates dynamic variables #include <iostream> using namespace std; int main () {    int...

    This program illustrates dynamic variables #include <iostream> using namespace std; int main () {    int *p1;    p1 = new int;             // Variables created using the new operator are called dynamic variables    cout << "Enter an integer \n";    cin >> *p1;    *p1 = *p1 + 7;    cout << << "Your input + 7 = " << *p1 << endl;    delete p1;                // Delete the dynamic variable p1 and return the memory occupied by p1 to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT