C Program To Find Sum of All Odd Numbers Between Two Integers, using While loop


Lets write a C program to find sum of all odd numbers between range or between 2 integers input by the user.

Odd Number: An odd number is an integer that is not exactly divisible by 2.

For Example: 13 % 2 != 0. When we divide 13 by 2, it does not give a reminder of 0. So number 13 is an odd number.

Note: In this C program we ask the user to input start and end value. We assume that the user enters bigger value for variable end and smaller value for variable start. i.e., start < end

If user enters start = 10 and end = 20. C program finds all the odd numbers between 10 and 20, including 10 and 20. So the odd numbers are 11, 13, 15, 17, 19. We add all these odd numbers and output the sum to the console window. i.e., 11 + 13 + 15 + 17 + 19 = 75. We out put the value 75 as result.

Related Read:
Decision Control Instruction In C: IF
while loop in C programming
Even or Odd Number: C Program
C Program to Generate Odd Numbers Between Two Integers
C Program To Find Sum of All Odd Numbers From 1 To N, using While loop

Video Tutorial: C Program To Find Sum of All Odd Numbers Between Range, using While loop


[youtube https://www.youtube.com/watch?v=B_jGvvCh7Ho]

YouTube Link: https://www.youtube.com/watch?v=B_jGvvCh7Ho [Watch the Video In Full Screen.]

Logic To Find Sum of All Odd Numbers Between Two Integers, using While loop

Step 1: We ask the user to enter start and end value.

Step 2: We iterate through the while loop until value of start is less than or equal to value of variable end. Inside while loop we keep incrementing the value of variable start by one for each iteration.

Step 3: For every iteration we check if value present in variable start is an odd number. i.e., start % 2 != 0. If this condition is true, then we add the value present in variable start to the previous value of variable sum.

Step 4: Once the control exits while loop, we print the value present in variable sum – which has the sum of all the odd numbers between the range entered by the user.

Source Code: C Program To Find Sum of All Odd Numbers Between Two Integers, using While loop

#include<stdio.h>

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

    printf("Enter start and end value\n");
    scanf("%d%d", &start, &end);

    printf("\nSum of odd numbers from %d to %d is ", start, end);
    while(start <= end)
    {
        if(start % 2 != 0)
        {
            sum = sum + start;
        }
        start++;
    }
    printf("%d\n", sum);

    return 0;
}

Output 1:
Enter start and end value
10
20

Sum of odd numbers from 10 to 20 is 75

Output 2:
Enter start and end value
25
50

Sum of odd numbers from 25 to 50 is 481

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *