Program to display prime numbers between two intervals in C

Source Code

// 30. Program to display prime numbers between two intervals
// Arpan Das - CST 2nd Year (VOCLET)

#include <stdio.h>
//#include <conio.h>
//#include <stdlib.h>

void getPrimes(int, int);

void main () {
	int start, end; // 1 = prime
	//system("cls");
	//clrscr();

	printf("\t\t..:: check whether a number is prime or not ::..\n\n");	
	printf("please enter the starting and ending range respectively: ");
	scanf("%d%d", &start, &end);
	
	getPrimes(start, end);	
	
	//getch();
}

void getPrimes(int start, int end) {
	int i, j, num, flag;
	
	for (i=start; i<=end; i++) {
		flag = 1;
		
		if (i == 0 || i == 1) continue;
		
		// checking each value of i
		for (j=2; j<=i/2; j++) {
			if (i%j == 0) {
				flag = 0;
				break;
			}
		}
		
		if (flag==1) 
			printf("\n%d is a prime number.", i);
	}
}

Output

		..:: check whether a number is prime or not ::..

please enter the starting and ending range respectively: 1 50

2 is a prime number.
3 is a prime number.
5 is a prime number.
7 is a prime number.
11 is a prime number.
13 is a prime number.
17 is a prime number.
19 is a prime number.
23 is a prime number.
29 is a prime number.
31 is a prime number.
37 is a prime number.
41 is a prime number.
43 is a prime number.
47 is a prime number.

Leave a Reply

Note: Your email address will not be published. Required fields are marked *