Write a code to find the frequency of prime numbers from a user input queue
#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;
}
Comments
Leave a comment