Monday 21 August 2017

Sum of primes

Your task is to calculate sum  of primes present as digits of given number 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
2 ≤ N ≤ 50000

Example:
Input:
2
333
686
Output:
9
0
                                         C-Solution
#include <stdio.h>

int main() {
     int t,num,ans,dig;
    scanf("%d",&t);
    while(t--)
    {
        ans=0;
       scanf("%d",&num);
       while(num)
       {
            dig=num%10;
            num/=10;
            if(dig==2 || dig== 5 || dig==7 || dig==3) ans+=dig;
       }
       printf("%d\n",ans);
    }
    return 0;
}

No comments:

Post a Comment