C Program to Sum Even numbers in range of 1 to n

In this article, I will show you how you can write a C Program to Sum Even numbers in range of 1 to n. I will explain the algorithm, and concepts and then help you to write C Program to Sum Even numbers in range of 1 to n.

1. Program Outline

In this program, you’ll have to take two numbers as input from the user and then calculate the Sum Even numbers in the range of 1 to n and display them in the terminal.

2. Concepts

To write this C program to Sum Even numbers in the range of 1 to n, you need to learn the following concepts of C Programming:

  1. Input
  2. Variables
  3. while Loop
  4. If Else

3. Algorithm

To write this C program, follow this Algorithm:

Step 1: START
Step 2: Input value of n
Step 3: Run Loop until n>=0
Step 4: Check n for Even number
Step 5: If true add n to sum
Step 6: decrease value of n by 1
Step 7: Output value of sum
Step 8: STOP

4. Program

Here’s the C Program to Sum Even numbers in the range of 1 to n:

#include<stdio.h>
 
int main()
{
 
int n, sum=0;
printf("Enter the value of n to print sum of even numbers between 1 and n: ");
scanf("%d", &n);
 
    while(n>=1) /* identifying even number */
    {
        if (n%2==0)
        {
            sum += n;
         }
        n -= 1;
    }
    printf("Sum of even numbers is %d", sum);
 
}

5. Output

Here’s the output of the above C Program:

C Program to Sum Even numbers in range of 1 to n
Default image
groot

Leave a Reply

Your email address will not be published.