
Examples of Prime numbers
Prime number is the one that is divisible only itself & the number ‘1’ (without giving any reminders).
If you divide it with any other number, then it will give a remainder. i.e., 7 % 7 = 0, 7 % 1 = 0 (whereas % = mod). With any other number it will result in a residual reminder. The prime number series will be like 1,2,3,5,7,11,13, 17,19 and so on.
Not all the numbers have equal interval in this series. It keep on changing. It is not predictable, but still we have few techniques to find it.
Logic to find List of Prime numbers
The complexity in finding whether a number is Prime or not is that, we have to make sure that it is not divisible by any other numbers. To prove it, we use the below logic.
 Get the Input number (n1).
 Divide the number with all number between 2 to n1/2(inclusive).
 Get the remainder from each division (n1%mod).
 If we get a Zero in any of this iteration, then the number is not a Prime number.
 If we did not get a Zero till end of iteration, then the number is Prime.
For example, if we have to find whether number 10, then we have to divide it by 2,3,4,5 (10/2) and get the remainder for each division (i.e., 10 mod (2,3,4,5)).
C Program to Find Prime numbers List
In this below we are going to find list of Prime number with C Program using while loop or for loop. It is actually exact implementation of the above logic. It will generate prime numbers list between 2 numbers.
Once the compilation & linking process is over, invoke the program from command prompt with syntax as: primegen n1 n2
Where primegen – program executable name, n1 is lowest number, n2 is largest number.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152//Include Header Files#include <stdio.h>#include <stdlib.h>int main (int argc,char *argv[]){ int n1,n2,i,tempnum;int primeflag;//Print arguments passed to main programprintf("Parameters passed:");for (i=1;i<argc;i++) printf("%s ;",argv[i]);//Check for error in Argumentsif(argc != 3){ printf("\nSyntax Error. Proper Syntax: Primegen n1 n2\n");return(1);}n1 = atoi (argv[1]);n2 = atoi (argv[2]);//Check whether user entered valid numbersif ((n1==0)  (n2==0)){ printf("\nEnter a Valid Number Range\n");return(1);}//if User entered number in descending orderif (n2 < n1){ tempnum = n1;n1 = n2;n2 = tempnum;}printf("\nGenerating Prime numbers from %d  %d\n",n1,n2);//Actual loop to generate prime numberswhile (n1 <= n2){ primeflag=0;for (i=2;i<=n1/2;i++){ if(n1%i == 0){ primeflag=1;break;}}//Prime number detectionif (primeflag == 0){ printf("%d\n",n1); //Printing prime numbers list}n1 = n1+1;}return(0);}
Compilation of the above program varies slightly depending on the operating system & compiler used. But there wont be any change in the programming code. It can be used as such.Print Prime number from 1 to 100
Using the above program’s executable file, we can generate list of prime numbers between any range. For example to generate a list of prime numbers from 1 to 100 type as,
123456789101112131415161718192021222324252627282930//In case Turbo C is installed in D drive, provide the command as belowD:\TC\BIN\Primegen 1 100Parameters passed:1 ;100 ;Generating Prime numbers from 1  10012357111317192329313741434753596167717379838997You can also type primegen 1 100>prime_numbers_list.txt to output the results to a text file. The above command examples are given in Turbo C and Windows operating system. In Unix/Linux there will be little variation in executing the executable.
Excel VBA Program for Prime Number Generation
If you are looking for a solution to generate the Prime numbers through VBA program in Excel, then make use of the below coding. It is assumed that you give the inputs in sheet(1).
123456789101112131415161718192021222324252627282930Option Explicit'VBA program to print Prime numbersSub Print_Prime_Numbers_List()Dim n1, n2, i, tempnum, iRowDim prime_num_flag As Boolean'Get Input Numbersn1 = ThisWorkbook.Sheets(1).Cells(1, 1)n2 = ThisWorkbook.Sheets(1).Cells(1, 2)iRow = 2'Take Mod, Verify and Print Prime NumberWhile (n1 < n2)prime_num_flag = TrueFor i = 2 To (n1 / 2)If ((n1 Mod i) = 0) Thenprime_num_flag = FalseExit ForEnd IfNext i'Print Prime Numbers List once it is verifiedIf prime_num_flag = True ThenThisWorkbook.Sheets(1).Cells(iRow, 1) = n1iRow = iRow + 1End Ifn1 = n1 + 1WendEnd SubThis is not a fully implemented VBA program. I have just implemented the Prime number finding logic in here. If you execute this code more than once, the results will be overwritten in same cells. Clear them every time before executing this code or change the target location.
Recommended For You
You may also like these Posts
Previous Post: Hello World C Programming & Easy Compiling Steps
Next Post: Palindrome program in C & VBA
C Program to find Prime Numbers List & in VBA Too
Post Categories
Popular Posts
Similar Pages