Dremendo Tag Line

Input a number and count digits using a function in C

Function - Question 1

In this question, we will see how to input a number and count how many digits are there in it in C programming using a function. To know more about function click on the function lesson.

Q1) Write a program in C to input a number and count how many digits are there in it using a function.

Program

#include <stdio.h>
#include <conio.h>

int countdigit(int num)
{
    int dc=0;
    while(num>0)
    {
        dc=dc+1;
        num=num/10;
    }
    return dc;
}

int main()
{
    int n;
    printf("Enter a number ");
    scanf("%d",&n);
    printf("Total Number of Digits = %d",countdigit(n));
    return 0;
}

Output

Enter a number 42863
Total Number of Digits = 5
video-poster