C Program to find Prime Numbers List & in VBA Too

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.

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

//Include Header Files

#include <stdio.h>

#include <stdlib.h>

intmain(intargc,char*argv[])

{intn1,n2,i,tempnum;

intprimeflag;

//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,

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).

Visual Basic

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

OptionExplicit

'VBA program to print Prime numbers

SubPrint_Prime_Numbers_List()

Dimn1,n2,i,tempnum,iRow

Dimprime_num_flag AsBoolean

'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

Fori=2To(n1/2)

If((n1 Modi)=0)Then

prime_num_flag=False

ExitFor

EndIf

Nexti

'Print Prime Numbers List once it is verified

Ifprime_num_flag=TrueThen

ThisWorkbook.Sheets(1).Cells(iRow,1)=n1

iRow=iRow+1

EndIf

n1=n1+1

Wend

EndSub

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.

Author: Kumarapush

Kumarapush has experience in IT industry for 12+ Years including 10 Years in MNC. Has vast expertise in Microsoft Excel automation using VBA Macro. Excel to CSV converter is one of his famous tool that has been downloaded & used by 68,000 users around the globe.