Write a C program to print all Prime numbers between 1 to n using loop. C program to print all prime number within a given range. How to print all prime numbers between given interval using loop in C program. Logic to print prime numbers in a given range in C program.
Example
Input
Input lower limit: 1 Input upper limit: 20
Output
Prime numbers between 1-20: 2, 3, 5, 7, 13, 17, 19
Required knowledge
Basic C programming, If else, For loop, Nested loops
What is Prime number?
Prime number is a positive integer greater than 1 that is only divisible by 1 and itself. For example: 2, 3 , 5, 7, 11 are the first five prime numbers.
Logic to print prime numbers between 1 to n
Below is the step by step descriptive logic to print all prime numbers between 1 to n.
- Read upper limit to print prime numbers from user. Store it in some variable say n.
- Run a loop from 1 to n, increment 1 in each iteration. The loop structure should be like for(i=1; i<=n; i++).
- Inside the loop print the value of i if it is prime. Now how do you check prime number? Below post contains detail explanation of checking prime number read it.
Let me write down the above logic to C program.
Program to print prime numbers between 1 to n
/** * C program to print all prime numbers between 1 to n */ #include <stdio.h> int main() { int i, j, n, isPrime; //isPrime is used as flag variable /* Read upper limit to print prime */ printf("Find prime numbers between 1 to : "); scanf("%d", &n); printf("All prime numbers between 1 to %d are:\n", n); /* Find all Prime numbers between 1 to n */ for(i=2; i<=n; i++) { /* Assume that the current number is Prime */ isPrime = 1; /* Check if the current number i is prime or not */ for(j=2; j<=i/2; j++) { /* * If i is divisible by any number other than 1 and self * then it is not prime number */ if(i%j==0) { isPrime = 0; break; } } /* If the number is prime then print */ if(isPrime==1) { printf("%d, ", i); } } return 0; }
So once you are done with generating prime numbers between 1 to n. You can easily modify the program to work for any range. To print all prime numbers in given range. You need to input upper as well as lower limit from user. Let us modify the above program to work for prime numbers in given range.
Program to print prime numbers in given range
/** * C program to print all prime numbers between 1 to n */ #include <stdio.h> int main() { int i, j, start, end; int isPrime; //isPrime is used as flag variable /* Read upper and lower limit to print prime */ printf("Enter lower limit: "); scanf("%d", &start); printf("Enter upper limit: "); scanf("%d", &end); printf("All prime numbers between %d to %d are:\n", start, end); /* Find all Prime numbers between 1 to n */ for(i=start; i<=end; i++) { /* Assume that the current number is Prime */ isPrime = 1; /* Check if the current number i is prime or not */ for(j=2; j<=i/2; j++) { /* * If i is divisible by any number other than 1 and self * then it is not prime number */ if(i%j==0) { isPrime = 0; break; } } /* If the number is prime then print */ if(isPrime==1) { printf("%d, ", i); } } return 0; }
Learn this program using modular approach.
Enter lower limit: 1 Enter upper limit : 100 All prime numbers between 1 to 100 are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
Happy coding ;)
Recommended post
- Loop programming exercises index.
- C program to check divisibility.
- C program to print factors of a number.
- C program to print Prime factors of a number.
- C program to print Strong numbers between 1 to n.
- C program to print Armstrong number between 1 to n.
- C program to print Perfect numbers between 1 to n.