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
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>
int digits(long N)
{
/* N can be modified in the function
since it is passed by copy. */
int i;
/* modify the sign if N is negative */
if (N<0)
N *= -1;
/* count the digits */
for (i = 1; N>10; i++)
N /= 10;
return i;
}
void main()
{
long A;
printf("Enter an integer : ");
scanf("%ld", &A);
printf("The number %ld have %d digits.\n", A, digits(A));
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Star pattern 18