Answers for "find sum of all odd numbers from 1 to n using for loop"

C
3

find sum of all odd numbers from 1 to n using for loop

/**
 * C program to print the sum of all odd numbers from 1 to n
 */

#include <stdio.h>

int main()
{
    int i, n, sum=0;

    /* Input range to find sum of odd numbers */
    printf("Enter upper limit: ");
    scanf("%d", &n);

    /* Find the sum of all odd number */
    for(i=1; i<=n; i+=2)
    {
        sum += i;
    }

    printf("Sum of odd numbers = %d", sum);

    return 0;
}
Posted by: Guest on April-07-2022

Code answers related to "find sum of all odd numbers from 1 to n using for loop"

Code answers related to "C"

Browse Popular Code Answers by Language