Answer to Question #180673 in C++ for Anshu Kumar

Question #180673

2. Param is a course instructor of c++ programming and currently he is teaching conversions to his students. He started with the basic programs and now he want to introduce some complex problems. Before starting the complex problems, he wants to start with high difficulty level problem, such as: counting the digits of a number. Help Mr. param to write a program which will accept one integer argument and returns count of digits of that argument using class to basic conversion.


1
Expert's answer
2021-04-12T17:57:44-0400
#include <iostream>
#include <string>
using namespace std;


class CounterDigit{
private:
	long int number;
public:
	//Constructor
	CounterDigit(long int number){
		this->number=number;
	}
	//class to basic conversion
	operator int(){
		int counter = 0;
		while (number != 0){
			number = number / 10;
			counter++;
		}
		return counter;
	}
};
int main(){
	long int number;
	//Read the number
	cout<<"Enter the number: ";
	cin>>number;
	//create CounterDigit object
	CounterDigit counterDigit(number);
	cout<<"Number of digits: "<<counterDigit<<"\n";
	system("pause");
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS