Question & Answer: Write a C program that sums the odd integers between 1 and 500 using a for loop……

1. Write a C program that s ums the odd integers between 1 and 500 using a for loop.

Write a C program that sums the odd integers between 1 and 500 using a for loop.

Expert Answer

 

Hi,

Please find the solution for the above question.

Program:

#include <stdio.h>

int main() {
int counter, sum = 0;

for(counter = 1; counter <= 500; counter++) {
/* Odd numbers are not divisible by 2 */
if(counter%2 == 1) {
/* counter is odd, add it to sum */
sum = sum + counter;
}
}
printf(“Sum of all Odd numbers between 1 to 500 is %d”, sum);

return 0;
}

Output:

Sum of all Odd numbers between 1 to 500 is 62500Question & Answer: Write a C program that sums the odd integers between 1 and 500 using a for loop...... 1

Still stressed from student homework?
Get quality assistance from academic writers!