w3resource

C Exercises: Sum of n even numbers starting from m


Compute sum of nnn even numbers starting from mmm

Write a C program that reads two integers m, n and computes the sum of n even numbers starting from m.

Sample Solution:

C Code:

#include <stdio.h>

int main () {
    int m, n, i, j, k, sum_even_nums = 0;

    // Prompt user for input
    printf("\nInput two integers (m, n):\n");

    // Read two integer values 'm' and 'n' from user
    scanf("%d %d", &m, &n);

    // Print a message indicating what the program will do
    printf("\nSum of %d even numbers starting from %d: ",n,m);

    // Loop to find and sum 'n' even numbers starting from 'm'
    for (k = 0, j = m; k < n; j++) {

        // Check if 'j' is even
        if (j % 2 == 0) {
            sum_even_nums += j; // Accumulate even numbers
            k++; // Increment the count of even numbers found
        }
    }

    // Print the sum of even numbers
    printf("\n%d", sum_even_nums);

    return 0; // End of program
}

Sample Output:

Input two integes (m, n):
20
60

Sum of 60 even numbers starting from 20:
4740

Flowchart:

C Programming Flowchart: Sum of n even numbers starting from m.


For more Practice: Solve these Related Problems:

  • Write a C program to calculate the sum of n consecutive even numbers starting from m using a for-loop.
  • Write a C program to generate the sequence of even numbers starting at m and compute their sum iteratively.
  • Write a C program to use a while-loop to accumulate the sum of n even numbers, starting from a user-defined m.
  • Write a C program to compute the sum of even numbers in a range starting from m using arithmetic progression formulas.

Go to:


PREV : Find and print all divisors of an integer.
NEXT : Compute sum of nnn odd numbers starting from mmm.

C programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.