# Answer to Question #24167 in C++ for Ayman

Question #24167

Write a C++ function that (prints) the sum of the prime digits in an integer (unknown its number of digits)

e.g :

>> please enter an integer : 345693

3+5+3 = 11

>> please enter an integer : 76341237

7+3+1+2+3+7=23

e.g :

>> please enter an integer : 345693

3+5+3 = 11

>> please enter an integer : 76341237

7+3+1+2+3+7=23

Expert's answer

#include <iostream>

#include <conio.h>

#include <string>

using namespace std;

bool IsPrime(int number)

{

if ((number & 1) == 0)

{

if (number == 2)

{

return true;

}

else

{

return false;

}

}

for (int i = 3; (i * i) <= number; i += 2)

{

if ((number % i) == 0)

{

return false;

}

}

return number != 1;

}

int main()

{

string number="";

cout<<"Please enter an integer : ";

getline(cin,number);

int sum=0;

for(int i=0;i<number.length();i++){

int k=int(number[i])- '0';

if(IsPrime(k)==true){

sum+=k;

cout<<number[i]<<"+";

}

}

cout<<" = "<<sum;

getch();

return 0;

}

#include <conio.h>

#include <string>

using namespace std;

bool IsPrime(int number)

{

if ((number & 1) == 0)

{

if (number == 2)

{

return true;

}

else

{

return false;

}

}

for (int i = 3; (i * i) <= number; i += 2)

{

if ((number % i) == 0)

{

return false;

}

}

return number != 1;

}

int main()

{

string number="";

cout<<"Please enter an integer : ";

getline(cin,number);

int sum=0;

for(int i=0;i<number.length();i++){

int k=int(number[i])- '0';

if(IsPrime(k)==true){

sum+=k;

cout<<number[i]<<"+";

}

}

cout<<" = "<<sum;

getch();

return 0;

}

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment