Question

In C++ write a program that prompts the user to enter two positive integers less than...

In C++ write a program that prompts the user to enter two positive integers less than 1,000,000,000 and the program outputs the sum of all the prime numbers between the two integers. Two prime numbers are called twin primes, if the difference between the two primes is 2 or -2. Have a program output all the twin primes and the number of twin primes between the two integers.

NEEDED OUTPUTS FOR PROGRAM:

Enter two positive integers <1,000,000,000: 1 100

-1 and 1 are twin primes.

1 and 3 are twin primes.

3 and 5 are twin primes.

5 and 7 are twin primes.

11 and 13 are twin primes.

17 and 19 are twin primes.

29 and 31 are twin primes.

41 and 43 are twin primes.

59 and 61 are twin primes.

71 and 73 are twin primes.

The sum of prime numbers between 1 and 100 = 1059

The number of twin primes between 1 and 100 = 10

Homework Answers

Answer #1

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

#include <iostream>
#include <string.h>
using namespace std;

void twinPrimes(long int number1, long int number2)
{
bool prime[number2 + 1];
memset(prime, true, sizeof(prime));
  
for (int p = 2; p * p <= number2; p++)
{
if (prime[p] == true)
{
for (int i = p * 2; i <= number2; i += p)
prime[i] = false;
}
}

long int sum=0;
for (int p = number1; p <= number2; p++)
{
if(prime[p] && p!=2)
sum+=p;
}
cout<<"1 and -1 are twin primes."<<endl;
int count =1;
for (int p = number1; p <= number2; p++)
if (prime[p] && prime[p + 2])
{
cout << p << " and " << p + 2 << " are twin primes."<<endl;
count++;
}
  
cout<<"The sum of prime numbers between "<<number1<<" and "<<number2<<" = "<<sum<<endl;
cout<<"The number of twin primes between "<< number1<<" and"<<number2<<" = "<<count<<endl;
}
  
int main()
{
long int number1, number2 ;
cout<<"Enter Two Positive Integers <1,000,000,000: ";
cin>>number1>>number2 ;
twinPrimes(number1,number2);
   return 0;
}

====================

SCREENSHOT:

Know the answer?
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
Similar Questions
Using nested loops, write a function called primes(a,b) that takes in two positive integers a and...
Using nested loops, write a function called primes(a,b) that takes in two positive integers a and b (where a<b). Then simply RETURNS a string containing all the prime numbers between a and b (or if there are none, the string "No Primes"). You should check that a and b are valid inputs, that is, that a and b are integers such that a<b (otherwise, the function should print “No Primes”). Three sample calls of your function (in IDLE) should produce...
Write a Python program that prompts a user for two integers and stores the two integers...
Write a Python program that prompts a user for two integers and stores the two integers the user types into the shell. Print the product, float division, integer division, remainder, sum, and difference of the two integers to the console. The entire equation must be printed (see below for formatting). Only display 2 digits after the decimal place for results that are floats. Include a module docstring that summarizes the program.. Sample Run 1: Enter an integer: 5 Enter another...
design a program that prompts the user to enter a positive integer number and reads the...
design a program that prompts the user to enter a positive integer number and reads the user’s input. If the user input is not a positive number, the program should prompt them repeatedly until they enter a valid input. Once a valid input is received ,the program uses a loop to calculate the sum of the digits of the user’s input and displays the sum. For example, if the user enters the number 94311, the program should print the sum...
User enters two numbers, N1, and N2. Write a program that determine if the numbers are...
User enters two numbers, N1, and N2. Write a program that determine if the numbers are twin primes. Definition: Twin primes are two prime numbers separated by 2. For example, 3 and 5 are both prime numbers and separated by 2. Another example is 17, 19.
User enters two numbers, N1, and N2. Write a program that determine if the numbers are...
User enters two numbers, N1, and N2. Write a program that determine if the numbers are twin primes. Definition: Twin primes are two prime numbers separated by 2. For example, 3 and 5 are both prime numbers and separated by 2. Another example is 17, 19. matlab
Write a C++ fragment that prompts the user to enter integers one at a time, keeping...
Write a C++ fragment that prompts the user to enter integers one at a time, keeping track of the total number of odd numbers entered. If a negative number is entered, stop asking for numbers and print the result. Assume all needed headers are included.
Write a C program, called logic_abc.c, that prompts the user to enter three integers a, b,...
Write a C program, called logic_abc.c, that prompts the user to enter three integers a, b, c, and then computes the results of the following logical operations, in sequence: !a || !b++ && c (a-1 || b/2) && (c*=2) (a-- || --b) && (c+=2) a || !(b && --c)
Write a program that prompts the user to enter an integer number between 1 and 999....
Write a program that prompts the user to enter an integer number between 1 and 999. The program displays the sum of all digits in the integer if the input is valid; otherwise, it displays a message indicating that the integer is not between 1 and 999 and hence, is invalid. Name the program file Q1.cpp Example: if the user enters 12, sum of digits is 3. If the user enters 122, sum of digits is 5.
Create a C++ Program that prompts the user to enter a number and compute for the...
Create a C++ Program that prompts the user to enter a number and compute for the factorial value. SAMPLE OUTPUT: ENTER A NUMBER: -5 zero & positive numbers only!!! ENTER A NUMBER: 5 Factorial of 5 : 5*4*3*2*1 = 120 DO NOT USE FUNCTION FOR THIS PROBLEM. THANK YOU   
PLease code a C++ program that prompts a user to enter 10 numbers. this program should...
PLease code a C++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the 10 numbers and the average of the 10 numbers please use file i/o and input measures for Handling Errors in C++ When Opening a File