write a program to calculate the no.of digits in a given number.
write a program to calculate the no.of digits in a given number.
Answer: Python program that calculates the number of digits in a given number:
num = int(input("Enter a number: ")) # get input from user
count = 0 # initialize count variable
while num != 0:
num //= 10 # integer division by 10 to remove the last digit
count += 1 # increment the count variable
print("Number of digits in the given number:", count)'
In this program, we first get the input number from the user using the input() function and convert it to an integer using the int() function. We then initialize a variable count to 0 to keep track of the number of digits.
We then enter a while loop that will keep running as long as num is not equal to 0. In each iteration of the loop, we use integer division (//) by 10 to remove the last digit of num. We then increment the count variable by 1 to keep track of the number of digits we have removed.
Finally, when the while loop terminates, we print the value of count, which gives us the number of digits in the original number.