Sum of primes

PROBLEM :

Your task is to calculate sum  of primes in a digit N.

Input:

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The next T lines contains an integer N.

Output:
Print sum of primes in the digit

Constraints:
1 = T = 50
1 = N = 50000


Example:

Input:

2
333
686

Output:
9
0
     
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
bool isPrime(int ) ;
int main()
 {
int t,no,sum,rem ;
cin>>t ;
while(t--)
{
   cin>>no ;
   sum=0 ;
 
   while(no!=0)
   {
       rem=no%10 ;
       if(isPrime(rem))
       {
           sum=sum+rem ;
       }
       no=no/10 ;
   }
   cout<<sum<<endl ;
}
return 0;
}

bool isPrime(int no)
{
    int i ;
    if(no==1)
        return false ;
       
    for(i=2;i<no;i++)
    {
        if(no%i==0)
            return false ;
    }
    return true ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )