Question #300976

A company is planning to bucketize



its data to improve the search



operation. The data is a sequence of



digits [0-9]. The bucket ID is



calculated as the sum of all the prime



digits in the data. The prime number



is a number that is divisible only by



itself and 1.



Write an algorithm to find the bucket



ID for the data as the sum of all



prime digits in the data.

Expert's answer

#include <stdio.h>
#include <string.h>

int main()
{
    char sequence[100];
    int digit, isprime, sum;
    sum = 0;
    printf("Enter sequence of digits: ");
    scanf("%[^\n]", sequence);    
    for(int i = 0; i< strlen(sequence); i++)
    {
        digit = sequence[i] - '0';
        isprime = 1;
        for(int j = 2; j < digit; j++)
            if (digit % j == 0)
                isprime = 0;
        if (isprime == 1 && digit > 1)        
            sum += digit;
    }
    printf("\nSum prime digits: %d\n", sum);
}
LATEST TUTORIALS
APPROVED BY CLIENTS