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

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

Write a Program to Count Number of Digits in Number using For Loop

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

#include <stdio.h>

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

	printf("Enter the number :--> ");
	scanf("%ld", &n);

	m = n;

    for(; n > 0; )
	{
		n = n / 10;
		count = count + 1;  //count digits
	}
	printf("Number of digits in %ld :--> %d", m, count);
	return 0;
}

Output:

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