-
-
Save Harishhona07/c8fb6192035c710294addb279cd14311 to your computer and use it in GitHub Desktop.
C program to display the sum of even and odd numbers from 1 to 50.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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