![]() This process repeats until any multiple of num is found. If it is, then it returns false else the recursive call is made. Here, a recursive function find_Prime is made that simply checks if the num is divisible by any number. This is a recursive approach to find the prime numbers. Printf("%d is not a Prime number \n", num) check if num is divisible by any number recursive function to check if a number Pseudocode to Find Prime NumberĮND Implementing C program for Prime Numbers STEP 6: Recursively call the function and pass num as an argument. STEP 4: If num is divisible by “i”, then RETURN false. STEP 4: If num is equal to “i”, then RETURN true. STEP 3: If num is equal to 0 or 1, then RETURN false. STEP 1: Define a recursive function that accepts an integer num. So, the space complexity will be O(1).Īlso Read: Learn C Program for String Palindrome C Program for Prime Numbers Using Recursion Algorithm to Find Prime Number The function uses only constant space to store the variables. Therefore, in the worst case, the time complexity will be O(n). The function has only one “for” loop that runs from 2 to n/2. Printf("\n %d is Not a Prime Number", num) STEP 6: In the main function: If the temp is equal to 0,ĮND Implementing C Program for Prime Numbers STEP 1: Define a function that accepts an integer num. ![]() C Program for Prime Numbers Using Functions Algorithm to Find Prime Number In this program, only constant space is being used for some variables. The loop in the program runs from 2 to n/2, therefore, the time complexity for the worst case will be O(n). The logic is the same as the previous program. 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 C Program for Prime Numbers Using While Loop Algorithm to Find Prime Number 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). 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. Pseudocode to Find Prime NumberĪs 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. STEP 4: If num is divisible by loop iterator, then increment temp. STEP 3: Iterate a “for” loop from 2 to num/2. (n - 1) ! ≡ (n-1) mod n C Program for Prime Numbers Using For Loop Algorithm to Find Prime Number
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |