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

Comment the following code in detail. And include a check if the user enters a s

ID: 3831393 • Letter: C

Question

Comment the following code in detail. And include a check if the user enters a symbol or a character. And add a check for float or double.

#include

#include

using namespace std;

int main()

{

   int n;

   cout << "Please enter a number";

   cin >> n;

   if (n == 1)

   {

       cout << "No prime numbers were found";

   }

   else if (n<0)

   {

       cout << "Negative integers can't be prime, try using positive value or entering a number not a character";

   }


   else

   {

       int count = 1;

       int prime;

       for (int i = 2; i <= n; i++)

       {

           prime = 1;

           for (int j = 2; j <= sqrt(i); j++)

           {

               if (i%j == 0)

               {

                   prime = 0;

                   break;

               }

           }

           if (prime == 1)

           {

               cout << i << " ";

               for (int k = 0; k

               {

                   cout << "*";

               }

               count++;

               cout << endl;

           }

           else

           {

               cout << i << endl;

           }

      
      
       }

   }

   return 0;

}

Explanation / Answer

#include <iostream>
#include <cmath>
#include <limits>
using namespace std;

int main()

{

   int n;

   cout << "Please enter a number";

  
   cin >> n;
    while(cin.fail()) // after cin.ignore() , cin fails
    {
        cin.clear();
        //input numeric_limits < max allowed then ignore it
        cin.ignore(numeric_limits<streamsize>::max(),' '); //use header file <limits> to check if input is not number then ignore it
        std::cout << " Error: Enter a NUMBER: "; //display mesage to input number only
        std::cin >> n;
    }
   cout<<endl;

   if (n == 1)

   {

       cout << "No prime numbers were found"; //1 is not prime number

   }

   else if (n<0)

   {

       cout << "Negative integers can't be prime, try using positive value or entering a number not a character";

   }


   else

   {

       int count = 1;

       int prime;

       for (int i = 2; i <= n; i++)   //check numbers from 2 to inputted number for primes

       {

           prime = 1;

           for (int j = 2; j <= sqrt(i); j++)// chcek a number if it is prime by dividing it by 2,3,4,upto sqrt of the number

           {

               if (i%j == 0) //if number is divisible by any ,remainder = 0,number is not prime

               {

                   prime = 0;

                   break;

               }

           }

           if (prime == 1) //else if number is not divisible by any 2,3,4, upto sqrt of number ,it is prime

           {

               cout << i << " ";

               for (int k = 0; k <=5;k++)

               {

                   cout << "*"; //display *** after all prime numbers

               }

               count++;

               cout << endl;

           }

           else

           {

               cout << i << endl; //display without asterisk

           }

    
    
       }

   }

   return 0;

}

Output:

Please enter a number s

Error: Enter a NUMBER: x

Error: Enter a NUMBER: 5

2 *****

3 *****

4

5 *****