CLS
INPUT "N-"; n$
INPUT "K-"; k
OPEN "1.txt" FOR OUTPUT AS #1
PRINT #1, n$
CLOSE #1
OPEN "1.txt" FOR INPUT AS #2
INPUT #2, p5
CLOSE #2
KILL "1.txt"
c = LEN(n$)
chislo = 0
FOR i = 1 TO c
p1 = 10 ^ (c - i)
p2 = FIX(p5 / p1)
p5 = p5 - (p2 * p1)
IF p2 / 2 = INT(p2 / 2) THEN p2 = k
p2 = p2 * p1
chislo = chislo + p2
NEXT i
PRINT "novoe chislo:"; chislo
Тестирование выполнено в программе QB64 ( Скачать )