// C++로 시작하는 객체지향 프로그래밍 p.282 예제 6.28
Q. 정수의 숫자 개수를 반환하는 함수를 작성하여라. 예를 들어, getSize(45)는 2를 반환하고, getSize(3434)는 4를 , getSize(4)는 1을 반환한다.
#include <iostream>
using namespace std;
int getSize(int n){
int count = 0;
while(n != 0){
n /= 10;
count++;
}
return count;
}
int main(){
int x;
cout << "Enter an integer :";
cin >> x;
cout << "The number of digits in " << x << " is " << getSize(x) << endl;
system("pause");
return 0;
}
댓글 없음:
댓글 쓰기