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

### C/C++

``````#include <stdio.h>
int main()
{
int n, i, flag = 0;

printf("Enter a positive integer: ");
scanf("%d",&n);

for(i=2; i<=n/2; ++i)
{
// condition for nonprime number
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;
}``````

### Java

``````public class Prime {

public static void main(String[] args) {

int num = 29;
boolean flag = false;
for(int i = 2; i <= num/2; ++i)
{
// condition for nonprime number
if(num % i == 0)
{
flag = true;
break;
}
}

if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}``````

### Command Line

It is highly advisable to go through Command Line Arguments Post before even looking at the code. Please study this for TCS and come back to this post later

Please comment your own version of code in the comment section below –

```#include

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;

}```