Question:C code to display prime numbers within a range 

Answer 
#include<stdio.h>

int main(){

    int num,i,count,min,max;

     printf("Enter min range: ");
     scanf("%d",&min);

    printf("Enter max range: ");
    scanf("%d",&max);

    for(num = min;num<=max;num++){

         count = 0;

         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }
        
         if(count==0 && num!= 1)
             printf("%d ",num);
    }
  
   return 0;
}
Alternate with while loop
#include<stdio.h>

int main(){

   int num,i,count,min,max;

printf("Enter min range: ");
    scanf("%d",&min);

    printf("Enter max range: ");
    scanf("%d",&max);

    num = min;
    while(num<=max){

         count = 0;
         i=2;

         while(i<=num/2){
             if(num%i==0){
                 count++;
                 break;
             }
             i++;
        }
        
         if(count==0 && num!= 1)
             printf("%d ",num);

         num++;
    }
  
   return 0;
}
Sample output: Enter min range: 10 Enter max range: 50 11 13 17 19 23 29 31 37 41 43 47 

+ ExplanationNot Moderated
+ Report
Total Preview: 917
C code to display prime numbers within a range
Copyright © 2024. Powered by Intellect Software Ltd