How to resolve the algorithm Sum and product of an array step by step in the UNIX Shell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sum and product of an array step by step in the UNIX Shell programming language

Table of Contents

Problem Statement

Compute the sum and product of an array of integers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sum and product of an array step by step in the UNIX Shell programming language

Source code in the unix programming language

sum=0
prod=1
list="1 2 3"
for n in $list
do sum="$(($sum + $n))"; prod="$(($prod * $n))"
done
echo $sum $prod

sum=0
prod=1
for n
do sum="$(($sum + $n))"; prod="$(($prod * $n))"
done
echo $sum $prod

sum=0
prod=1
while read n
do sum="$(($sum + $n))"; prod="$(($prod * $n))"
done
echo $sum $prod

list=(20 20 2);
(( sum=0, prod=1 ))
for n in "${list[@]}"; do
   (( sum += n, prod *= n ))
done
printf '%d\t%d\n' "$sum" "$prod"

  

You may also check:How to resolve the algorithm RSA code step by step in the Ruby programming language
You may also check:How to resolve the algorithm General FizzBuzz step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Leap year step by step in the X86 Assembly programming language
You may also check:How to resolve the algorithm Validate International Securities Identification Number step by step in the langur programming language
You may also check:How to resolve the algorithm Pell's equation step by step in the Arturo programming language