algorithm - How do i determine the first digit of a number without knowing the number of digits?c++ -
for example, 42556
. how determine first digit if don't know number of digits in number? could't find algorithm suits me anywere! (i mean determine 4 in 42556)
assuming a
input number.
#include <iostream> #include <cmath> using namespace std; int main() { long = 42556; long num; num=floor(log10(a))+1; //cout<<num<<" "<<"\n"; //prints number of digits in number cout<<a/(int)pow(10,num-1)<<"\n"; //prints first digit cout<<a%10<<"\n"; //prints last digit return 0; }
live demo here.
Comments
Post a Comment