Learn C programming from these daily lessons I found. They're eleven
days ahead of you already so get busy...
http://www.visualcmaniac.com
DON'T! Unless you want to learn how to write *bad* C-like programs.
And here's why ("Lesson" 12):
#include <stdio.h>
#include <math.h>
Not necessary to include math.h as it's not used below
After this, all bets are off. Watch pink pigs crawl out your ears
(undefined behaviour, `main` has to return `int`).
int i, n, prime=1; // prime is true
Comments like these are OK in C99.
printf("Input natural number :");
Not terminating `printf` with \n makes it possible for the
implementation to NOT print anything out (at least not before firts \n
it sees).
No error checking. What if user enters something that's not an integer.
Even if integer is entered, what if it's negative?
for( i=2; i<= n-1; i++) {
if( n % i == 0 ) { // also possible to state if(!(n % i))
Much better to parenthesise appropriatelly:
if( (n % i) == 0 ) { // also possible to
state if(!(n % i))
prime =0; // prime is now false
break;
}
}
if( prime )
printf("%d is prime number !\n", n);
else
printf("%d isn't prime number!\n", n);
}
Spacing is atrocious (or sloppy, if you will).
I didn't check whether the algorithmu used is actually correct either.