Tuesday 12 February 2013

Displaying prime numbers between given numbers in C

This program is another example of application of loop. Here the prime numbers in given range are displayed using two loops.The outer loop increases the number from starting point to end point and inner loop checks whether the number from outer loop is prime or not. If prime it displays it not it does not. The complete source code and output is given here
Source Code


1: //displaying the prime numbers in given range
2: #include<stdio.h>
3: 
4: int main(){
5:     int start_point, end_point, i, r, k, p;
6:     printf("Enter starting point: ");
7:     scanf("%d",&start_point);
8:     printf("Enter end point: ");
9:     scanf("%d",&end_point);
10: 
11:     printf("The prime numbers between %d and %d are\n",start_point,end_point);
12: 
13:     for(i = start_point; i <= end_point; i++){
14:         p = 2;
15:         do{
16:             r = i%p;
17:             if(r==0)
18:             break;
19:             p++;
20:         }while(p<=i);
21:         if(p == i){
22:             printf("%d ",i);
23:         }
24:     }
25:     printf("\n\n");
26:     return 0;
27: }
28: 

Output

prime

No comments:

Post a Comment

Comment