Question

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
from 2 to num-1. If any trial divides num, then num is not prime. But you only have to
test up to (and including) the integer square root of num. Say that num=a*b. Then if
a is larger than the square root, b must be smaller than the square root and has
already been tested.
Use your own function isqrt() for computing the integer square root. You will need
a prototype for this in isPrime.c
C:\Source\>gcc primeTester.c isqrt.c isPrime.c
C:\Source\>.\a.exe
Enter num: 223
223 is a prime

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

isPrime.c

#include <stdio.h>

#include <math.h>

int isPrime(long num) {

if (num <= 0)

return 0;

if (num == 2)

return 1;

if (num % 2 == 0)

return 0;

for (int i=3; i<sqrt(num); i+=2) {

if (num % i == 0) {

return 0;

}

}

return 1;

}

driver.c

#include <stdio.h>

#include <math.h>

#include "isPrime.c"

// driver code

int main() {

long num;

printf("Enter a number: ");

scanf("%ld", &num);

if (isPrime(num) == 1) {

printf("%ld is a prime.", num);

} else {

printf("%ld is not a prime.", num);

}

}

clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) Enter a number: 223 223 is a prime.​​​​​​​

Add a comment
Know the answer?
Add Answer to:
In ASCII C programming write a function that determines if an integer is prime. A prime...
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
  • 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...

  • Write a Python function isPrime(number) that determines if the integer argument number is prime or not....

    Write a Python function isPrime(number) that determines if the integer argument number is prime or not. The function will return a boolean True or False. Next, write a function HowManyPrimes(P), that takes an integer P as argument and returns the number of prime numbers whose value is less than P. And then write a function HighestPrime(K) that takes integer K as an argument and returns the highest prime that is less than or equal to K. USE THE WHILE LOOP...

  • Write a C++ function that inputs a positive integer and determines whether it is prime using...

    Write a C++ function that inputs a positive integer and determines whether it is prime using trial division and find as many primes of the form n2+1 , where n is a positive integer, as you can. c++/discrete structure

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

  • Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument,...

    Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument, and provides an output argument called Primes that is a vector containing all prime numbers between 2 and n, inclusive. Verify the correctness of your code with inserting 13 as the input argument, and check that your output is Primes = [2,3,5,7,11,13]. You are NOT allowed to use any directly relevant MATLAB built-in function such as divisors, etc. NOTE: A prime number is a...

  • IN PYHTON CODE Question #3 Produce a function prime_factors.dict that has one integer variable n that...

    IN PYHTON CODE Question #3 Produce a function prime_factors.dict that has one integer variable n that is assumed to be greater than 1. The function will return a dictionary with keys the integers from 2 to n, inclusive, and with values the set of prime factors of each key. So, the command prime_factors.dict (8) will return the dictionary 2 123,3: 3),4 2),5 (53,6 2,3),7:7),8 {2)) Note that even though the prime number 2 appears twice in the prime fac- torization...

  • Code to be written in Java public static boolean isPrime(int n) Checks whether the parameter integer...

    Code to be written in Java public static boolean isPrime(int n) Checks whether the parameter integer n is a prime number. For this lab, it is sufficient to use the primality test algorithm that simply loops through all potential integer factors up to the square root of n and stops as soon as it finds one factor. (If an integer has any nontrivial factors, at least one of these factors has to be less than or equal to its square...

  • Use C programming Make sure everything works well only upload Write a program that takes an...

    Use C programming Make sure everything works well only upload Write a program that takes an integer from standard input and prints all prime numbers that are smaller than it to standard output. Recall that a prime number is a natural number greater than 1 whose only positive divisors are 1 and itself. At the start of the program, prompt the user to input a number by printing "Input a number greater than 2:\n". If the user's input is less...

  • I need help with this exercise. Thank you for your help Language is C++ 6. Write...

    I need help with this exercise. Thank you for your help Language is C++ 6. Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd inte- ger is prime if it is not divisible by any odd integer less than or equal to the square root of the number.)

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

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