Input a number and count digits using a function in C++
Function - Question 1
In this question, we will see how to input a number and count how many digits are there in it in C++ programming using a function. To know more about function click on the function lesson.
Q1) Write a program in C++ to input a number and count how many digits are there in it using a function.
Program
#include <iostream>
#include <conio.h>
using namespace std;
int countdigit(int num)
{
int dc=0;
while(num>0)
{
dc=dc+1;
num=num/10;
}
return dc;
}
int main()
{
int n;
cout<<"Enter a number ";
cin>>n;
cout<<"Total Number of Digits = "<<countdigit(n);
return 0;
}
Output
Enter a number 42863 Total Number of Digits = 5