In this C program, we will find sum of all even numbers between 1 to N using for loop.
Required Knowledge
C program to find sum of all even numbers between 1 to N using for loop
#include <stdio.h> int main() { int counter, N, sum = 0; printf("Enter a Positive Number\n"); scanf("%d", &N); for(counter = 1; counter <= N; counter++) { /* Even numbers are divisible by 2 */ if(counter%2 == 0) { /* counter is even, add it to sum */ sum = sum + counter; } } printf("Even numbers sum between 1 to %d is %d", N, sum); return 0; }Output
Enter a Positive Number 9 Even numbers sum between 1 to 9 is 20
Related Topics