Latest Updates ☛
  • Graphs: Introduction of graphs and types of graphs
  • SPOJ : Geeky solves geeky solution
  • Solution Code : Pass the time problem on SPOJ.
  • Find the minimum shortest path solution in c language ! !

Saturday, August 10, 2013

C program to generate Prime Numbers between two given numbers ! !


Prime number Generator in C language

The C version of the prime number generator as givern earlier for java version this uses same working concepts as java....
A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number. For example, 5 is prime because only 1 and 5 evenly divide it, whereas 6 is composite because it has the divisors 2 and 3 in addition to 1 and 6. Thefundamental theorem of arithmetic establishes the central role of primes in number theory: any integer greater than 1 can be expressed as a product of primes that is unique up toordering. The uniqueness in this theorem requires excluding 1 as a prime because one can include arbitrarily many copies of 1 in any factorization, e.g., 3, 1 × 3, 1 × 1 × 3, etc. are all valid factorizations of 3.
Source:Wikipedia : Prime_number
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//Program to generate prime number
//*** By CodeAuthor

#include<stdio.h>

void prime(int m,int n) //function to generate prime numbers
{ 
  if(m>n)
         return;

   int j=2;
   int flag=1;
   while(j<m)
        {
             if(m%j==0)
             flag = 0;
            j++;
        }
   if(flag)
      printf("%d ",m);  //printing numbers
   prime(m+1,n);       //recalling self
}


int main()
{
int m,j,n,t;
printf("Enter test cases : ");
scanf("%d ",&t);

        
while (t > 0)
{
       printf("\nEnter starting and ending  numbers : ");
       scanf("%d",&m);   //starting no.
       scanf("%d",&n);   //ending no.
             
       if(m==1 || m==0)
               m=2;
       printf("\nThe nubers are : ");       
       prime(m,n);   //calling function
            
       printf("\n");
       t--;
            
}
        
getch();
        
}
 

The output of the program is -----



Enter test cases : 3

Enter starting and ending  numbers :0 10
The numbers are : 2 3 5 7

Enter starting and ending  numbers : 1 20
The numbers are : 2 3 5 7 11 13 17 19

Enter starting and ending  numbers : 50 70
The numbers are : 53 59 61 67

No comments :

Post a Comment

Privacy Policy | Disclaimer | Terms of Service

Copyright © 2013, Code Author. Powered by Blogger.