Question

please solve this question easiest way I saw the other answers but those are not solve my programTwin primes are two consequetive odd numbers that are prime. Write a function that returns 1 if the input is a prime and O ot

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 #include <iostream> using namespace std; int is_prime(int n){ int flag=0; if(n<2){ //number less than 2 is not a prime number return 0; } if(n==2){ //2 is the smallest prime number return 1; } for(int i=2;i<(n/2);i++){ if(n%i==0){ flag=1; } } if(flag==0){ return 1; } else{ return 0; } } void is_twinprime(int min,int max){ cout<<"\ntwin primes are: "; for(int i=min;i<=max;i++){ int r1=is_prime(i); if(r1==1 && i%2==1){ //if no. is prime also check no. is odd or not int r2=is_prime(i+2); //check next odd no. is prime or not if(r2==1 && (i+2)%2==1){ //if this no. is also prime then shows that they are twin primes cout<<"\n"<<i<<" "<<i+2; } } } } int main() { int n,r,min,max; cout<<"Enter the Number to check prime or not: "; cin>>n; r=is_prime(n); //this fuction used to check no. is prime or not cout<<"returned value is: "<<r; cout<<"\nEnter the start of range: "; cin>>min; cout<<"\nEnter the end of range: "; cin>>max; is_twinprime(min,max); //this fuction use to find twin primes between the range return 0; }

Input 2 2 19 Output Enter the start of range: Enter the end of range: twin primes are: 3 5 5 7 11 13 17 19

Above solution is 100% working. please provide feedback.

Add a comment
Know the answer?
Add Answer to:
please solve this question easiest way I saw the other answers but those are not solve...
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
  • write easiest program to under stand:- a) Python program that takes three coefficients (a, b, and...

    write easiest program to under stand:- a) Python program that takes three coefficients (a, b, and c) of a Quadratic equation (ax2+bx+c=0) as input and compute all possible roots b).User defined function isprime(num) that accepts an integer argument and returns 1 if the argument is prime, a 0 otherwise. Write a Python program that invokes this function to generate prime numbers between the given ranges

  • *Please write in code in C* First, write a function called prime_check(int x) that takes an...

    *Please write in code in C* First, write a function called prime_check(int x) that takes an integer number as an input then return 1 if it is a prime number nd returns 0 if it is a composite number Then, Write a program that prompt the user to input two numbers: Print the multiplication of these two numbers if both of them are prime. Or Print " Sorry at least one of your number is composite" if otherwise

  • Write a C++ program that simulates playing the Powerball game. The program will generate random numbers...

    Write a C++ program that simulates playing the Powerball game. The program will generate random numbers to simulate the lottery terminal generated numbers for white balls and red Powerball. The user will have the option to self-pick the numbers for the balls or let the computer randomly generate them. Set the Grand Prize to $1,000,000,000.00 in the program. Project Specifications Input for this project: Game mode choice – self pick or auto pick Five numbers between 1 and 69 for...

  • 3. Primes primes.py We're going to solve a popular interview question! Write a function that checks...

    3. Primes primes.py We're going to solve a popular interview question! Write a function that checks whether a number is prime. Handle the case where the argument is not a positive integer. Next, write a function that returns the first N primes. Can you optimize your code? Note that any number that is not prime is always divisible by at least one prime that comes before it. Ensure your program passes the tests provided.

  • use simple C++ please Exercise 3: Write a program that reads a positive number and checks...

    use simple C++ please Exercise 3: Write a program that reads a positive number and checks if the number can be expressed as a sum of two prime numbers. If yes, write all possible ways of expressing the number as sum of primes. Note: n is a prime if it is divisible only by 1 and itself, hence not divisible by any integer in the range from 2 to sqrt(n), both inclusive. Write a function is Prime that take a...

  • (Python) Please do NOT solve it by using special functions(like max min, divmod), I have to...

    (Python) Please do NOT solve it by using special functions(like max min, divmod), I have to ask the same questions again and again otherwise, thanks for your understanding. 1. Write a program that prompts the user for an integer, calculates the factorial of the input integer and displays the result as a string with a comma at every third position, starting from the right. Assume that user input is valid. Hint: Think about representing a number as a string. Sample...

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

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

  • can someone please help me solve these problem in c++ language and leave useful comments below...

    can someone please help me solve these problem in c++ language and leave useful comments below so i can follow along and know what i am doing /view wa New Tab CSSO IDE 15 THE DIGIT SWAP PROBLEM Write a function named swapDigitPairs() that accepts a positive integer n as an input-output parameter which is changed to a new value similar to n's but with each pair of digits swapped in order. For example: int n = 482596; int old...

  • PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a...

    PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a function yesOrNo which has no parameters. When called, it gets input from the user until the user types either 'yes' or 'no', at which point the function should return True if the user typed 'yes' and False if the user typed 'no'. Any other entries by the user are ignored and another value must be input. For example: Test Input Result print(yesOrNo()) hello blank...

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