C program to find sum of prime numbers






#include<stdio.h>
int main(){

    int num,i,count,min,max,sum=0;

     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)
             sum = sum + num;
    }

    printf("Sum of prime numbers is: %d ",sum);
  
   return 0;
}

Sample output:
Enter min range: 50
Enter max range: 100
Sum of prime numbers is: 732


Algorithm:



Definition of prime number:

A prime number, in the realm of natural numbers greater than one, is characterized by having no divisors other than 1 and itself. Simply put, a prime number possesses only two divisors: 1 and the number itself. For instance, 5 is a prime number as its only divisors are 1 and 5.

Notably, 2 stands as the sole even prime number.


Logic for prime number in c
We can determine whether a number is prime by iterating through a loop and dividing the number by integers from 2 to (number/2). If the number is not divisible by any of these integers, we identify it as a prime number.

Example of prime numbers : 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199 etc.




No comments: