C Exercises: Compute the sum of consecutive odd numbers from a given pair of integers
C Basic Declarations and Expressions: Exercise-34 with Solution
Write a C program to compute the sum of consecutive odd numbers from a given pair of integers.
Sample Solution:
C Code:
#include <stdio.h>
int main() {
int x, y, i, total = 0; // Declare variables for user input and calculations
printf("\nInput a pair of numbers (for example 10,2):");
printf("\nInput first number of the pair: ");
scanf("%d", &x); // Read the first number
printf("\nInput second number of the pair: ");
scanf("%d", &y); // Read the second number
if (x < y) {
return 0; // If x is less than y, exit the program
}
printf("\nList of odd numbers: ");
for (i = y; i <= x; i++) {
if ((i % 2) != 0) {
printf("%d\n", i); // Print odd numbers within the range
total += i; // Add odd numbers to the total
}
}
printf("Sum=%d\n", total); // Print the total sum
return 0;
}
Sample Output:
Input a pair of numbers (for example 10,2): Input first number of the pair: 10 Input second number of the pair: 2 List of odd numbers: 3 5 7 9 Sum=24
Flowchart:

C programming Code Editor:
Previous: Write a C program that accepts some integers from the user and find the highest value and the input position.
Next: Write a C program to check if two numbers in a pair is in ascending order or descending order.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join