Answer to Question #265785 in C++ for Nae

Question #265785

A prime number is called an Additive prime if the sum of its digits is also a prime


number. Write a program that will display the N (user input) number of additive prime numbers


and displays the output in the following format:


Prime number


Sum of its digits


2


2


3


3


...


11


2


using user-defined functions



Sample output:


Enter an integer: 10


Prime number Sum of Digits


2 2


3 3


5 5


7 7


11 2


23 5


29 11


41 5


43 7


47 11

1
Expert's answer
2021-11-15T07:38:39-0500
#include <iostream>
#include <iomanip>
#include <math.h>


using namespace std;
bool isPrime(int num) {
	if (num < 2){
		return false;
	}
	if (num == 2 || num == 3){
		return true;
	}
	for (int i = 2; i <= sqrt(num*1.0); i++){
		if (num % i == 0){
			return false;
		}
	}
	return true;
}


int sumDigits(int number){
	int sum = 0;
	while (number != 0) {
		sum = sum + number % 10;
		number = number / 10;
	}
	return sum;
}


int main(){
	int number;
	int counter=0;
	int i=0;
	cout<<"Enter an integer: ";
	cin>>number;
	cout<<"Prime number\tSum of Digits\n";
	while(counter<number){
		int sumD=sumDigits(i);
		if(isPrime(i) && isPrime(sumD)){
			cout<<i<<"\t\t"<<sumD<<"\n";	
			counter++;
		}
		i++;
	}




	cin>>number;
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS