C Language

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

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 <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j=2,n,count=0,sum=0,flag=1;
    //the program ask to input maximum limit
    printf("Enter the value for n: ");
    scanf("%d",&n);
    //store the maximum in the max variable
while(count<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;
        count++;
       }
       j++;
       flag=1;
    }
    printf("sum of all first %d prime numbers: %d",n,sum);
    getch();
    return 0;
}

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

Enter the value for n: 25
Sum of all first 25 prime numbers: 1060

 

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 <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j=2,n,count=0,sum=0,flag=1;
    //the program ask to input maximum limit
    printf("Enter the value to n: ");
    scanf("%d",&n);
    //store the maximum in the max variable
while(count<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;
        count++;
       }
       j++;
       flag=1;
    }
    printf("sum of all first %d prime numbers: %d",n,sum);
    getch();
    return 0;
}

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

Enter the value for n: 100
Sum of all first 25 prime numbers: 24133

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 <stdio.h>
#include <stdlib.h>
int isPrime(int j){
int count=0,i;
for(i=2; i<=j/2; i++){
            if(j%i==0){
                count=1;
        }
}
if(count==0)
    return 1;
else return 0;
}
int main(void)
{
    int n;
    printf("Enter value for n: \n");
    scanf("%d",&n);
    int i=0,j=1,sum=0;
    while(1){
        j++;
        if(isPrime(j)){
        sum+=j;
        i++;
       }
       if(i==n){
        break;
       }
    }
        printf("sum of all first %d prime numbers: %d",n,sum);
getch();
    return 0;
}

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

Enter the value for n: 150
Sum of all first 25 prime numbers: 59269

 

Suggested post

for loop in C language

while loop in C language

do-while loop in C language

if statements in C language

function 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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.