Recursive def dec2Oct(dec_num): if(dec_num > 0): dec2Oct((int)(dec_num/8)) print(dec_num%8, end='') dec_num = 1 print(end='') dec2Oct(dec_num) watch the code in action on pythontutor.com while loop def dec2octal(num): octalNum =[] octalNum.append(num % 8) i = 0 while (num != 0): octalNum[i] = num % 8 num = int(num / 8) i += 1 for j in range(i - 1, -1, -1): print(octalNum[j], end="") #dec2octal(1) Algorithm:
If the given decimal number is 16. Step 1: Remainder when 16 is divided by 8 is 0. Therefore, arr[0] = 0. Step 2: Divide 16 by 8. New number is 16/8 = 2. Step 3: Remainder, when 2 is divided by 8, is 2. Therefore, arr[1] = 2. Step 4: Divide 2 by 8. New number is 2/8 = 0. Step 5: Since number becomes = 0. Stop repeating steps and print the array in reverse order. Therefore, the equivalent octal number is 20. watch the code in action on Pythontutor.com Question on stackoverflow |
Comunication is the art of combine information
Imagination is the mental capacity to picture things.
Categories
All
Hall of FameJohn Conway
Post Archives
May 2024
if you ain´t new ideas then you need learn new stuff.
XPRICEShot trends |