+ 1
Fix it for me plz prime nimber
#include<stdio.h> int main() { Â Â int s=0,i,n,count=0; Â Â printf("Enter a positive number:"); Â Â scanf("%d",&n); Â Â for(i=1;i<=n; i++) Â Â { Â Â Â Â if(n%i==0){ Â Â Â Â Â Â printf(" %d \n",i); Â Â Â Â Â Â count++; Â Â Â Â } Â Â } Â Â if(count==2){ Â Â Â Â printf("Prime"); Â Â } Â Â else{ Â Â Â Â printf("Not Prime"); Â Â } Â Â return 0; }
2 Réponses
+ 1
C
int n;
scanf("%d",&n);
int p=1;
for (int i=2; i<n; i++) if (n %i == 0) {p=0; break;}
if (p==1)
printf("%d %s\n", n,"is a prime number");
else
printf("%d %s\n", n,"is not a prime number");
https://code.sololearn.com/cn7bc0Uk3evY
C#
public static bool chkPrime(int num)
{
for (int i=2; i<num; i++) if (num %i == 0) return false;
return true;
}
https://code.sololearn.com/cJqJ46Lalczq
0
Typo is there... If(count==2).. And remove unused variable like s