Bash Script to Find Sum of Odd Numbers
Use a Bash loop with an
if condition to check odd numbers and add them to a sum variable, like: sum=0; for i in {1..N}; do if (( i % 2 != 0 )); then sum=$((sum + i)); fi; done; echo $sum.Examples
Input5
Output9
Input10
Output25
Input1
Output1
How to Think About It
To find the sum of odd numbers up to a limit, think of counting from 1 to that number. For each number, check if it is odd by using the remainder operator
%. If the remainder when divided by 2 is not zero, add it to a running total. Finally, print the total sum.Algorithm
1
Get the input number N from the user2
Initialize a sum variable to 03
Loop from 1 to N4
Check if the current number is odd using the remainder operator5
If odd, add it to sum6
After the loop ends, print the sumCode
bash
read -p "Enter a number: " N sum=0 for (( i=1; i<=N; i++ )) do if (( i % 2 != 0 )) then sum=$((sum + i)) fi done echo "$sum"
Output
9
Dry Run
Let's trace input 5 through the code
1
Initialize sum
sum=0
2
Loop i=1
1 % 2 != 0 is true, sum=0+1=1
3
Loop i=2
2 % 2 != 0 is false, sum=1
4
Loop i=3
3 % 2 != 0 is true, sum=1+3=4
5
Loop i=4
4 % 2 != 0 is false, sum=4
6
Loop i=5
5 % 2 != 0 is true, sum=4+5=9
| i | Is Odd? | Sum |
|---|---|---|
| 1 | Yes | 1 |
| 2 | No | 1 |
| 3 | Yes | 4 |
| 4 | No | 4 |
| 5 | Yes | 9 |
Why This Works
Step 1: Check odd numbers
The code uses i % 2 != 0 to find odd numbers because odd numbers leave a remainder of 1 when divided by 2.
Step 2: Add to sum
When a number is odd, it is added to the sum variable to keep a running total.
Step 3: Print result
After checking all numbers, the script prints the total sum of all odd numbers found.
Alternative Approaches
Using while loop
bash
read -p "Enter a number: " N sum=0 i=1 while (( i <= N )) do if (( i % 2 != 0 )) then sum=$((sum + i)) fi ((i++)) done echo "$sum"
This uses a while loop instead of for loop; functionally the same but some prefer while for clarity.
Using arithmetic progression formula
bash
read -p "Enter a number: " N max_odd=$(( N % 2 == 0 ? N - 1 : N )) count=$(((max_odd + 1) / 2)) sum=$(( count * count )) echo "$sum"
This uses the formula for sum of first k odd numbers (k^2) for faster calculation without looping.
Complexity: O(n) time, O(1) space
Time Complexity
The script loops through all numbers from 1 to N once, so it runs in linear time O(n).
Space Complexity
Only a few variables are used regardless of input size, so space complexity is O(1).
Which Approach is Fastest?
The arithmetic formula approach is fastest with O(1) time, but looping is simpler and more intuitive for beginners.
| Approach | Time | Space | Best For |
|---|---|---|---|
| For loop with condition | O(n) | O(1) | Simple and clear for beginners |
| While loop with condition | O(n) | O(1) | Alternative loop style |
| Arithmetic formula | O(1) | O(1) | Fastest for large inputs, less intuitive |
Use
(( i % 2 != 0 )) to easily check if a number is odd in Bash.Beginners often forget to initialize the sum variable to zero before adding numbers.