Question

In: Computer Science

Problem description Write a C++ program that prompts the user to enter two non-negative integers, firstNum...

Problem description

Write a C++ program that prompts the user to enter two non-negative integers, firstNum and secondNum. The program then prints all palindromic primes (Links to an external site.) between firstNum and secondNum, inclusive.

A palindromic number is a number whose digits are the same forward or backward (e.g., 12321). A palindromic prime is a prime number that is also a palindromic number (e.g., 101).

You must implement and use the following functions as prototyped below:

/// ---------------------------------------------------------------
/// Returns 'number' with its digits reversed.
///
/// @param number The number to reverse.
///
/// @returns The 'number' with its digits reversed.
/// ---------------------------------------------------------------

int reverse(int number);

/// ---------------------------------------------------------------
/// Returns true if 'number' is a numeric palindrome, where 
/// the digits are the same forward and backward.
///
/// @param number The number to test.
///
/// @returns true if 'number' is a palindrome, else false.
/// ---------------------------------------------------------------

bool is_palindrome(int number);

/// ---------------------------------------------------------------
/// Tests 'number' to see if it is prime.
///
/// @param number The number to test.
///
/// @returns true if 'number' is prime, else false.
/// ---------------------------------------------------------------

bool is_prime(int number);

Implementation notes

The reverse function reverses the digits of a number.

Use the reverse function to implement is_palindrome. A number is a palindrome if its reversal is the same as itself.

An integer greater than 1 is prime if its only positive divisor is 1 or itself (i.e., an even number is prime if it is 2; an odd integer is prime if it is not divisible by any odd integer less than or equal to the square root of the number).

Input specification

The input consists of two positive integers in the range [1..1'000'000), separated by white space.

Output specification

Your program will generate one line of output consisting of all palindromic primes within the range [firstNum..secondNum], inclusive. A single space separates each number, and the line is terminated by a newline character.

Sample interaction

$ ./a.out
Enter two positive integers: 1 500
2 3 5 7 11 101 131 151 181 191 313 353 373 383
$ ./a.out
Enter two positive integers: 797 313
313 353 373 383 727 757 787 797
$

Solutions

Expert Solution

C++ program:

#include<iostream>
using namespace std;

/// ---------------------------------------------------------------
/// Returns 'number' with its digits reversed.
///
/// @param number The number to reverse.
///
/// @returns The 'number' with its digits reversed.
/// ---------------------------------------------------------------

int reverse(int number){
int reversed_number=0;

while(number>0){
reversed_number = reversed_number*10 + number%10;
number /= 10;
}
return reversed_number;
}

/// ---------------------------------------------------------------
/// Returns true if 'number' is a numeric palindrome, where
/// the digits are the same forward and backward.
///
/// @param number The number to test.
///
/// @returns true if 'number' is a palindrome, else false.
/// ---------------------------------------------------------------

bool is_palindrome(int number){
if (number == reverse(number))
return true;
else
return false;

}
/// ---------------------------------------------------------------
/// Tests 'number' to see if it is prime.
///
/// @param number The number to test.
///
/// @returns true if 'number' is prime, else false.
/// ---------------------------------------------------------------

bool is_prime(int number){
if(number == 1)
return false;

if(number == 2)
return true;

if(number%2==0)
return false;

//Check if number has any divisor other than 1 and number itself
for(int i=2;i*i<=number;i++){
if(number%i == 0){
return false;
}
}
return true;
}

int main(){
int num1,num2;
cout<<"Enter two positive integers: ";
cin>>num1>>num2;

for(int i=num1; i<=num2; i++){
if(is_prime(i) && is_palindrome(i)){
cout<<i<<" ";
}
}
return 0;
}

Output:


Related Solutions

C++ Write a program that prompts the user to enter 50 integers and stores them in...
C++ Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are sum of two other array elements. If an array element is the sum of two other array elements, then for this array element, the program should output all such pairs separated by a ';'. An example of the program is shown below: list[0] = 15 is the sum of: ----------------------...
C Program 1. Write a program that prompts the user to enter 3 integers between 1...
C Program 1. Write a program that prompts the user to enter 3 integers between 1 and 100 from the keyboard in function main and then calls a function to find the average of the three numbers. The function should return the average as a floating point number. Print the average from main.The function header line will look something like this:float average(int n1, int n2, int n3) STOP! Get this part working before going to part 2. 2. Create a...
Write a program that prompts user to enter integers one at a time and then calculates...
Write a program that prompts user to enter integers one at a time and then calculates and displays the average of numbers entered. Use a while loop and tell user that they can enter a non-zero number to continue or zero to terminate the loop. (Switch statement) Write a program that prompts user to enter two numbers x and y, and then prompts a short menu with following 4 arithmetic operations: Chose 1 for addition Chose 2 for subtraction Chose...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
Problem 1 Write a program that prompts the user to enter an integer It then tells...
Problem 1 Write a program that prompts the user to enter an integer It then tells the user if the integers is a multiple of 2, 3, 5, 7 or none of the above. Program language is C Ex. Enter an integer 12 You entered 12 The number you entered is a multiple of 2 ----------------------------------------------- Enter an integer 11 You entered 11 The number you entered is not a multiple of 2, 3, 5, or 7
Question Write a C program that asks the user to enter two integers x and n....
Question Write a C program that asks the user to enter two integers x and n. Then the program computes xn (=x * x * x …… (n times)) using for loop. and give me an output please use printf and scanf #include int main(void) {     //Declare required variables             //read two integers x , n from the keyboard                 //compute xn using for loop                     printf("< Your name >\n");...
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value....
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value. The program should then output a message saying whether the number is positive, negative, or zero.
write a c++ program that prompts a user to enter 10 numbers. this program should read...
write 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 two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
( USE C++ ) The program prompts the user to enter a word. The program then...
( USE C++ ) The program prompts the user to enter a word. The program then prints out the word with letters in backward order. For example, if the user enter "hello" then the program would print "olleh" show that it works .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT