Write the NUM function of the type int which obtains an integer value N (positive or negative) of the long type as a parameter and which gives the number of digits of N as a result.

Write a small program that tests the function NUM.

Example:

          Enter an integer number: 6457392

          The number 6457392 has 7 digits.


Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>

int ndigits(long N)
{
	if (N != 0)
		return 1 + ndigits(N / 10);
	return 0;
}

void main()
{
	long n;
	
	do {
		printf("Enter an integer: ");
		scanf("%ld", &n);
	} while (n < 0);

	printf("Number of digits = %d.", ndigits(n));

	getch();
}

 

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Prim Algorithm - Minimal Spanning Tree