Skip to content

Instantly share code, notes, and snippets.

@Harishhona07
Created April 9, 2023 12:43
C program to display the sum of even and odd numbers from 1 to 50.
#include <stdio.h>
int main() {
int i, sum_even = 0, sum_odd = 0;
// loop through numbers 1 to 50
for (i = 1; i <= 50; i++) {
if (i % 2 == 0) {
// if number is even, add to sum_even
sum_even += i;
} else {
// if number is odd, add to sum_odd
sum_odd += i;
}
}
// display the results
printf("Sum of even numbers from 1 to 50: %d\n", sum_even);
printf("Sum of odd numbers from 1 to 50: %d\n", sum_odd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment