C++

C++ program to calculate sum of prime numbers between 1 to n

C++ program to Calculate sum of prime numbers between 1 to n

In this article, we will discuss the concept of C++ program to calculate sum of prime numbers between 1 to n

In this code, we are going to learn how to find sum of prime numbers 1 to n using different methods in C++ language.

This is done using for loop,while loop,do-while loop in C++ language

Code to display sum of prime numbers

Code to calculate sum of prime numbers using for loop

In this program, we will calculate sum of prime numbers 1 to n using for loop in C++ language

program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,j,maximum,counter,sum=0;
    cout<<"Enter the maximum value: ";
    cin>>maximum;

    for(i=2; i<=maximum; i++){
        counter=1;
        for(j=2; j<=i/2; j++){
            if(i%j==0){
                counter=0;
                break;
            }
        }
        if(counter==1){
            sum+=i;
        }
    }
    cout<<"sum of all prime numbers between 1 to "<<maximum<<" is: "<<sum;
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter the maximum value:30
Sum of all prime numbers between 1 to 30 is:129

 

Code to calculate sum of prime numbers using while loop

In this program, we will calculate sum of prime numbers 1 to n using while loop in C++ language

program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,j,maximum,counter,sum=0;
    cout<<"Enter the maximum value: ";
    cin>>maximum;
i=2;
    while(i<=maximum){
        counter=1;
        j=2;
        while( j<=i/2){
            if(i%j==0){
                counter=0;
                break;
            }
             j++;
        }
        if(counter==1){
            sum+=i;
        }
        i++;
    }
    cout<<"sum of all prime numbers between 1 to "<<maximum<<" is: "<<sum;
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter the maximum value:200
Sum of all prime numbers between 1 to 200 is:4227

 

Code to calculate sum of prime numbers using do-while loop

In this program, we will calculate sum of prime numbers 1 to n using do-while loop in C++ language

program 3

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,j,maximum,counter,sum=0;
    cout<<"Enter the maximum value: ";
    cin>>maximum;
i=2;
    do{
        counter=1;
        j=2;
        do{
            if(i%j==0){
                counter=0;
                break;
            }
             j++;
        }while( j<=i/2);
        if(counter==1){
            sum+=i;
        }
        i++;
    }while(i<=maximum);
    cout<<"sum of all prime numbers between 1 to "<<maximum<<" is: "<<sum;
    getch();
    return 0;
}


When the above code is executed, it produces the following result

Enter the maximum value:500
Sum of all prime numbers between 1 to 500 is:21534

 

 

Suggested post

Operator in C++ language

for loop in C++ language

while loop in C++ language

do-while loop in C++ language

 

Similar post

Java programming code to check prime or not

C programming code to check prime or not

C++ programming code to check prime or not

Python programming code to check prime or not

 

Code to print prime numbers from 1 to 100 or 1 to n in Java

Code to print prime numbers from 1 to 100 or 1 to n in C

Code to print prime numbers from 1 to 100 or 1 to n in C++

Code to print prime numbers from 1 to 100 or 1 to n in Python

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.