Count number of Digits of an Integer in C++
Advertisements
C++ Program to Find number of Digits from any number
Any number is a combination of digits, like 342 have three digits and 4325 have four digits. For calculating the digits of any number user have three possibilities to input values.
- Enter +ve numbers
- Enter -ve numbers
- Enter zero
This code is correct when user enter +ve numbers Only.
Example
#include<iostream.h>
#include<conio.h>
void main()
{
int no,a=0;
clrscr();
cout<<"Enter any num : ";
cin>>no;
while(no>0)
{
no=no/10;
a++;
}
cout<<"\n no. of digits in given number is: "<<a;
getch();
}
Output
Enter any num : 650 no. of digits in given number is : 3
This code is satisfy all the above three conditons.
C++ program to count number of digits of an integer
#include<iostream.h>
#include<conio.h>
void main()
{
int no,a=0;
clrscr();
cout<<"Enter any num : ";
cin>>no;
if(no<0)
{
no=no * -1;
}
else if(no==0)
{
no=1;
}
while(no>0)
{
no=no/10;
a++;
}
cout<<"\n no. of digits in given number is: "<<a;
getch();
}
Output
Enter any num : -650 no. of digits in given number is : 3
Google Advertisment
