Answer to Question #296846 in C for Kamalesh

Question #296846

Write a code to find the frequency of prime numbers from a user input queue


1
Expert's answer
2022-02-12T04:34:16-0500
#include <stdio.h>
#include <stdbool.h>



bool is_prime(int x) {
    int i;


    if ( x < 2) {
        return false;
    }


    if (x == 2) {
        return true;
    }
    if (x%2 == 0) {
        return false;
    }


    i = 3;
    while (i*i <= x) {
        if (x%i == 0) {
            return false;
        }
        x += 2;
    }
    return true;
}


int main() {
    int x, n=0, n_prime=0;
    double freq;
    
    printf("Enter an integer number: ");
    scanf("%d", &x);
    while (x > 0) {
        n++;
        if (is_prime(x)) {
            n_prime++;
        }
        printf("Enter another number (0 to exit): ");
        scanf("%d", &x);
    }


    freq = ((double) n_prime) / n;
    printf("Fequency of prime number is %f\n", freq);

    return 0;
}

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