0

write a program to input a number and print the number of digits used.

without array

16th Aug 2016, 6:04 PM
Asim
Asim - avatar
3 Respostas
+ 2
std::ceil(std::log10(std::abs(x))); does the charm, too. :-) Nevertheless, you have to include <cmath>.
17th Aug 2016, 2:46 PM
Stefan
Stefan - avatar
+ 1
#include <iostream> void main() { int x, n(0); std::cin >> x; while (x != 0) { x /= 10; ++n; } std::cout << n; } Not tested it but I believe this will work for any positive integer.
17th Aug 2016, 3:07 AM
Ahkrin
+ 1
thanks man it works
17th Aug 2016, 8:51 AM
Asim
Asim - avatar