I
I
igordata12020-04-19 17:03:47
PHP
igordata1, 2020-04-19 17:03:47

How to make an algorithm. which to each digit of a number (eg 151)?

How to make an algorithm. which will add 1 to each digit of a number (for example, 151) (the result should be 262). As an additional option - do not use any kind of conversion to strings..?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
X
xmoonlight, 2020-04-19
@xmoonlight

X=262-151

Z
zax2002, 2020-04-20
@zax2002

<?php

$number1 = 567;
$number2 = 5;
$number3 = 367835;

function f($num) {
    return $num + floor(0.11111111111*(10**(floor(log10($num))+1)));
}

echo(f($number1)."\n".f($number2)."\n".f($number3));

/*
678
6
478946
*/

A
Alexander, 2020-04-19
Madzhugin @Suntechnic

And what about Bitrix?
If right on the forehead, then something like this:

$n = 151;
$m = 1;
if ($n > 9) {
  $i=0;
  $k=0;
  while ($k < $n) {
    $m = $k;
    $k = $k+10**$i;
    $i++;
  }
  
}
$n = $n+$m;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question