Answer to Question #300976 in C for Jeevan

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.

1
Expert's answer
2022-02-22T06:45:26-0500
#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);
}

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