Lowest price ever! Learn Generative AI for 48% less!
Get my discount0
Here is my code for sum of the n prime number. If you want you can print all the prime number also with this code. Though I omit 1 as prime number as I don't believe that's a prime number 😁 (add it by just put a ssingla line if you want)
#include<iostream>
using namespace std;
int main() {
int n, i = 3, count, c, sum=0;
cout<< "Enter the number of prime numbers which required to be added "<<endl;
cin>>n;
if ( n >= 1 ) {
cout<<"First "<<n<< " prime numbers are :\n2\n";
sum+=2; }
for ( count = 2 ;count <= n ; ) {
for ( c = 2 ; c <= i - 1 ; c++ ) {
if ( i%c == 0 ) break;
}
if ( c == i ) {
cout<<i<<endl;sum+=i; count++;}
i++;
}
cout<<"The total sum is : "<<sum;
return 0; }