Loading…
OS · Loops & numbers · Program 12 · In the record
Write a shell script to print the sum of odd and even numbers from 1 to 50
1#!/bin/bash2 3odd=04even=05 6for (( i = 1; i <= 50; i++ ))7do8 if (( i % 2 == 0 ))9 then10 even=$((even + i))11 else12 odd=$((odd + i))13 fi14done15 16echo "Sum of odd numbers from 1 to 50 = $odd"17echo "Sum of even numbers from 1 to 50 = $even"Sum of odd numbers from 1 to 50 = 625 Sum of even numbers from 1 to 50 = 650
Run program 12 in your browser
bash · just-bash. Change the code or the input, run it, break it, and see what happens.