First 100 prime numbers list

broken image
broken image
broken image

STEP 6: In the main function: If the temp is equal to 0, STEP 4: If num is divisible by loop iterator, then increment temp. STEP 3: Iterate a “for” loop from 2 to num/2. STEP 1: Define a function that accepts an integer num. The logic is the same as the previous program.Ĭ Program for Prime Numbers Using Functions Algorithm to Find Prime Number In the following program, we have implemented a “while” loop instead of a “for” loop. check if num is divisible by any number. Printf('Enter any number to Check for Prime: ') STEP 4: Iterate a “while” with the condition, loop STEP 3: Initialize the iterator variable loop to 2. the number itself.Īlso Read: C Program for Bubble Sort to Sort Elements in An OrderĬ Program for Prime Numbers Using While Loop Algorithm to Find Prime Number Program in C That one factor can be found in the first half, as you can notice that there is only one factor in the second half and i.e. So, to find if a number is prime or not, finding only one factor is enough. 6, there is only one factor left that is the number itself (12 in this case).

broken image

You can observe here that after 12/2 i.e. What’s the reason for leaving the other half? Let us understand this with the help of examples. You could ask why we are iterating the loop till n/2 instead of n. The program is not using any extra auxiliary space, only constant space is being used. As the loop is iterating from 2 to n/2, the time complexity for the worst case will be O(n), where n is the input element.

broken image