Răspuns :
varianta return si eficienta
#include<iostream>
#include<cmath>
using namespace std;
int nrcifre(int n){
return (int) log10 ((double) n) + 1;
}
int main(){
int n;
cout<<nrcifre(n);
return 0;
}
varianta return si eficienta - fara functii predefinite
#include<iostream>
using namespace std;
int nrcifre(int x){
return(x<10?1: (x<100?2: (x<1000?3: (x<10000?4: (x<100000?5: (x<1000000?6: (x<10000000?7: (x<100000000?8: (x<1000000000?9: 10)))))))));
}
int main(){
int n;
cout<<nrcifre(n);
return 0;
}
varianta return eficienta - stringuri
#include<bits/stdc++.h>
using namespace std;
int nrcifre(int x){
return to_string(x).length();
}
int main(){
int n;
cin>>n;
cout<<nrcifre(n);
return 0;
}
varianta didactica
#include<iostream>
using namespace std;
int nrcifre(int n) {
int k=0;
while(n!=0) {
n/=10;
++k;
}
return k;
}
int main(){
int n;
cin>>n;
cout<<nrcifre(n);
}