Write a function that check if a number is prime.
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
#include<math.h>
int isPrime(int nb)
{
int i;
for (i = 2; i <= sqrt(nb); i++)
if (nb%i == 0)
return 0;
return 1;
}
void main()
{
int a;
printf("Enter an integer: ");
scanf("%d", &a);
if (isPrime(a))
printf("%d is prime.\n",a);
else
printf("%d is not prime.\n", a);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Implementation of a deque using a doubly-linked list