write a program to accept a natural number and display whether it is a one digit number or two digit number or three digit number or its contents four or more digit
Answers
# Iterative Python program to count
# number of digits in a number
def countDigit(n):
count = 0
while n != 0:
n //= 5
count+= 1
return count
# Driver Code
n = 346284
print ("Number of digits : % d"%(countDigit(n)))
➝ Number of digit = 4
____________________________________
For more than 4 :-
# Iterative Python program to count
# number of digits in a number
def countDigit(n):
count = 0
while n != 0:
n //= 4+n , (n= 1,2,3,4......,n )
count+= 1
return count
# Driver Code
n = 34628456555n
print ("Number of digits : % d"%(countDigit(n)))
➝ Number of digit = 4+n , (n= 1,2,3,4......,n )
_________________________________
Question:-
Write a program to accept a natural number and display whether it is a one digit number or two digit number or three digit number or its contents four or more digit.
Code:-
In Java....
class Number
{
public static void main(int n)
{
int c=0;
for(;n!=0;n/=10)
c++;
System.out.println("Number of digits: "+c);
}
}
ln Python...
n=int(input("Enter a number: "))
c=0
while n!=0:
c=c+1
n=n/10
print("Total number of digits: ",c)