Programming Example
C Program - Command Line Program to Check if a Number is Prime or Not
Command Line Program to Check if a Number is Prime or Not
Command Line Program to Check if a Number is Prime or Not
#include <stdio.h>
int main(int argc, char *argv[])
{
int n, i, flag = 0;
n = atol(argv[1]);
for(i=2; i<=n/2; ++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}
13
13 is a prime number.
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.