wap in c to find first 20 prime number
Question
wap in c to find first 20 prime number
Solution
Sure, here is a simple C program to print the first 20 prime numbers:
#include <stdio.h>
int main() {
int i, j, count, flag;
printf("First 20 prime numbers are:\n");
// 2 is the first prime number
printf("2\n");
count = 1; // count of prime numbers
i = 3; // next number to be checked
while (count < 20) {
flag = 0;
// check divisibility of i from 2 to i/2
for (j = 2; j <= i/2; j++) {
if (i % j == 0) {
flag = 1;
break;
}
}
// if i is a prime number
if (flag == 0) {
printf("%d\n", i);
count++;
}
// check next number
i++;
}
return 0;
}
This program uses a while loop to find the first 20 prime numbers. It starts from 2 (the first prime number), and for each subsequent number, it checks whether the number is divisible by any number from 2 to the number divided by 2. If the number is not divisible by any of these numbers, it is a prime number, and the program prints it and increments the count of prime numbers. The loop continues until it finds 20 prime numbers.
Similar Questions
WAP to print the prime numbers between 1 – 100
write easies code for me : Write a program in C to find if a number is prime or not.
wap in java to find hcf and lcm of 2 numbers
Youwrite a program to find first 50 prime numbers
write a c program to print unique prime numbers in array
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.