Saturday, November 16, 2024
Google search engine
HomeLanguagesPython program to print the octal value of the numbers from 1...

Python program to print the octal value of the numbers from 1 to N

Given a number N, the task is to write a Python program to print the octal value of the numbers from 1 to N.

Examples:

        

Approach:

  • We will take the value of N as input.
  • Then, we will run the for loop from 1 to N+1 and traverse each ā€œiā€ through oct() function.
  • Print each octal value.

Note: The oct() function is one of the built-in methods in Python3. The oct() method takes an integer and returns its octal representation in a string format.Ā 

Below are the implementations based on the above approach:

Python3




# Python program to print the octal value of the
# numbers from 1 to N
Ā 
# Function to find the octal value of the numbers
# in the range 1 to N
def octal_in_range(n):
Ā 
Ā Ā Ā Ā # For loop traversing from 1 to N (Both Inclusive)
Ā Ā Ā Ā for i in range(1, n+1):
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # Printing octal value of i
Ā Ā Ā Ā Ā Ā Ā Ā print(oct(i)[2:])
Ā 
Ā 
# Calling the function with input 3
print("Input: 3")
octal_in_range(3)
Ā 
# Calling the function with input 11
print("Input: 11")
octal_in_range(11)


Output

Input: 3
1
2
3
Input: 11
1
2
3
4
5
6
7
10
11
12
13

Time complexity: O(n), where n is the input argument to the function. The time complexity is linear as the for loop traverses the range from 1 to N.

Space complexity: O(1), as the function only uses a constant amount of extra space to store the temporary variables i and the octal representation of i.

Approach: Decimal to Octal Conversion using repeated division by 8

  1. Define a function named decimal_to_octal that takes a decimal number as an argument and converts it to its octal representation using repeated division by 8 and concatenating the remainders.
  2. In the decimal_to_octal function, initialize a variable octal_num to 0 and a variable i to 1.
  3. Using a while loop, repeatedly divide the decimal number by 8 and compute the remainder.
  4. Divide the decimal number by 8 using the floor division operator // and update the value of the decimal number.
  5. Multiply the remainder by i and add it to the octal_num.
  6. Multiply i by 10 to shift the position of the next digit to be added to the left.
  7. After the loop finishes, return the octal_num as the octal representation of the original decimal number.
  8. In the main program, take user input for a number n.
  9. Use a for loop to iterate over the numbers from 1 to n inclusive.
  10. For each number i in the loop, call the decimal_to_octal function on i and print the resulting octal value.
  11. The final output will be the octal representation of all the numbers from 1 to n.

Python3




# Function to convert decimal to octal
def decimal_to_octal(n):
Ā Ā Ā Ā octal_num = 0
Ā Ā Ā Ā i = 1
Ā Ā Ā Ā while (n != 0):
Ā Ā Ā Ā Ā Ā Ā Ā rem = n % 8
Ā Ā Ā Ā Ā Ā Ā Ā n //= 8
Ā Ā Ā Ā Ā Ā Ā Ā octal_num += rem * i
Ā Ā Ā Ā Ā Ā Ā Ā i *= 10
Ā Ā Ā Ā return octal_num
Ā 
# Main program
n = 11
for i in range(1, n+1):
Ā Ā Ā Ā print(decimal_to_octal(i))


Output

1
2
3
4
5
6
7
10
11
12
13

The time complexity of this program is O(n log n), where n is the input number, The auxiliary space used by this program is O(1).

List Comprehension with Join method

The idea is to create a list comprehension that generates octal values for each number from 1 to N and print the octal values on separate lines using the join() method.

Steps:

  1. Initialize a variable N to the user input.
  2. Generate a list of octal values using a list comprehension that iterates over the range from 1 to N and applies the built-in oct() function to each number.
  3. Join the octal values using the join() method and print them on separate lines.

Python3




# Python program for the above approach
Ā 
# Driver Code
N = 11
Ā 
# generate a list of octal values
# using list comprehension
octal_values = [oct(i)[2:] for i in range(1, N+1)]
Ā 
# print octal values on separate lines
print('\n'.join(octal_values))


Output

1
2
3
4
5
6
7
10
11
12
13

Time Complexity: O(N)
Auxiliary Space: O(N), used to store the list of octal values

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?