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

All parts are related with each other. Please answer all of them in C or C++ lan

ID: 3819486 • Letter: A

Question

All parts are related with each other. Please answer all of them in C or C++ language

Write a function named prime() that determine whether or not a given number n (greater than one) is prime. The algorithm: Write a program that: If n is even then n is not a prime number except n = 2. For all odd numbers less or equal to the squareroot of n, if any of them divide n, then n is not prime Asks the user to enter an integer Saves the entry as var Calls the function prime() on the entry Displays whether or not the entry is prime 5. Write a function named eratos() that find all the prime number less than a given number, its argument n Write a program that: Asks the user to enter an integer Saves the entry as bound Passes bound onto eratos() Displays all the prime numbers less or equal to bound

Explanation / Answer

Q.3 Function to analyze whether the given number is prime or not:

Here we are returning zero or one depending on the condition. The condition for checking is in if.

Condition is:A%c==0 i.e. we are checking for remainder. If remainder is zero that shows the given number is non prime.

=========================================================

Q.4Program to check number is prime or not:

#include<stdio.h>

int prime(int);

main()
{
   int n, result;

   /* Asking for user input*/
   printf("Enter an integer to check whether it is prime or not. ");
   scanf("%d",&n);

   result = prime(n);

   /* To Print the result whether result is prime or not*/
   if ( result == 1 )
      printf("%d is prime. ", n);
   else
      printf("%d is not prime. ", n);

   return 0;
}

/* Actual logic for analyzing whether the given number is prime or not*/
int prime(int a)
{
   int c;

   for ( c = 2 ; c <= a - 1 ; c++ )
   {
      if ( a%c == 0 )
     return 0;
   }
   return 1;
}

===========================================================

Q.5 Function to print all prime numbers less than the give number:

The logic behind this function is we are making boolean flag false for those entries which are multiple of 2 the rest entries with value as true are prime.

void SieveOfEratosthenes(int n)
{
    // Create a boolean array "prime[0..n]" and initialize
    // all entries it as true. A value in prime[i] will
    // finally be false if i is Not a prime, else true.
    bool prime[n+1];
    memset(prime, true, sizeof(prime));

    for (int p=2; p*p<=n; p++)
    {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true)
        {
            // Update all multiples of p
            for (int i=p*2; i<=n; i += p)
                prime[i] = false;
        }
    }

    // Print all prime numbers
    for (int p=2; p<=n; p++)
       if (prime[p])
          cout << p << " ";
}

===========================================================

Q.6 Program to print all prime numbers which are less than given number n.

// C++ program to print all primes smaller than or equal to

// n using Sieve of Eratosthenes

#include <bits/stdc++.h>

using namespace std;

void SieveOfEratosthenes(int n)

{

    // Create a boolean array "prime[0..n]" and initialize

    // all entries it as true. A value in prime[i] will

    // finally be false if i is Not a prime, else true.

    bool prime[n+1];

    memset(prime, true, sizeof(prime));

    for (int p=2; p*p<=n; p++)

    {

        // If prime[p] is not changed, then it is a prime

        if (prime[p] == true)

        {

            // Update all multiples of p

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

                prime[i] = false;

        }

    }

    // Print all prime numbers

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

       if (prime[p])

          cout << p << " ";

}

// Driver Program to test above function

int main()

{

    int n = 30;

    cout << "Following are the prime numbers smaller "

         << " than or equal to " << n << endl;

    SieveOfEratosthenes(n);

    return 0;

}