i need help how can i program a code in C language that if the user inputs a number it will check if thenumber is prime or not
32 Answers
New Answerwithout using for loop.. only if else statements
12/11/2018 1:27:08 AM
Cjay Medallo32 Answers
New AnswerCjay Medallo , yes, your code looks good now. All we have to do is convert the following for loop into a while loop for(i = 2; i <= n/2; ++i) { if(n%i == 0) { num = 1; break; } } Well, that's not too hard. It has three parts: 1. An initialization (i=2), which happens only ONCE at the beginning, 2. a test condition (i<=n/2), which is checked at the beginning of each loop, and 3. an update statement (++i) executed at the end of each loop. We can take care of #2 with while (i<=n/2), but the rest we'd have to put at appropriate places ourselves. But that's not hard at all if you know what they do: #1 goes right before the while loop, and #3 goes right at the end (but inside) of the loop. So it'd look like i = 2; while (i <= n/2) { if(n%i == 0) { num = 1; break; } ++i; } Does that make sense? If you understand this you'd be able to convert any for loop in C into a while loop.
https://code.sololearn.com/chDtCd6ZOwZH/?ref=app alpha5and5 here's you corrected recursive code
Well, can you write the code using for loop, save it in the code playground, and share a link? Then I can help you convert it into a while loop version.
It has the correct logic, but you need to print if it is a prime or not. Can you not finish the code?
alpha5and5 please make a separate post in the Q&A section, and I'm sure someone will help you out!
It looks good, but you seen to have accidentally written the same code segment twice. First from lines 5-12, and then again from 13-20. Just using that once should be enough.
yeah one is enough sorry.. because when i code it using that the number 1 print f can't execute.. i don't understand that's why i write it twice then it works.. sorry for the hassle bro.. thank you so much i appreciate your effort
You're welcome! 😊 I'm not sure what happened before, but it looks like it's working now, even with 1, right?
alpha5and5 your code isn't correct. It says 7 is not a prime. The scanf part was correct in the original code. And the use of break was also a great idea. And please, let the original poster finish their code. Thanks!
is it printf("Prime number"); if and then else.. i don't know sorry.. im new so pls help
i see.. i'm also new in C .. i focused in web dev.. i will try my best to finish this thank you all
i think it's right.. pls check.. the problem is we are not allowed to use for loop.. maybe there is a way to code it without using for loop so pls help me
Learn Playing. Play Learning
SoloLearn Inc.
4 Embarcadero Center, Suite 1455Send us a message