+ 1
Prime or Not
How can I write a code in C to check whether a number is Prime or Not without looping? We have only learned 1.if, else if , 2.rational operators 3.Multi conditional statement 4.and & or operators OmG I’m Dying!!! Pls help
2 Respostas
+ 3
I am not sure if it’s possible to check a number if it’s a prime or not without using a loop. The code for checking for prime is working in the way that you have the number n1 to check, and you have to devide this number by values in a range from 2 upto the this number in steps of 1. (in optimized code you don’t have to use all numbers in the given range, but that’s an other story.)
+ 4
Using recursion it should be possible to write one.
Keep a counter variable from 1, increment it at each iteration, store the result of the division in an array. Then check if the array has more than 1 divisors; if it doesn't then it's a prime else continue recursing.
Here's an article which does the same https://www.geeksforgeeks.org/recursive-program-prime-number/
But loops need not be hard and sometimes can be more clear then recursion, you can learn the basic for loop from the sololearn course
https://www.sololearn.com/learn/C/2927/
Good Luck🍀