Prime Number Program In C

Program to find whether a given number is Prime or Not

```#include<stdio.h>

int main()
{
/* Declaring variable for n=number, c=counter variable:holds
number of factor of 'n' */
int n,c=0, i;

// Inputting number
printf("Enter number to check whether Prime or Not:");
scanf("%d",&n);

// Checking whether number valid or not
if(n>0)
{

// Checking whether number is prime or not
for(i=1;i<=n;i++)
{
if(n%i==0)
c=c+1;
}

if(c==2)
printf("Prime Number");
else
printf("Not a Prime Number");
}

else
printf("Not a valid Number");```
```
return 0;
}
```

Program to print ( list ) all the prime number between 1 to n

```#include<stdio.h>

int main()
{
/* Declaring variable for n=number, c=counter variable:holds
number of factor of 'n' */
int n,c, i,j;

// Inputting number
printf("Enter number till which you want to list prime number:");
scanf("%d",&n);

// Checking whether number valid or not

if(n>0)
{
printf("List of Prime Numbers:");
// Checking whether number is prime or not
for(i=1;i<=n;i++)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
c=c+1;
}
if(c==2)
printf("%d ",i);
}

}
else
printf("Not a valid Number");

return 0;
}```

Latest
Previous
Next Post »