My first ever project as a newbie - seems almost there and I'm getting outputs that make sense. Problem is that the if statement in the for loop wont break if (n%i<1)...
I've included the whole code in case I've messed up in the declarations...
//Program to take a number and see if it's prime
#include<stdio.h>
#include<conio.h>
int main() {
int n, i, f;
f = 0;
//input
printf("Enter a positive integer:");
scanf_s("%d", &n);
printf("%d is flag \n", f);
//logic
for (i=2;i<=n/2;i++) {
printf("%d \t %d \n\n",i ,n%i); //debug info printing
if (n%i<1) {
f = 1;
break;
}
}
// output
if (f = 1)
printf("%d is not a prime number. \n", n);
else
printf("%d is a prime number. \n", n);
printf("%d is flag\n", f);
_getch();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire