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 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

11th Dec 2018, 1:27 AM
Christian Jay Medallo
Christian Jay Medallo - avatar
25 Answers
+ 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.
11th Dec 2018, 2:48 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 5
alpha5and5 Most welcome ^_^ , fixed the bug check it now .
11th Dec 2018, 1:19 PM
Anon Fox
Anon Fox - avatar
+ 4
11th Dec 2018, 8:46 AM
Anon Fox
Anon Fox - avatar
+ 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.
11th Dec 2018, 1:40 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
Is while loop allowed? Or recursion? Otherwise I don't see a good way to do it.
11th Dec 2018, 1:33 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
It has the correct logic, but you need to print if it is a prime or not. Can you not finish the code?
11th Dec 2018, 2:05 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
alpha5and5 please make a separate post in the Q&A section, and I'm sure someone will help you out!
11th Dec 2018, 3:18 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 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.
12th Dec 2018, 10:47 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 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
12th Dec 2018, 10:58 AM
Christian Jay Medallo
Christian Jay Medallo - avatar
+ 1
You're welcome! 😊 I'm not sure what happened before, but it looks like it's working now, even with 1, right?
12th Dec 2018, 11:08 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
yep it's perfectly fine now.. again thanks for the help Kishalaya Saha
12th Dec 2018, 12:44 PM
Christian Jay Medallo
Christian Jay Medallo - avatar
0
yeah only for loop.. is not allowed pls help i need to understand it
11th Dec 2018, 1:35 AM
Christian Jay Medallo
Christian Jay Medallo - avatar
11th Dec 2018, 1:51 AM
Christian Jay Medallo
Christian Jay Medallo - avatar
0
that is all i can do.. im new.. i need some expert to check if it's right or not
11th Dec 2018, 1:52 AM
Christian Jay Medallo
Christian Jay Medallo - avatar
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!
11th Dec 2018, 2:14 AM
Kishalaya Saha
Kishalaya Saha - avatar
0
is it printf("Prime number"); if and then else.. i don't know sorry.. im new so pls help
11th Dec 2018, 2:26 AM
Christian Jay Medallo
Christian Jay Medallo - avatar
11th Dec 2018, 2:26 AM
Christian Jay Medallo
Christian Jay Medallo - avatar
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
11th Dec 2018, 2:35 AM
Christian Jay Medallo
Christian Jay Medallo - avatar
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
11th Dec 2018, 2:40 AM
Christian Jay Medallo
Christian Jay Medallo - avatar
11th Dec 2018, 2:40 AM
Christian Jay Medallo
Christian Jay Medallo - avatar