Program to Print First k Digits of 1N where N is a Positive Integer

Python Program to Print First k Digits of 1/N where N is a Positive Integer

Given the numbers N and K, the task is to print the first K Digits of 1/N in Python.

Examples:

Example1:

Input:

Given numb=9
Given k=3

Output:

The first [3] digits in the given number [ 9 ] is :111

Example2:

Input:

Given numb=1
Given k=7

Output:

The first [7] digits in the given number [ 1 ] is :00000001

Program to Print First k Digits of 1/N where N is a Positive Integer in Python

Below are the ways to print the first K Digits of 1/N in Python.

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Algorithm:

Initially, enter the user’s n-th number. Then enter the number k, which represents the number of digits. You will now know how to develop the code so that it writes the first k digits to the output after receiving inputs. So we use to divide and multiply in the code. We divide 1 by n and get a floating number less than 1. Then we multiply k by 10 and then multiply by that floating number before converting the complete result to an integer and printing that integer to get the first k digits of 1/n.

If n is equal to 1, this will not work since 1/1 is no longer a floating number, and we are not going to convert y into an integer because it is already an integer, but we are going to convert it into a string and reverse that string. That string should be printed.

Method #1: Using If Statement (Static Input)

Approach:

  • GIve the numbers n and k as static input and store them in two separate variables.
  • Calculate the value of 1/n and store it in a variable say divval.
  • Calculate the value of 10 power k using the ** operator and store it in a variable say powerval.
  • Calculate the integer value of the product of divval and powerval and store it in another variable say resval.
  • Check if the value of n is equal to 1 or not using the If conditional Statement.
  • Convert the resval to string using the str() function and store it in the same variable.
  • Print the resval in reverse order using slicing.
  • If the “IF” conditional statement is false then print the value of resval.
  • The Exit of the Program.

Below is the implementation:

# GIve the numbers n and k as static input and store them in two separate variables.
numb = 9
k = 3
# Calculate the value of 1/n and store it in a variable say divval.
divval = 1/numb
# Calculate the value of 10 power k using the ** operator
# and store it in a variable say powerval.
powerval = 10**k
# Calculate the integer value of the product of divval and powerval
# and store it in another variable say resval.
resval = int(divval*powerval)
# Check if the value of n is equal to 1 or not
# using the If conditional Statement.
if(numb == 1):
        # Convert the resval to string using the str() function
    # and store it in the same variable.
    resval = str(resval)
    # Print the resval in reverse order using slicing.
    print("The first ["+str(k)+"] digits in the given number [ " +
          str(numb)+" ] is :"+str(resval[::-1]))
# If the "IF" conditional statement is false then print the value of resval.
else:
    print("The first ["+str(k)+"] digits in the given number [ " +
          str(numb)+" ] is :"+str(resval))

Output:

The first [3] digits in the given number [ 9 ] is :111

Method #2: Using If Statement (User Input)

Approach:

  • GIve the numbers n and k as user input using map(),int(),split() functions.
  • Store them in two separate variables.
  • Calculate the value of 1/n and store it in a variable say divval.
  • Calculate the value of 10 power k using the ** operator and store it in a variable say powerval.
  • Calculate the integer value of the product of divval and powerval and store it in another variable say resval.
  • Check if the value of n is equal to 1 or not using the If conditional Statement.
  • Convert the resval to string using the str() function and store it in the same variable.
  • Print the resval in reverse order using slicing.
  • If the “IF” conditional statement is false then print the value of resval.
  • The Exit of the Program.

Below is the implementation:

# GIve the numbers n and k as user input using map(),int(),split() functions.
# Store them in two separate variables.
numb, k = map(int, input(
    'Enter some random values of numb and k separated by spaces = ').split())
# Calculate the value of 1/n and store it in a variable say divval.
divval = 1/numb
# Calculate the value of 10 power k using the ** operator
# and store it in a variable say powerval.
powerval = 10**k
# Calculate the integer value of the product of divval and powerval
# and store it in another variable say resval.
resval = int(divval*powerval)
# Check if the value of n is equal to 1 or not
# using the If conditional Statement.
if(numb == 1):
        # Convert the resval to string using the str() function
    # and store it in the same variable.
    resval = str(resval)
    # Print the resval in reverse order using slicing.
    print("The first ["+str(k)+"] digits in the given number [ " +
          str(numb)+" ] is :"+str(resval[::-1]))
# If the "IF" conditional statement is false then print the value of resval.
else:
    print("The first ["+str(k)+"] digits in the given number [ " +
          str(numb)+" ] is :"+str(resval))

Output:

Enter some random values of numb and k separated by spaces = 1 7
The first [7] digits in the given number [ 1 ] is :00000001

Related Programs: