Description
Take an integer as the input from the user and then calculate the number of digits in the given input and print it as the output.
Input
3241
Output
4
Input
6
Output
1
C Program
#include <stdio.h>
int main()
{
int num,count=0;
printf("enter the number: ");
scanf("%d",&num);
if(num==0)
printf("digit is 1");
else
{
while(num!=0)
{
num=num/10;
++count;
}
printf("Number of digits is : %d",count);
}
return 0;
}
C++ Program
#include <iostream>
using namespace std;
int main()
{
int num,count=0;
cout<<"enter the number: ";
cin>>num;
if(num==0)
cout<<"digit is 1";
else
{
while(num!=0)
{
num=num/10;
++count;
}
cout<<"Number of digits is :"<<count;
}
return 0;
}
Java Program
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an Integer: ");
int num = sc.nextInt();
int count = 0;
if(num==0)
System.out.print("Number of Digits = 1");
else{
while(num != 0)
{
count++;
num = num / 10;
}
System.out.print("Number of Digits is "+count);
}
}
}
Python
Method -1
num = int(input('Enter an integer :'))
count=0
if(num==0):
print("Number of digits is 1")
else:
while(num!=0):
num=num//10
count=count+1
print("Number of digits is ",count)
Method-2
num = int(input('Enter an integer :'))
String = str(num)
print(len(String))