-1

I have list of numbers, and I want to multiply the digits within each number with each other; e.g. for the number 1234 it is 1 X 2 X 3 X 4 = 24

E.g. the following input

7675342567
098765342567
1234567890
0987654
234567
8765678
98
0999
09876543
345678
876543
87654

needs the following result:

7408800
0
0
0
5040
564480
72
0
0
20160
20160
6720

How should I proceed?

6
  • 2
    Sorry but this is not a scripting service. If you do not know any shell scripting, you'd start learning.
    – andcoz
    Commented Oct 8, 2018 at 15:39
  • 6
    @andcoz i don't like programming and it is not my hobby, I have problem and I need help what is wrong with that?
    – Sydney
    Commented Oct 8, 2018 at 15:40
  • 7
    Sydney: The problem is that this site teaches people programming. If you don't care about the "how", pay someone to solve the problem for you.
    – choroba
    Commented Oct 8, 2018 at 15:50
  • 5
    "what is wrong with that?" is a complex question, please read this: How do you feel about “GIVE ME TEH CODEZ” questions?
    – andcoz
    Commented Oct 8, 2018 at 15:52
  • Homework: policy proposal Commented Oct 8, 2018 at 19:11

2 Answers 2

6

You can do it this way:

<file sed 's/./&*/g;s/*$//' | bc

7408800
0
0
0
5040
564480
72
0
0
20160
20160
6720

With GNU sed, that can be simplified as:

<file sed 's/./*&/2g' | bc
0
1

Strictly within bash, assuming you don't overflow beyond 9 quintillion and change (9,223,372,036,854,775,807),

while IFS= read -r 
do 
  res=1
  for((i=0;i<${#REPLY};i++))
  do 
    res=$((res * ${REPLY:i:1}))
  done
  echo "$res"
done < input > output

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .