Sum of prime numbers from 1 to 100 in c





C program to get sum of prime numbers from 1 to 100 


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

    int num,i,count,sum=0;

    for(num = 1;num<=100;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;
}
  
Output:
Sum of prime numbers is: 1060


Algorithm:


Definition of prime number:

A prime number is a natural number greater than one that possesses no divisors other than 1 and itself. In simpler terms, a prime number has only two divisors: 1 and the number itself. For instance, 5 is a prime number, and its sole divisors are 1 and 5.
Note: 2 is only even prime number.

Logic for prime number in c

We employ a loop to divide the given number by integers ranging from 2 to the square root of the number. If the number is not divisible by any of these integers, it qualifies as a prime number. This approach is efficient, as factors larger than the square root would have corresponding factors smaller than the square root, and they would have already been checked in earlier iterations.

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: