
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Sum of the first N Prime numbers
The program to print the sum of the first N prime numbers uses the method to find n prime numbers and then add them to find the sum. This sum is saved to an integer that outputs the sum .
The code takes a number checks it for prime, if it is prime then adds it to the sum variable. Till n prime number it does the same and then after that it prints the sum.
Example Code
#include <stdio.h> int isprime(int j) { int count=0; for(int 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 = 5; int i=0, j= 1; int sum = 0; while(1) { j++; if(isprime(j)) { sum += j; i++; } if(i == n) { break; } } printf("The sum of first %d prime numbers is %d", n, sum); return 0; }
Output
The sum of the first 5 prime numbers is 28
- Related Articles
- Find the Product of first N Prime Numbers in C++
- Sum of sum of first n natural numbers in C++
- Find the sum of first $n$ odd natural numbers.
- Sum of square of first n odd numbers
- Sum of square-sums of first n natural numbers
- Sum of first n natural numbers in C Program
- 8085 program to find the sum of first n natural numbers
- C Program for the cube sum of first n natural numbers?
- Swift Program to calculate the sum of first N even numbers
- Swift Program to calculate the sum of first N odd numbers
- Difference between sum of the squares of and square of sum first n natural numbers.
- Sum of squares of the first n even numbers in C Program
- Java Program to Display Numbers and Sum of First N Natural Numbers
- Find the mean of first five prime numbers
- Python Program for cube sum of first n natural numbers

Advertisements