Computer Science, asked by thisisgourab92, 6 months ago

write a program in c to calculate sum of prime numbers​

Answers

Answered by ojasvasinghlavania
0

Answer:

here is your answer

Explanation:

Program to find sum of prime numbers between 1 to n

/**

* C program to find sum of prime numbers between 1 to n

*/

#include <stdio.h>

int main()

{

   int i, j, end, isPrime, sum=0;

   /* Input upper limit from user */

   printf("Find sum of all prime between 1 to : ");

   scanf("%d", &end);

   /* Find all prime numbers between 1 to end */

   for(i=2; i<=end; i++)

   {

       /* Check if the current number i is Prime or not */

       isPrime = 1;

       for(j=2; j<=i/2 ;j++)

       {

           if(i%j==0)

           {

               /* 'i' is not prime */

               isPrime = 0;

               break;

           }

       }

       /*

        * If 'i' is Prime then add to sum

        */

       if(isPrime==1)

       {

           sum += i;

       }

   }

   printf("Sum of all prime numbers between 1 to %d = %d", end, sum);

   return 0;

}

Program to find sum of prime numbers in given range

/**

* C program to find sum of prime numbers in given range

*/

#include <stdio.h>

int main()

{

   int i, j, start, end;

   int isPrime, sum=0;

   /* Input lower and upper limit from user */

   printf("Enter lower limit: ");

   scanf("%d", &start);

   printf("Enter upper limit: ");

   scanf("%d", &end);

   /* Find all prime numbers in given range */

   for(i=start; i<=end; i++)

   {

       /* Check if the current number i is Prime or not */

       isPrime = 1;

       for(j=2; j<=i/2 ;j++)

       {

           if(i%j==0)

           {

               /* 'i' is not prime */

               isPrime = 0;

               break;

           }

       }

       /*

        * If i is Prime then add to sum

        */

       if(isPrime==1)

       {

           sum += i;

       }

   }

   printf("Sum of all prime numbers between %d to %d = %d", start, end, sum);

   return 0;

}

Answered by shweta1515
1

Explanation:

Code for sum of prime numbers in c++

#include<iostream>

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;

}

cout << "Sum of prime numbers is: " << sum;

return 0;

}

Check given number is prime number or not using c++ program

Print all prime numbers from 1 to 100 using c++ program

C++ program to find all prime numbers in given max range

Program to find prime in given ranges number in c++

Code for sum of prime numbers in c++

C ++ code of sum of prime in given range

Programs of c++

Similar questions