You are currently viewing Sum Of Prime Numbers Between 1 to n using While Loop C Program

Sum Of Prime Numbers Between 1 to n using While Loop C Program

In this tutorial, you will learn Writing C Program to Find the sum of all prime numbers between 1 to n using a while loop conditional statement. We will also see the algorithm, examples, and programming explanations for your better understanding.

Required Knowledge

Problem Statement

We have to Write a C program that will take the number n as input. And after taking the input Find the all prime numbers between 1 and n using a while loop and then sum them. For a better understanding, you can refer to the below example which will help you to understand what you have to achieve.

Example

Input:
Enter the value of n: 10

Output:
The sum of prime numbers between 1 and 10 is: 17

In the above example, you can see the input is 10. The prime number between 1 to 10 are 2, 3, 5, 7, and the sum of these prime numbers are 17.

Algorithm

  1. Start the program.
  2. Declare the variables n, num, isPrime, and sum.
  3. Read the value of n from the user.
  4. Initialize num to 2 and sum to 0.
  5. Enter the outer while loop with the condition num <= n.
    • Initialize isPrime to 1 (true).
    • Declare the variable divisor and initialize it to 2.
    • Enter the inner while loop with the condition divisor <= sqrt(num).
      • Check if num is divisible by divisor without a remainder.
        • If true, set isPrime to 0 (false) and break out of the loop.
        • If false, continue to the next iteration.
      • Increment divisor by 1.
    • Exit the inner while loop.
    • Check the value of isPrime.
      • If isPrime is 1, add num to sum.
      • If isPrime is 0, continue to the next iteration.
    • Increment num by 1.
  6. Exit the outer while loop.
  7. Print the sum of prime numbers as: “The sum of prime numbers between 1 and n is: sum”.
  8. End the program

Program to find sum of all prime numbers between 1 to n

#include <stdio.h>
#include <math.h>
int main() {
    int n, num = 2, isPrime, sum = 0;
    printf("Enter the value of n: ");
    scanf("%d", &n);
    while (num <= n) {
        isPrime = 1;
        int divisor = 2;
        while (divisor <= sqrt(num)) {
            if (num % divisor == 0) {
                isPrime = 0;
                break;
            }
            divisor++;
        }
        if (isPrime == 1) {
            sum += num;
        }
        num++;
    }
    printf("The sum of prime numbers between 1 and %d is: %d\n", n, sum);
    return 0;
}

Output

Enter the value of n: 12
The sum of prime numbers between 1 and 12 is: 28

Program Explanation

  • The program takes input for the value of n from the user.
  • It initializes a variable num to 2, which represents the first number to be checked for primality.
  • It initializes a variable sum to 0, which will hold the running sum of prime numbers.
  • The program enters a while loop with the condition num <= n to iterate through numbers from 2 to n.
  • Inside the while loop, it checks if num is a prime number.
  • It initializes a variable isPrime to 1, which represents the initial assumption that num is prime.
  • It initializes a variable divisor to 2, which will be used to check divisibility.
  • It enters another while loop with the condition divisor <= num/2 to check if num is divisible by any number other than 1 and itself.
  • Inside the inner while loop, it checks if num is divisible by the current value of divisor.
  • If num is divisible, it sets the value of isPrime to 0 and breaks out of the inner while loop.
  • It then increments the value of divisor by 1 to move on to the next possible divisor.
  • The inner while loop continues until divisor exceeds half of num.
  • After the inner while loop, it checks the value of isPrime.
  • If isPrime is 1, it adds the value of num to sum.
  • It then increments the value of num by 1 to move on to the next number.
  • The outer while loop continues until num exceeds the value of n.
  • Once the while loop is complete, the program prints the value of sum, which represents the sum of all prime numbers between 1 and n.
  • The program ends.

Conclusion

The C program successfully finds the sum of all prime numbers between 1 and n using a while loop. By iterating through the numbers and checking each number for primality using a nested while loop, it accumulates the prime numbers and calculates their sum. The program then displays the sum on the screen.