Question



17. Prime Numbers A prime number is a number 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 S. The number 6, how- ever, is not prime because it can be divided evenly by 1, 2, 3, and 6. Write a Boolean function named is_prime which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Use the function in a program that prompts the user to enter a number then displays a message indicating whether the number is prime TIP: Recall that the % operator divides one number by another and ret remainder of the division. In an expression such as num1 % num2, the % operator will return 0 if num1 is evenly divisible by num2.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

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

int main() {
    int n;
    cout << "Please input a positive integer: ";
    cin >> n;
    if(is_prime(n)) {
        cout << n << " is a prime number." << endl;
    } else {
        cout << n << " is not a prime number." << endl;
    }
    return 0;
}

Please input a positive integer: 13 is a prime number

Add a comment
Know the answer?
Add Answer to:
17. Prime Numbers A prime number is a number that is only evenly divisible by itself...
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
  • a prime number is a number that is only evenly divisible by itself and 1. for...

    a prime number is a number 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. use this functuion in a...

  • A prime number is a number that can be evenly divided by only itself and

    A prime number is a number that can be evenly divided by only itself and 1. For example, the number 5 is prime because it can be evenly divided by only 1 and 5. The number 6, however isn't prime because it can be evenly divided by 1,2,3, and 6. Write a bool function named isprime that takes an integer as an argument and returns true if the argument is a prime number or false otherwise. Use the function in...

  • A prime number is a method that is evenly divisible by only itslef and 1. Write...

    A prime number is a method that is evenly divisible by only itslef and 1. Write a method called isPrime, which takes only one integer as an argument and returns true if the number is prime or false if the number is not prime. Create a method to store a list of all the prime numbers from 1 to 100 in a file called Primes.txt. Use the package primes; at the beginning of your code and be sure to document...

  • Source: 2014 Nielsen 72 Prime Numbers A prime number is a number that is evenly divisible...

    Source: 2014 Nielsen 72 Prime Numbers A prime number is a number that is evenly divisible only by 1 and itself. The prime numbers less than 100 are listed below. 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 Choose one of these numbers at random. Find the probability that a. The number is odd b. The sum of the digits is odd c....

  • Application program           Program name      PrimeNumbers.cpp           Problem background:        &n

    Application program           Program name      PrimeNumbers.cpp           Problem background:                    In mathematics, a PRIME number is a number that is divisible by only                    itself and 1.           Examples:                    2, 3, 5, 7, 11 are all primes                    these are not primes the number 4        divisible by 2 the number 6        divisible by 2 and 3 the number 12       divisible by 2, 3, 4, and 6 Write a C program to find all the prime numbers between 0 and...

  • A positive integer is a prime number if its only positive integer divisors are itself and...

    A positive integer is a prime number if its only positive integer divisors are itself and 1. Write a program to determine whether or not a given integer is prime. The program should contain two functions: main: to ask the user for a positive integer and to print the result isPrime: to determine whether the user's input is prime by testing all possible divisors. This function should return two values: variable_1: a Boolean value indicating whether the number is prime...

  • Is Prime Number In this program, you will be using C++ programming constructs, such as functions....

    Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...

  • JAVA please! Prime numbers are interesting numbers. A prime number is one that is only divisible...

    JAVA please! Prime numbers are interesting numbers. A prime number is one that is only divisible by 1 and itself. For hundreds of years mathematicians have looked for the largest prime number. In the year 1456 the largest known prime was 8191. By the year 1588 it was a 6-digit number: 131,071. The famous mathematician Leonhard Euler discovered the 10-digit number 2,147,483,647 in 1772. Over the years larger and larger primes have been found. There are contests to find the...

  • this is c++ and please let the code works on vs2017. the output should be same...

    this is c++ and please let the code works on vs2017. the output should be same as the question. mes A. Hotz Programming Challenge: Prime qumber Array Values from Canvas: ICE12A Please name your source file: PC7101.CPP A prime number is a number 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...

  • The following method checks if a number is prime by checking for divisibility on numbers less...

    The following method checks if a number is prime by checking for divisibility on numbers less than it only needs to go up to the square root of n because if n is divisible by a number greater than its square root then it’s divisible by something smaller than it. boolean isPrime(int n){ for (int x = 2; x* x<=n; x++){ if (n % x == 0){ return false; } } } What’s the time complexity of the method above...

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
ADVERTISEMENT