Write a program to Display Prime Numbers Between Intervals Using Function.
Prime Number: A number which is divisive by 1 and itself then the number is prime otherwise the number is not a prime number.
Note: 1 is not a prime
C Program
#include <stdio.h>void primeRange(int start, int end){for (int i = start; i <= end; i++){if (i == 1){continue;}else{int flag = 0;for (int j = 2; j < i; j++){if (i % j == 0){flag = 1;break;}}if (flag == 0){printf("%d ", i);}}}}int main(){int start, end;printf("Enter starting number : ");scanf("%d", &start);printf("Enter ending number : ");scanf("%d", &end);printf("Prime number in range %d to %d are:\n", start, end);primeRange(start, end);return 0;}
Input:
Enter starting number : 1
Enter ending number : 20
Output:
Prime number in range 1 to 20 are:
2 3 5 7 11 13 17 19
No comments:
Post a Comment