A prime number is an integer that is greater than 1 and that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6. Write a function name isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Demonstrate the function in a complete program that prompts for a number and indicates whether or not it is prime.

Respuesta :

Answer:

#include <iostream>

using namespace std;

bool isPrime (int num);

int main ()

{

  int num=0;

  cout << "Enter a number and I'll tell you whether it is prime: ";

  cin >> num;

  if (isPrime(num)==true)

      cout << num << " is prime.";

  else

      cout << num << " is NOT prime.";

  system("pause");

  return 0;

}

bool isPrime(int input)

{

  if(input<1)

      return false;

  else if (input == 1||input ==2 ||input==3)

  {

      return true;

  }

  else

  {

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

      {

          if(input%i==0)

              return false;

      }

      return true;

  }

}

Explanation:

Answer:

def Isprime(number):

          if number>1 :

                  for i in range(2, number):

                          if (number % i) == 0:

                                   print(number, "is not a prime number")

                                   break

                    else:

                              print (number, "is a prime number")

           else:

                   print(number, "is not a prime number")

x = Isprime(5)

y = Isprime(17)

z = Isprime(14)

Explanation:

A prime number is a number that can only be divided by 1 and itself. Prime number includes 5, 7, 11 etc.

def Isprime(number):  A function is created Isprime and the argument number is used.

if number>1 :  if the number is greater than 1 .

for i in range(2, number):  This expression loop through 2 to the number(argument)

if (number % i) == 0:  If the number divided by the looped value is equal to zero.

print(number, "is not a prime number")   print the number is not a prime number

break   This means stop and go back to the loop

else:   otherwise

print (number, "is a prime number") Print the number is a prime number.

else: This else is under the first if statement.

print(number, "is not a prime number")  print the number is not a prime number.

x = Isprime(5)

y = Isprime(17)

z = Isprime(14)

These call the function with the argument.