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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -