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
Java program to find sum of prime numbers from 1 to n

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

Posted on December 11, 2020

Table of Contents

  • C program to calculate sum of prime numbers between 1 to n
    • Code to calculate sum of prime numbers
      • Code to calculate sum of prime numbers using for loop
      • Code to calculate sum of prime numbers using while loop
      • Code to calculate sum of prime numbers using do-while loop
    • Related

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

int main()
{
    int i,j,max,count,sum=0;
    printf("Enter the maximum value");
    scanf("%d",&max);

    for(i=2; i<=max; i++){
        count=1;
        for(j=2; j<=i/2; j++){
            if(i%j==0){
                count=0;
                break;
            }
        }
        if(count==1){
            sum+=i;
        }
    }
    printf("sum of all prime numbers between 1 to %d=%d",max,sum);
getch();
    return 0;
}

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

Enter the maximum value: 10
Sum of all prime numbers between 1 to 10=17

 

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

int main()
{
    int i,j,max,count,sum=0;
    printf("Enter the maximum value ");
    scanf("%d",&max);
i=2;
    while(i<=max){
        count=1;
        j=2;
        while(j<=i/2){
            if(i%j==0){
                count=0;
                break;
            }
            j++;
        }
        if(count==1){
            sum+=i;
        }
        i++;
    }
    printf("sum of all prime numbers between 1 to %d=%d",max,sum);
    getch();
    return 0;
}

 

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

Enter the maximum value: 50
Sum of all prime numbers between 1 to 50=328

 

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

int main()
{
    int i,j,max,count,sum=0;
    printf("Enter the maximum value ");
    scanf("%d",&max);
i=2;
    do{
        count=1;
        j=2;
        do{
            if(i%j==0){
                count=0;
                break;
            }
            j++;
        }while(j<=i/2);
        if(count==1){
            sum+=i;
        }
        i++;
    }while(i<=max);
    printf("sum of all prime numbers between 1 to %d=%d",max,sum);
    getch();
    return 0;
}


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

Enter the maximum value: 100
Sum of all prime numbers between 1 to 100=1058

 

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

Related

Recent Posts

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes