what should be condition for while before adding while loops it prints even no upto 100 but I need to print sum of even numbers
#!/bin/bash
sum=0
for((n=2;n<=100;n=n 2))
do
echo $n
while [[$n 0]] # what should be condition for while loop
do
sum= `expr sum $n`
done
echo "sum is $sum "
done
CodePudding user response:
Does this count ^_* :
kent$ seq -s 2 2 100|bc
2550
CodePudding user response:
k=0;
for i in {1..100}; do
if [[ $(( i % 2 )) == 0 ]]; then
let k=k i
fi ; done
echo $k
Prints
2550
or this:
k=0; for i in {1..100}; do
if (( i % 2 == 0 )); then
(( k=k i )) ; fi ; done ; echo $k
CodePudding user response:
You shouldn't have the while
loop at all. You're already iterating with the for
loop.
#!/bin/bash
sum=0
for((n=2;n<=100;n=n 2))
do
echo $n
((sum =n))
done
echo "sum is $sum "