Friday, 31 May 2013

Prime Numbers in c-programing

How to execute C-program in linix..........
1. Creage a file example.c
2. Compile it by using comand cc example.c
3. Now create new file by name a.out
    3.1: if compilation is error free then a.out file is created
    3.2: a.out is a assembler output file
4. Now you can able to run the C code by using comand ./a.out

Note: there may be N file in the directory but a.out is only one and it will execute which ever file is compiled earlier...

Using header files: 

syntak : #include<stdio.h>   // No Semicoloun is use at end
              #include<math.h>


 Finding Prime Number in C: 

#include<stdio.h>

main()
{
   int number, next_prime_after_2_is = 3, count, check;

   printf("Enter the number of prime numbers required\n");
   scanf("%d",&number);

   if ( number >= 1 )
   {
      printf("First %d prime numbers are :\n",number);
      printf("2\n");
   }

   for ( count = 2 ; count <= number ;  )
   {
      for ( check = 2 ; check <= next_prime_after_2_is - 1 ; check++ )
      {
         if ( next_prime_after_2_is%check == 0 )
            break;
      }
      if ( check == next_prime_after_2_is )
      {
         printf("%d\n",next_prime_after_2_is);
         count++;
      }
      next_prime_after_2_is++;
   }

}