Home / Programs / C Program - Command Line Program to Check if a Number is Prime or Not
Programming Example

C Program - Command Line Program to Check if a Number is Prime or Not

👁 697 Views
💻 Practical Program
📘 Step by Step Learning

Command Line Program to Check if a Number is Prime or Not

Program Code

#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;

}

Output

13

13 is a prime number.

Explanation

Nope

How to learn from this program

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.