Sum first and last digit of a number and make the product of remaining digits | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Sum first and last digit of a number and make the product of remaining digits

Guys, some person called me looking for some help. The question is: Make a program that make the sum of the first number and the last number, and the product of remaining numbers, for example: Number: 1512 Sum: 1+2=3 Product=5*1=5 The only way I found was convert the number to an array. But that person wanted this solution without array, using only the number. It is possible ?

10th Oct 2017, 12:05 PM
Luiz Augusto Ventura
Luiz Augusto Ventura - avatar
2 Answers
+ 4
Solution for numbers of 4 digits https://code.sololearn.com/cQFf11P2bQfS/#cpp You can extend it to n digits by looping i*=10 until you hit n == n%i Added for the n case, https://code.sololearn.com/c0G3kd3ZCw2R/#cpp Special cases: one digit: sum = digit and product = 0 two digits: sum = digit + digit and product = 0
10th Oct 2017, 1:56 PM
nevu
nevu - avatar
0
Very good man. But I don't understood how to extend it.
10th Oct 2017, 1:25 PM
Luiz Augusto Ventura
Luiz Augusto Ventura - avatar