In: Computer Science
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 $
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: