C Program To Find Sum of Odd Numbers

In this post, we will learn how to find sum of odd numbers using the C Programming language.

Odd Numbers are the integers that always leave a remainder when divided by 2. For example: 3, 5, 15, 21, 47, . . . , and so on.

We will be calculating the sum of odd numbers using three different approaches.

1. By using For Loop
2. Without using If Statement
3. Sum of Odd Numbers in a Given Range

So, without further ado, letâ€™s begin this tutorial.

C Program To Find Sum of Odd Numbers

```// C Program To Find Sum of Odd Numbers
#include <stdio.h>
int main(){
int num, sum = 0;

printf("Enter the maximum value: ");
scanf("%d", &num);

printf("Odd Numbers Between 0 To %d are: \n", num);
for (int i = 1; i <= num; i++ ){
if (i % 2 != 0){
printf("%d\n", i);
sum = sum + i;
}
}
printf("The Sum of Odd Numbers From 0 To %d is %d.", num, sum);
return 0;
}
```

Output

``````Enter the maximum value: 22
Odd Numbers Between 0 To 22 are:
1
3
5
7
9
11
13
15
17
19
21
The Sum of Odd Numbers From 0 To 22 is 121.
``````

How Does This Program Work ?

``````    int num, sum = 0;
``````

In this program, we have declared two int data type variables named num and sum.

``````    // Asking for Input
printf("Enter the maximum value: ");
scanf("%d", &num);
``````

Then, the user is asked to enter the value of the maximum number. The value of the maximum number will get stored in the num named variable.

``````    for (int i = 1; i <= num; i++ ){
if (i % 2 != 0){
printf("%d\n", i);
sum = sum + i;
}
}

``````

We have used different iterations of i to check whether different values of i are divisible by 2 or not. If the condition is true, then that value of i is an odd number. We will compile and add the value of i to sum.

``````    printf("The Sum of Odd Numbers From 0 To %d is %d.", num, sum);
``````

Finally, the sum of odd numbers is printed to the screen using printf() function.

C Program To Find Sum of Odd Numbers Without Using If Statement

```// C Program To Find Sum of Odd Numbers
#include <stdio.h>
int main(){
int num, sum = 0;

printf("Enter the maximum value: ");
scanf("%d", &num);

printf("Odd Numbers Between 0 To %d are: \n", num);
for (int i = 1; i <= num; i = i + 2 ){
printf("%d\n", i);
sum = sum + i;
}
printf("The Sum of Odd Numbers From 0 To %d is %d", num, sum);
return 0;
}
```

Output

``````Enter the maximum value: 15
Odd Numbers Between 0 To 15 are:
1
3
5
7
9
11
13
15
The Sum of Odd Numbers From 0 To 15 is 64
``````

C Program To Find Sum of Odd Numbers in a Given Range

```// C Program To Find Sum of Odd Numbers in a Given Range
#include <stdio.h>
int main(){
int min, max, sum = 0;

printf("Enter the minimum value: ");
scanf("%d", &min);
printf("Enter the maximum value: ");
scanf("%d", &max);

printf("Odd Numbers From %d to %d are: \n", min, max);
for (int i = min; i <= max; i++){
if (i % 2 != 0){
printf("%d\n", i);
sum = sum + i;
}
}
printf("The Sum of Odd Numbers from %d to %d is %d.", min, max, sum);
return 0;
}
```

Output

``````Enter the minimum value: 5
Enter the maximum value: 15
Odd Numbers From 5 to 15 are:
5
7
9
11
13
15
The Sum of Odd Numbers from 5 to 15 is 60.
``````

Conclusion

I hope after going through this post, you understand how to find sum of odd numbers using C Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.

2 thoughts on “C Program To Find Sum of Odd Numbers”

1. // C Program To Find Sum of Odd Numbers
#include
int main(){
int num, sum = 0;

printf(“Enter the maximum value: “);
scanf(“%d”, &num);

printf(“Odd Numbers Between 0 To %d are: \n”, num);
for (int i = 1; i <= num; i++ ){
if (i % 2 != 0){
printf("%d\n", i);
sum = sum + i;
}
}
printf("The Sum of Odd Numbers From 0 To %d is %d.", num, sum);
return 0;
}

/* code is wrong */