Write a C program to find the sum of all even integers between 1 and n.
int main() {
int n, i, sum = 0;
printf("Enter the value for n:");
scanf("%d", &n);
/* calculate the sum of even nos between 1 and n */
for (i = 1; i < n; i++) {
/* skip the odd numbers */
if (i % 2 != 0)
continue;
sum = sum + i;
}
printf("Output: %d\n", sum);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the value for n:100
Output: 2450
Enter the value for n:100
Output: 2450
No comments:
Post a Comment