Question

write a function that will determine if an integer is prime. you should return 1 if...

write a function that will determine if an integer is prime. you should return 1 if the value is prime and 0 otherwise. you may not use the math library. create both function and prototype.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

A number that is divisible only by 1 and itself is a prime number.
For example: 2, 3, 5, 7, 11, 13 etc.
Note: 1 is neither prime nor a composite number.

Below are the steps to check whether a number is prime or not a prime.
Step-1: Read the number and store it in the variable num.
Step-2:  Call the function primeNum, by passing the value. Check if the entered number is equal to 1(num==1), if so, print the statement it is neither prime nor composite. If the entered number is other than 1 use the for loop to check whether the number is divisible by other number beginning, from i=2 to num/2 (remember a number is prime if it is divisible by 1 and itself, so i=2). Now use the if statement to check whether the remainder is zero after dividing the number by i, if the remainder is 0, return 0 to main function and print the number is not prime, else return 1 and print the entered number is prime.

NoteL Please refer to the screenshot of the program for code indentation and explanation.

C program to check whether the entered number is prime or not.

#include<stdio.h>

int primeNum(int);
int main()

{  
int num, res;
printf("Enter a number to check whether it is prime or not: ");
scanf("%d",&num);   
res = primeNum(num);

if ( res == 1 )
printf(" %d is a prime number.", num);

else if(res==0)
printf("\n %d is not a prime number", num);
return 0;

}

/*Function to check whether a number is prime or not. If prime returns 1, else returns 0 */
int primeNum(int num)

{  
int i;    
if(num==1)
{
printf(" 1 is neither prime nor a composite number ");  
}
else  
{

for ( i = 2 ; i <= num/2 ; i++ )
{
  if ( num%i == 0 )

return 0;  
}
return 1;
}

}

The screenshot of the complete program along with the explanation and output is shown below:

8 1 #include<stdio.h> 2 3 //function declaration 4 int primeNum(int); 5 6 int main() 7-{ int num, res; // variable declaratio{ 22 //function definition 23 int primeNum(int num) 24 - { 25 int i; // variable declaration 26 27 if(num==1) // if entered nOutput-1:

Enter a number to check whether it is prime or not: 3 prime number. 3 is a

Output-2:

Enter a number to check whether it is prime or not: 12 12 is not a prime number

Output-3:

Enter a number to check whether it is prime or not: 1 1 is neither prime nor a composite number


Add a comment
Know the answer?
Add Answer to:
write a function that will determine if an integer is prime. you should return 1 if...
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
  • 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...

  • In this assignment you are asked to write a Python program to determine all the prime...

    In this assignment you are asked to write a Python program to determine all the prime numbers in between a range and store them in a list that will be printed when the range is searched. The user is prompted for two positive integer values as input, one is the start of the range and the other is the end of the range. If the user gives a negative value or enters a second number that is lower than the...

  • Your program  write a program to test whether a number is prime or not. Your program should...

    Your program  write a program to test whether a number is prime or not. Your program should ask user to input an integer and respond: "Prime" or "Not prime". Don't use any library function that tests for prime numbers. The program should continue to ask for input, till it sees a 0, when it exits. Please submit printed pseudocodeshould ask user to input an integer and respond: "Prime" or "Not prime". Don't use any library function that tests for prime numbers....

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

  • Q2. To check and print if entered integer is prime or not. Prompt the user to...

    Q2. To check and print if entered integer is prime or not. Prompt the user to enter a positive integer or 0 to exit. while negative integer entered - prompt for valid values again. If positive integer entered - check if it is prime. Write a separate function that accepts a positive integer as argument and checks and returns 1 if the integer is prime or 0 if it is not prime. Call the function from main. Accept the return...

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

  • Prime Numbers Write a program that will determine whether a set of randomly-generated numbers are prime...

    Prime Numbers Write a program that will determine whether a set of randomly-generated numbers are prime numbers. To write your program, you will need to use the following programming features: The random library (module) A function A variable that will prompt the user to say how many random numbers the program should generate A for loop with a range function A randint function taken from the random library (module); the randint function takes two arguments – the beginning and the...

  • Write the function prototype and the definition of a function call "oddoreven" that determine if an...

    Write the function prototype and the definition of a function call "oddoreven" that determine if an integer value is ODD or EVEN. The function will return a string containing that message. Use only ONE function! As an example if an integer value of 20 was passed to that function then the function will return "20 is an Even Number." if an integer value of 35 was passed to that function then the function will return "35 is an Odd Number."

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

  • In R! Write a function named prime_factor(x) that will find the prime factorization of an integer...

    In R! Write a function named prime_factor(x) that will find the prime factorization of an integer x. The function will output a numeric vector. All of the values in the output vector will be prime. The product of the numeric vector will be the original value x. There should be a check to make sure that the input value is a number greater than 2 with no decimal values with appropriate error messages. If you’re stuck on getting started with...

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