+ 2
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
without using for loop.. only if else statements
32 ответов
+ 1
Cjay 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.
+ 5
alpha5and5 Most welcome ^_^ , fixed the bug check it now .
+ 4
https://code.sololearn.com/chDtCd6ZOwZH/?ref=app
alpha5and5 here's you corrected recursive code
+ 3
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.
+ 1
Is while loop allowed? Or recursion? Otherwise I don't see a good way to do it.
+ 1
It has the correct logic, but you need to print if it is a prime or not. Can you not finish the code?
+ 1
alpha5and5 please make a separate post in the Q&A section, and I'm sure someone will help you out!
+ 1
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.
+ 1
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
+ 1
You're welcome! 😊
I'm not sure what happened before, but it looks like it's working now, even with 1, right?
+ 1
yep it's perfectly fine now.. again thanks for the help Kishalaya Saha
0
yeah only for loop.. is not allowed pls help i need to understand it
0
that is all i can do.. im new.. i need some expert to check if it's right or not
0
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!
0
is it printf("Prime number");
if and then else.. i don't know sorry.. im new so pls help
0
i see.. i'm also new in C .. i focused in web dev.. i will try my best to finish this thank you all
0
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