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
- Python Program to Find Whether a Number is a Power of Two
- Python Program to Check whether kth Bit is Set or not
- Python Program to Print Binary Representation of a Number
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:
- python program to print sum of negative numbers positive even numbers and positive odd numbers in a list
- python program to find all numbers in a range which are perfect squares and sum of all digits in the number is less than 10
- python program to print a unique pyramid pattern of digits
- python program to print a string n number of times
- python program to accept a sentence and print only the first letter of each word in capital letters separated by a full stop
- python program to count the number of digits present in a number
- python program to print binary representation of a number