C language - Checking whether an integer is a prime number

A program in the C language can be used to check if an integer is a prime number. Some knowledge of programming concepts and languages like C is required to write a program code in C. Basic concepts such as looping, including for loops, while do loops, if else loops, functions, etc, are necessary to write programs. A program to check if an integer is a prime number in C can be written by using nested for loops. Nested for loops contain one for loop inside another. Some knowledge of C functions such as scanf and printf will also prove to be helpful when programming in C.

[C language]Checking whether an integer is a prime number




Definition of a prime number


A prime number is an integer, which is divided only by 1 and itself.

Algorithm 1: divisors between 2 and N-1 will be tested


/**************************    

  • prime_number1.c */
/* algorithm : test all the dividers */ #include <stdio.h> int main (void) { int i, nb, count, test; test = count = 0; printf ("enter integer: "); if (scanf ("%d", &nb) != 1) return -1; for (i = 2; i < nb; i++, count++) if (nb % i == 0) test = 1; if (!test) printf ("%d prime number, number of iterations = %dn", nb, count); else printf ("%d is not a prime number, number of iterations = %dn", nb,count); return 0; }

Algorithm 2: Even divisors will not be tested, research is limited to odd divisors


/**************************    

  • prime_number2.c */
/* algorithm : exclude even numbers and
  • test all the dividers */
#include <stdio.h> int main (void) { int i, nb, count, test; test = count = 0; printf ("enter integer: "); if (scanf ("%d", &nb) != 1) return -1; if (nb % 2 == 0) test = 1; else{ for (i = 3 ; i < nb; i+=2, count++) if (nb % i == 0) test = 1; } if (!test) printf ("%d prime number, number of iterations = %dn", nb, count); else printf ("%d is not a prime number, number of iterations = %dn",nb, count); return 0; }

Algorithm 3: All odd divisors up to the square root of N will be tested


/**************************    

  • prime_number3.c * /
/* algorithm : exclude all even numbers and
  • test all the dividers up to square root */
#include <stdio.h> #include <math.h> int main (void) { int i, nb, count, test,limit; test = count = 0; printf ("enter integer : "); if (scanf ("%d", &nb) != 1) return -1; limit = sqrt(nb) + 1; if (nb % 2 == 0) test = 1; else{ for (i = 3 ; i < limit; i+=2, count++) if (nb % i == 0) test = 1; } if (!test) printf ("%d prime number, number of iterations = %dn", nb, count); else printf ("%d not a prime number, number of iterations = %dn",nb, count); return 0; }

Algorithm 4: stop the program when a divider is found


/**************************    

  • prime_number4.c */
/* algorithm : exclude all even numbers and
  • test all dividers up to the square root
  • exit loop when first divider is found */
#include <stdio.h> #include <math.h> int main (void) { int i, nb, count, test,limit; test = count = 0; printf ("Enter integer: "); if (scanf ("%d", &nb) != 1) return -1; limit = sqrt(nb) + 1; if (nb % 2 == 0) test = 1; else{ for (i = 3 ; i < limit && ! test; i+=2, count++) if (nb % i == 0) test = 1; } if (!test) printf ("%d prime number, number of iterations = %dn", nb, count); else printf ("%d not prime number, number of iterations = %dn", nb, count); return 0; }
Published by deri58 - Last update on February 17, 2012 04:16 AM by Paul Berentzen
This document entitled « C language - Checking whether an integer is a prime number » from Kioskea.net (en.kioskea.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the licence, as this note appears clearly.
Suggestions
  •  C language - Checking whether an integer is a prime number
  •  C language - Handling 64-bit integers » Tips : You may need to handle very large numbers in the C language. An unsigned number of 32 bits cannot exceed a particular value. In order to handle larger integers, a separate data type for handling 64 bit integers can be used in the C programming...
  •  C Language - finding the square root of a number » Tips : Among the computer programming languages, C is the most basic and also the most widely used language. C can be used not only for text documents but can also be used for various mathematical operations. Statistical as well as mathematical exercises...
  •  Representation of real numbers and integers » Reviews : Representing a number in a computer Representing (or encoding) a number means to express it in binary form. Representing numbers in a computer is necessary in order for it to be able to store and manipulate them. However, the problem is that a...
  •  HOW TO DRAW A HUMAN FACE » Tips : HOW TO DRAW A HUMAN FACE A small programming tip from ramachandra1990 , please enjoy it. How to draw a human face? #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT,gm;...
  •  Generating random numbers with rand() » Tips : Generating random values and numbers is an integral part of computer programming.Randomisation of numbers can be created with the help of the C language. In C, there is a library function called rand which can be used for this process. There is,...
Swapping two variables without using a temp variable
C language - Handling 64-bit integers