• ## 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.

1. Get the Input number (n1).
2. Divide the number with all number between 2 to n1/2(inclusive).
3. Get the remainder from each division (n1%mod).
4. If we get a Zero in any of this iteration, then the number is not a Prime number.
5. 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)).

Also Read: Excel VBA - Get Extended File Property - Author Modified Date Time

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

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.

Also Read: Open Text File - Read Write File Operations - Any Programming Language

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

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.

Previous Post:
Next Post: