Write a program to evalute the series.
1+3+5+7+........+n
#include <stdio.h>
int main() {
// we must calculte 1 + 3 + 5 + .... + n
// n is odd
int n, Sum = 0;
printf("n = ");
scanf("%d", &n);
if (n % 2 == 1) {
for (int i = 1; i <= n; i += 2) {
Sum += i;
}
}
else {
printf("Enter only odd number.");
}
printf("The value of 1 + 3 + 5 + ... + %d = %d", n, Sum);
return 0;
}
Comments
Leave a comment