Topper Copies

Write an algorithm to add two numbers in the octal system.

Aryan Yadav, AIR 31, 2025

Addition :

1. Input $x$ (2 nos to add) 2. Input $y$ 3. $z=0$, $w=0$, $v=0$, $i=$ $[?]$ $-1$ 4. While ($x>0$ or $y>0$): 5. $z = z + w\times 10^{i+1}$; $i = i+1$ 6. $v =$ $[?]$ $x\%10 + y\%10$ 7. if ( $v \ge 8$ ): 8. $w = 1$ 9. $z = z+(v-8)\times 10^i$ 10. else : 11. $z = z + v \times 10^i$ 12. $w = 0$ 13. $x = x/10$ 14. $y = y/10$ 15. if ($w>0$): 16. $z = z + w\times 10^{i+1}$ 17. Print ( $z$ )

Logic $\Rightarrow$ Assume we add $137 + 24$

[137 and 24 written side by side, $x$ labelled under 137 and $y$ under 24]

1st loop: $z=0$, $i=0$, $v = 7+4 = 11$

Since ($v\ge 8$) $\Rightarrow$ $w=1$, $z = 0+(11-8)\times 10^0 = 3$

Now $x = 13$, $y = 2$ ($x/10$) ($y/10$)

2nd loop: $z = 3 + 1\times 10 = 13$, $i = 1$

$u = 3+2 = 5$

$u < 8 \Rightarrow z = 13+50 = 63$, $w=0$

Now $x = 1$, $y = 0$

3rd loop: $z = 63+0\times 100 = 0$, $i = 2$

$u = 1+0 = 1$

$u < 8 \Rightarrow z = 63+1\times 100 = 163$

Now $x = 0$, $y = 0 \Rightarrow$ loop ends

$\Rightarrow z = 163$ (Ans)