C program to find sum of odd numbers from 1 to n

C program to find sum of odd numbers from 1 to n

C program to find sum of odd numbers from 1 to n

Write a C program to find sum of odd numbers from 1 to n.

To make this program we required loops and increment operator.

Code

#include <stdio.h>

int main()
{
    int i, n, sum=0;
    printf("Enter upper limit: ");
    scanf("%d", &n);
    for(i=1; i<=n; i+=2)
    {
        sum += i;
    }
    printf("%d", sum);
    return 0;
}


Output

Enter upper limit: 3
4
Load comments