C++ Program to Count Number of Digits in Number using For Loop

Write C++ Program to Count Number of Digits in Number using For Loop

// CPP Program to Count Number of Digits in Number using For Loop

#include <iostream>

using namespace std;

int main()
{
	long int n, m;
	int Count = 0;

	cout << "Enter the number :--> ";
	cin >> n;

	m = n;

        for(; n > 0; )
	{
		n = n / 10;
		Count = Count + 1;  //count digits
	}
	cout << "Number of digits in " << m << " :--> " << Count;

	return 0;
}

Output:

Enter the number :--> 654321
Number of digits in 654321 :--> 6

≪ Previous | Next ≫