Question

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 program to store a list of all the prime numbers from 1 throuhg 300 in a file

recall that the% operator divides one number by another and returns the reminder of the division in an expression such as num1 % num2 the % operator will return 0 if num 1 is evenly divisible by num2

c plus plus

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

Solution:

Function isPrime()

bool isPrime(int num)
{
    for(int i = 2; i <= num / 2; i++) 
        {
       if(num % i == 0) 
           {
          return false;
       }
    }
    return true;
}

Complete program:

Note: Please specify the correct filename and path inside main.

#include<iostream>
#include<fstream>
using namespace std;

//Function to check if a number is prime
bool isPrime(int num)
{
    for(int i = 2; i <= num / 2; i++) 
        {
       if(num % i == 0) 
           {
          return false;
       }
    }
    return true;
}

int main()
{
        fstream newfile;
        //Provide the correct file name and path here
        newfile.open("primeList.txt");
        int x;
        //Loop is from 2 to 300 since 1 is not a prime number
        for(x=2;x<=300;x++) 
        {
                if(isPrime(x))
                {
                        //writing the number to the file
                        newfile << x <<endl;
                }
                
        }
        //Freeing unused memory of array q

        return 0;
        
}

After execution the following text will be stored to the file specified in the main function of the program

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
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199
211
223
227
229
233
239
241
251
257
263
269
271
277
281
283
293
Add a comment
Know the answer?
Add Answer to:
a prime number is a number that is only evenly divisible by itself and 1. for...
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
  • 17. Prime Numbers A prime number is a number that is only evenly divisible by itself...

    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...

  • 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...

  • 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...

  • 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...

  • In ASCII C programming write a function that determines if an integer is prime. A prime...

    In ASCII C programming write a function that determines if an integer is prime. A prime number is one that is not divisible by any number other than one and itself. int isPrime(long num); Return 1 if num is prime, 0 if not. As above, make this a pure function contained in its own file isPrime.c and test it with a separate tester program. To test if an integer is prime you could implement a loop that generates trial divisors...

  • 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...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • 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...

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