Question

Exercise 3: Write a program that reads a positive number and checks if the number can be expressed as a sum of two prime numb

use simple C++ please
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Please refer to the screenshot of the code to understand indentation

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

// isPrime(x) checks if x is prime or not. It returns false if it is not prime, and returns true otherwise.
bool isPrime(int x)
{
   int lowerLimit = 2;
   int upperLimit = sqrt(x);
   for(int i = lowerLimit; i<=upperLimit; i++)
   {
       if(x%i==0)
           return false;
   }
   return true;
}

int main()
{
   // Take an integer as user input
   cout<<"Enter a positive integer: ";
   int n;
   cin>>n;
  
   // We check if the inputted integer can be expressed as sum of two prime numbers, only upto its half (n/2), to avoid repetitions.
   int lowerLimit = 1;
   int upperLimit = n/2;
   for(int i = lowerLimit; i<=upperLimit; i++)
   {
       if(isPrime(i)==true)
       {
           int difference = n-i;
           if(isPrime(difference)==true)
           {
               cout<<n<<" = "<<i<<" + "<<difference<<endl;
           }
       }
   }
}

PrimeSum.cpp #include <iostream> #include <cmat h> 2 using namespace std 4 //isPrime(x) checks if x is prime or not. It retur

Enter a positive integer: 96 96 = 7 + 89 96 96 96 96 13 + 83 = 17 + 79 = 23 +73 = 29+67 = 96 37 + 59 96 = 43 + 53Enter a positive integer: 288 288 :5 + 283 = 288 7 + 281 = 288 = 11 + 277 288 = 17 + 271 288 31 257 288 288 59 229 288 288 28

Add a comment
Know the answer?
Add Answer to:
use simple C++ please Exercise 3: Write a program that reads a positive number and checks...
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
  • python language Primes Write a program primes.py that reads a positive integer from standard input, and...

    python language Primes Write a program primes.py that reads a positive integer from standard input, and determines whether or not the number is prime A prime number is a positive integer that is greater than 1, an e can be divided exactly (without leaving a rerm or itself 1 $ python3 primes. py 2 one positive integer please: 21 3 21 is not prime! 5 S python3 primes. py 6 one positive integer please 7 23 is prime

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

  • write a Matlab program ( solve the three questions). please use array and create your own...

    write a Matlab program ( solve the three questions). please use array and create your own function to check primality Question 1 : [10 points) Write a MATLAB program that will store all the first 1000 prime numbers in an array variable named arr_of_primes. Please note that a prime number is a positive integer that is bigger than or equal to 2 which is divisible only by 1 and itself. Examples of the first 8 primes are: 2, 3, 5,...

  • C++ please Question 4: 125 points] Write and test a program that reads two positive integers...

    C++ please Question 4: 125 points] Write and test a program that reads two positive integers nl and n2 with n2 > nl. The program then calculates the sum of the prime numbers, using is prime () function developed above, between nl and n2 (inclusive) A Sample input Enter values for nl and n2 (nl<n2): 3 9 Output: Thé Sum of prime numbers between 3 and 9 (inclusive) is 15

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

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

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

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

  • c++ 2. Write a function to test primality Use the definition of prime number to guide...

    c++ 2. Write a function to test primality Use the definition of prime number to guide your implementation of the following function. After testing it for correctness, think about how the following result can be used to improve the efficiency. For any positive integers n, r, s, if n=rs, then r<=sqrt(n) or s<=sqrt(n) /* return true if n is prime return false if n is not prime Precondition: n>1 bool IsPrime (int n)

  • 4. Write a logical function perfect Square that receives a positive integer number and checks if ...

    write the code in C please 4. Write a logical function perfect Square that receives a positive integer number and checks if it is a perfect square or not. Note: perfect square numbers are 4, 9,16,25,36 etc.... Write a main function that makes use of the perfect Square function to find and print all perfect squares between nl and n2. nl and n2 are end values of a range introduced by the user. ■ (inactive CAT EXE) Enter end values...

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