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.
//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 program printf("Parameters passed:"); for (i=1;i<argc;i++) printf("%s ;",argv[i]); //Check for error in Arguments if(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 numbers if ((n1==0) || (n2==0)) { printf("\nEnter a Valid Number Range\n"); return(1); } //if User entered number in descending order if (n2 < n1) { tempnum = n1; n1 = n2; n2 = tempnum; } printf("\nGenerating Prime numbers from %d - %d\n",n1,n2); //Actual loop to generate prime numbers while (n1 <= n2) { primeflag=0; for (i=2;i<=n1/2;i++) { if(n1%i == 0) { primeflag=1; break; } } //Prime number detection if (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,
//In case Turbo C is installed in D drive, provide the command as below D:\TC\BIN\Primegen 1 100 Parameters passed:1 ;100 ; Generating Prime numbers from 1 - 100 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
You 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).
Option Explicit 'VBA program to print Prime numbers Sub Print_Prime_Numbers_List() Dim n1, n2, i, tempnum, iRow Dim prime_num_flag As Boolean 'Get Input Numbers n1 = ThisWorkbook.Sheets(1).Cells(1, 1) n2 = ThisWorkbook.Sheets(1).Cells(1, 2) iRow = 2 'Take Mod, Verify and Print Prime Number While (n1 < n2) prime_num_flag = True For i = 2 To (n1 / 2) If ((n1 Mod i) = 0) Then prime_num_flag = False Exit For End If Next i 'Print Prime Numbers List once it is verified If prime_num_flag = True Then ThisWorkbook.Sheets(1).Cells(iRow, 1) = n1 iRow = iRow + 1 End If n1 = n1 + 1 Wend End Sub
This 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.