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

}
                        

🖥 Program Output

13

13 is a prime number.


                            

📘 Explanation

Nope
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.