Hi,
wt is the function that can use to count number of digits in a long integer?
e.g. 12345678 -> 8
11 ->2
456 -> 3
6724 -> 4
You'll need to use the modulo (or remainder) operator: %
It does what it says: cout << (123 % 10);
Hope this helps.
Hi,
wt is the function that can use to count number of digits in a long integer?
e.g. 12345678 -> 8
11 ->2
456 -> 3
6724 -> 4
I don't know that there is a function that does that. You can do two things though. You can use the log10 function from cmath:
http://www.cplusplus.com/reference/clibrary/cmath/log10.html
That function takes and returns doubles, so you'll need to do some typecasting and adjust by one.
Or you can set up a for-loop and divide by 10 each time through the loop using integer division. Count how many times you go through the loop.
or....
int getNumDecimalDigits(int value)
{
char myString[16];
sprintf(myString,"%d",value);
return strlen(myString);
}
:P
.
Hi,
wt is the function that can use to count number of digits in a long integer?
e.g. 12345678 -> 8
11 ->2
456 -> 3
6724 -> 4
Hey Saviniki try this code:
#include<stdio.h>
void main()
{
long int a;
int count=0,;
printf("Enter a interger :");
scanf("%d",&a);
while(a>0)
{
a=a/10;
count++;
}
printf("No. of digits in the no entered : %d",count);
}
@Deepak
Do not give away the solution. Read the last point of this link http://www.daniweb.com/forums/thread78223.html
Also posting C code in C++ forum is not a good idea that too without code tags.
int getNumDecimalDigits(int value)
{
char myString[16];
sprintf(myString,"%d",value);
return strlen(myString);
}
That is not the best way to do it, heres a better way:
// Will work with negative numbers
int CountDidgets(int val) {
int d = 1, c;
if (val >= 0) for (c = 10; c <= val; c *= 10) d++;
else for (c = -10 ; c >= val; c *= 10) d++;
return (c < 0) ? ++d : d;
}
> That is not the best way to do it
There's no such thing as the best overall way to do it. Your way might be better in Edward's program but worse in jephthah's, or vice versa. Though jephthah's function has the benefit of simplicity. :)
> heres a better way
The results are identical for the most part. Ed would argue that the sign has no business being counted as a digit, but that's something that depends on the needs of the program as well. ;) One big problem with your better way is that it fails to handle INT_MIN and INT_MAX.
Edward would do something more like this for the counting method:
int CountDigits(int value)
{
int count = 0;
value = std::abs(value);
do {
++count;
value /= 10;
} while (value != 0);
return count;
}
The results are identical
Not exactly, the function I made is Much more efficient and doesnt make any calls to any other functions. And I think the minus sign should count as a didget as if you are don't, trying to assign it to a char buffer and it wont have enough space if it's a negative number.
> the function I made is Much more efficient
Edward would argue that an infinite loop for some inputs isn't much more efficient. ;) Ed would also argue that any sane programmer will use a slow and correct function rather than a fast and broken function. Finally, pushing for efficiency at the cost of clarity without bottleneck benchmarks to back up the decision is usually a bad idea.
> trying to assign it to a char buffer and it wont have enough space if it's a negative number
That makes sense if all you use this function for is allocating buffers. There are other uses as well, and counting the sign would be treated as an error in many of them. I think you should avoid counting the sign and let the caller decide what to do:
int n = CountDigits(value);
if (value < 0 || includeSign)
++n;
...
But that's just a personal preference. As long as your choice is well documented, the caller can do the opposite just as easily:
int n = CountDigits(value);
if (value < 0)
--n;
...
Ive had that function for a year or two now, never failed on me o.0
Ed would also argue that any sane programmer will use a slow and correct function rather than a fast and broken function.
I agree there, although I would rather have both.
But I dont see how its broken.. :icon_confused:
You could also change the function very easily so you have a choise:
int CountDidgets(int val, bool signInc = 0) {
int d = 1, c;
if (val >= 0) for (c = 10; c <= val; c *= 10) d++;
else for (c= -10 ; c >= val; c *= 10) d++;
return (signInc && c < 0) ? ++d : d;
}
Now were both happy :)
best shmest.
I'm not trying to give bulletproof homework solutions.
i was just pointing out how one could "count characters" just as easily as one could divide/modulo
.
But I dont see how its broken.. :icon_confused:
Edward made a good point there, if you try
cout << CountDidget(INT_MAX) << '\n';
What happens :?:
Edward made a good point there, if you try
cout << CountDidget(INT_MAX) << '\n';
Im just gona have to keep changing ;)
template<typename type>
int CountDidgets(type val, bool signInc = 0) {
int d = 1; long long c;
if (val >= 0) for (c = 10; c <= val; c *= 10) d++;
else for (c= -10 ; c >= val; c *= 10) d++;
return (signInc && c < 0) ? ++d : d;
}
Now try this:
cout << CountDidgets<unsigned int>(INT_MAX) << '\n';
Should work now :icon_wink:
Hi,
wt is the function that can use to count number of digits in a long integer?
e.g. 12345678 -> 8
11 ->2
456 -> 3
6724 -> 4
yeah...... that's my main problem here....
what about in C only? the same question I would like to raise for you,
what is the function that can be used to count the number of days that a thread has been dead and buried?
// here I am going to write my first coding in semester fall 2012
#include <iostream>
using namespace std;
int thisisafun(int);
void main()
{
//finding number of digits in a number
cout<<"Please enter a valid number having more than one digit\n";
int a;
cin>>a;
cout<<"the number of digits are "<<thisisafun(a);
system("pause");
}
int thisisafun(int a)
{
if(a>-10 && a<10)
return 1;
else
{
return 1+thisisafun(a/10);
}
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.