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

Previous Program Next Program

Write a C program to print the sum of all odd numbers from 1 to n using for loop. How to find sum of all odd numbers in a given range in C programming. Logic to find sum of odd numbers in a given range in C program.

Example

Input

Input upper limit: 10

Output

Sum of odd numbers from 1-10: 25

Required knowledge

Basic C programming, For loop

Logic to find sum of odd numbers from 1 to n

Logic of this program isn't complex to implement. Once you are done with the below exercise you can easily derive the logic for this.

OK, let us now focus on to the main logic section. Below is the step by step descriptive logic to find sum of first n odd numbers.

  1. Read upper limit to find sum of odd numbers from user. Store it in some variable say N.
  2. Initialize some other variable that will hold the sum. Say sum = 0.
  3. Run a loop from 1 to N, increment 1 in each iteration. The loop structure must look similar to for(i=1; i<=N; i++).
  4. Inside the loop add the sum with the current value of i. Store the result back in sum. Which is sum = sum + i.
  5. Print the final value of sum.

Program to find sum of odd numbers from 1 to n

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

#include <stdio.h>

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

    /* Read 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;
} 

Note: sum += i and sum = sum + i both are same. You can use any of them as per your comfort.

That was easy, now using this you can also find the sum of odd numbers in a given range. For finding sum of odd numbers in a range M - N. You need two input from user specifying the lower and upper limit. Let us write now a program that sums all odd number in a given range.

Program to find sum of odd numbers in given range

/**
 * C program to print the sum of all odd numbers in given range
 */

#include <stdio.h>

int main()
{
    int i, start, end, sum=0;

    /* Reads range to find sum of odd numbers */
    printf("Enter lower limit: ");
    scanf("%d", &start);
    printf("Enter upper limit: ");
    scanf("%d", &end);

    /* If lower limit is even then make it odd */
    if(start % 2 == 0)
    {
        start++;
    }
    
    /* Iterate from start to end and find sum */
    for(i=start; i<=end; i++)
    {
        sum += i;
    }

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

    return 0;
}
Output
Enter lower limit: 4
Enter upper limit: 11
Sum of odd numbers (4-11) = 32

Happy coding ;)

You may also like

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment