The series 1+3+5+..+n, is a series of odd number. We need to write a program to display this series as well as find the sum upto nth number. The n will be inserted by the user. The program has been compiled and executed under DEV c++.
CODE:
#include void main() { int i,n,sum=1; printf("Enter any number n:"); scanf("%d",&n); printf("\n1 "); for(i=3;i<=n;i++){ printf("+ %d ",i); sum=sum+i; i++; } printf("\nSum is: %d", sum); }
OUTPUT:
Explanation: The i is incremented twice in the above program. First in the for loop itself and second inside the loop. There are multiple ways to do this. I choose to do it this way.