prime numbers program in c

The following code prints prime numbers using c programming language. the number is prime if it is divisible only by one and itself. Remember two is the only even and the smallest prime number. First, few prime numbers are 2, 3, 5, 7, 11, 13, 17

#include <stdio.h>
int main()
{
  int n,t,flag=0;
  printf("\nEnter a value: ");
  scanf("%d",&n);
  for(t=2; t<=n/2; t++)
  {
    if(n%t==0)
    {
      flag=1;
      break; 
    }
  }
  if(flag==0&&n>1)
   printf("\n%d IS PRIME NUMBER",n);
  else
   printf("\n%d IS NOT PRIME NUMBER",n);
  return 0;
}

[quote]

Output:

Enter a value: 17 
17 IS PRIME NUMBER 

[/quote]

prime number program between two numbers

The following code prints all prime numbers between two input values using c programming language

#include <stdio.h>
int main()
{
  long int n,n1,n2,t,count=0;
  int flag;
  printf("\nEnter Two values: ");
  scanf("%ld%ld",&n1,&n2);
  for(n=n1; n<=n2; n++)
  {
    flag=0;    
    for(t=2; t<=n/2; t++)
    {
       if(n%t==0)
       {
         flag=1;
         break; 
       }
    }
    if(flag==0)
      printf("\n%ld.PRIME:%ld",++count,n);
  }
  return 0;
}

[quote]

Output:

Enter Two values: 10 40                                      
1.PRIME:11                                                                                                                                                   
2.PRIME:13                                                                                                                                                   
3.PRIME:17                                                                                                                                                   
4.PRIME:19                                                                                                                                                   
5.PRIME:23                                                                                                                                                   
6.PRIME:29                                                                                                                                                   
7.PRIME:31                                                                                                                                                   
8.PRIME:37 

[/quote]

Leave a Reply

Your email address will not be published. Required fields are marked *