Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Write a Java program to find the sum of the first n prime numbers

Write a C++ program to find sum of first n prime numbers

Posted on December 18, 2020

Table of Contents

  • Write a C++ program to find sum of first n prime numbers
    • Code to calculate sum of first n prime numbers
      • Code to calculate sum of first n prime numbers using for loop
      • Code to calculate sum of first n prime numbers using while loop
      • Code to calculate sum of first n prime numbers using do-while loop

Write a C++ program to find sum of first n prime numbers

In this article, we will discuss the concept of Write a C++ program to find sum of first n prime numbers

In this code, we are going to learn how to write to calculate sum of the first n prime numbers  using different methods in C++ program.

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

Code to calculate sum of first n prime numbers

Code to calculate sum of first n prime numbers using for loop

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

Program 1

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

int main()
{
    int i,j=2,n,counter=0,sum=0,flag=1;
    //the program ask to input maximum limit
    cout<<"Enter the value to n: ";
    cin>>n;
    //store the maximum in the max variable
while(counter<n){
    for(i=2; i<=j-1; i++){
            if(j%i==0){
                flag=0;
                break;
        }
    }
       // when the 'i' is prime then add to sum
       if(flag){
        sum+=j;
        counter++;
       }
       j++;
       flag=1;
    }
    cout<<"sum of first "<<n<<" prime numbers:"<<sum;
    getch();
    return 0;
}

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

Enter the value to n:100
sum of first 100 prime numbers: 24133

 

Code to calculate sum of first n prime numbers using while loop

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

Program 1

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

int main()
{
    int i,j=2,n,counter=0,sum=0,flag=1;
    //the program ask to input maximum limit
    cout<<"Enter the value to n: ";
    cin>>n;
    //store the maximum in the max variable
while(counter<n){
        i=2;
    while(i<=j-1){
            if(j%i==0){
                flag=0;
                break;
        }
 i++;
    }
       // when the 'i' is prime then add to sum
       if(flag){
        sum+=j;
        counter++;
       }
       j++;
       flag=1;
    }
    cout<<"sum of all first "<<n<<" prime numbers:"<<sum;
    getch();
    return 0;
}

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

Enter the value to n:1000
sum of first 100 prime numbers: 3682913

 

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

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

Program 1

#include <iostream>
#include <conio.h>
using namespace std;
int isPrime(int j){
int counter=0,i;
for(i=2; i<=j/2; i++){
            if(j%i==0){
                counter=1;
        }
}
if(counter==0)
    return 1;
else return 0;
}
int main(void)
{
    int n;
    cout<<"Enter value for n: \n";
    cin>>n;
    int i=0,j=1,sum=0;
    while(1){
        j++;
        if(isPrime(j)){
        sum+=j;
        i++;
       }
       if(i==n){
        break;
       }
    }
        cout<<"sum of all first "<<n<< "prime numbers:"<<sum;
getch();
    return 0;
}

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

Enter the value to n:500
sum of first 500 prime numbers: 824693

 

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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes