This is a code for find a number whether it's prime or not. Is this code incorrect ???? Thank you. 😊 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

This is a code for find a number whether it's prime or not. Is this code incorrect ???? Thank you. 😊

#include <stdio.h> void main() { int a , count = 0; printf ("enter a number\n"); scanf("%d",&a); for(int i=1;i<=a;i++) { if((a%i==0) && (a%1==0)) count++; } if((count == 2) || (count == 1)) printf("%d is a prime number\n" , a); else printf("%d this is not a prime number\n" , a ); }

3rd Aug 2018, 11:52 AM
primex
primex - avatar
2 Answers
+ 1
Yes it works but try it yourself in the code playground.
3rd Aug 2018, 12:41 PM
B K
+ 1
You can improve it a little more. Looping upto sqrt(a) is enough, no need to loop upto full a.
3rd Aug 2018, 1:21 PM
nillyhan