Program to check whether a number is prime or not in C

Source Code

// 14.i. Program to check whether a number is prime or not
// Arpan Das - CST 2nd Year (VOCLET)

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

void main () {
	int i, num, flag=1; // 1 = prime
	//system("cls");
	//clrscr();

	printf("\t\t..:: check whether a number is prime or not ::..\n\n");	
	printf("pleae enter any number: ");
	scanf("%d", &num);

	for (i=2; i<=num/2; i++) {
		if (num%i == 0) {
			flag = 0;
			break;
		}
	}		
	
	if (flag==1) 
		printf("%d is a prime number.", num);
	else 
		printf("%d is not a prime number.", num);	
	
	//getch();
}

Output

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

pleae enter any number: 15
15 is not a prime number.

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

pleae enter any number: 17
17 is a prime number.

Leave a Reply

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