Write a program which indicates if a positive number N, filled by the user, is prime or not (N is a prime number if its only divisors are 1 and N).


Difficulty level
Video recording
This exercise is mostly suitable for students
#include<stdio.h>
#include<math.h>

int main()
{
	int N;
	int i;
	
	printf("Enter an integer: ");
	scanf("%d", &N);

    for (i = 2; i <= sqrt(N); i++)
		if (N%i == 0)
			break;
 
	if (i > sqrt(N))
		printf("%d is prime.\n",N);
	else
		printf("%d is not prime.\n", N);
 
	return 0;
}


Back to the list of exercises
Looking for a more challenging exercise, try this one !!
UVA 1112 - Mice and Maze